Python Forum
[Tkinter] Trying to change font size w/o changing button size
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Trying to change font size w/o changing button size
I hope my formatting is proper.
My code:
from tkinter import *
from tkinter import font

root = Tk()
root.title('Tkinter Frame Test')
myFont = font.Font(family = 'Helvetica', size = 20)

frame1 = Frame(root, bg='green')
frame1.pack(expand=True, fill=BOTH)

frame2 = Frame(frame1, width = 20, height = 20)
frame2.pack(expand = False)

button1 = Button(frame2, text='Grrrrr', font = myFont)

Made this little tkinter test code, i make tiny programs for testing so i don't screw up my main program, and what i am trying to do is get a button where i can change the font size of the button text without changing the button size. I know width= and height= are specified in character width which is why the button size gets bigger when the font get bigger. My understanding is that to make this work, i need to put the button in its own frame as the frame is specified in pixels and i just have to set the button to expand=True and fill=BOTH. I just can't get it to work. am i wrong in my thinking? am i coding it wrong? I made frame1 the root frame, frame2 the button frame inside of frame1 to hold my button. no matter what i do, when i change font size, button size changes even if i try to "freeze" frame1 by expand=False.
add width and height values to button to force desired size.
(Aug-04-2020, 11:23 AM)Larz60+ Wrote: add width and height values to button to force desired size.

when i do that, the button becomes almost as large as the window. the root.geometry('500x500') is in pixels , the button is still 20 characters high and wide, not sure what 20/20 on frame is is, doesnt seem like pixels or characters. i hope i am explaining this correctly.
if you use the variables, you control the width and height, it will become whatever size you specify
I am talking about width and height in the Button widget definition:
button1 = Button(frame2, text='Grrrrr', font = myFont, width=100, height=25)
I just guessed at sizes, you will have to adjust

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Horizontal extension of widgets + frame size adapted to content Fab117 3 207 Feb-22-2024, 06:54 PM
Last Post: deanhystad
  Can't change the colour of Tk button text Pilover 6 14,223 Nov-15-2022, 10:11 PM
Last Post: woooee
  [PyQt] QVBoxLayout default (minimum) size malonn 5 3,949 Oct-13-2022, 04:20 PM
Last Post: malonn
  [WxPython] [SOLVED] How to change button label? Winfried 3 1,941 May-31-2022, 06:37 PM
Last Post: Winfried
  [Tkinter] canvas size DPaul 2 5,462 May-04-2022, 12:34 PM
Last Post: DPaul
  Can't get tkinter button to change color based on changes in data dford 4 3,265 Feb-13-2022, 01:57 PM
Last Post: dford
  how to change font size barryjo 4 3,565 Jan-26-2022, 08:46 PM
Last Post: menator01
  [Tkinter] Trouble changing Font within tkinter frame title AnotherSam 1 3,836 Sep-30-2021, 05:57 PM
Last Post: menator01
  [Tkinter] changing and getting value through button niski1996 2 1,727 Aug-24-2021, 04:44 PM
Last Post: niski1996
  Tkinter menu font size -method to change tonycat 2 7,557 Oct-11-2020, 02:43 AM
Last Post: tonycat

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020