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
#1
I hope my formatting is proper.
My code:
from tkinter import *
from tkinter import font

root = Tk()
root.geometry('500x500')
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)
button1.pack()

root.mainloop()
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.
Reply
#2
add width and height values to button to force desired size.
Reply
#3
(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.
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter menu font size -method to change tonycat 2 1,193 Oct-11-2020, 02:43 AM
Last Post: tonycat
  [tkinter] color change for hovering over button teacher 4 2,153 Jul-04-2020, 06:33 AM
Last Post: teacher
  [PyQt] Increase text size and change color based on temp pav1983 5 671 Jun-22-2020, 10:52 PM
Last Post: menator01
  TKINTER - Change font color for night or day Ayckinn 2 1,113 May-24-2020, 09:25 PM
Last Post: Ayckinn
  [PyQt] Qtableview adapte size to WBPYTHON 3 1,218 Mar-23-2020, 01:51 AM
Last Post: deanhystad
  [Tkinter] Treeview expand button size shamnadinn 8 1,950 Mar-08-2020, 12:02 PM
Last Post: shamnadinn
  Tkinter scaling windows conten to or with its size not working Detzi 5 1,187 Jan-12-2020, 12:42 PM
Last Post: Detzi
  [Tkinter] Scrollbar, Frame and size of Frame Maksim 2 4,577 Sep-30-2019, 07:30 AM
Last Post: Maksim
  [Tkinter] changing background color of a button ieee488 2 1,796 Sep-06-2019, 01:24 PM
Last Post: ieee488
  change background color of button ieee488 3 5,864 Jul-30-2019, 09:49 PM
Last Post: ieee488

Forum Jump:

User Panel Messages

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