Home > Back-end >  How can you make Python Tkinter Label text resize automatically along with the root window?
How can you make Python Tkinter Label text resize automatically along with the root window?

Time:05-28

I am trying to make a tkinter label that stays in the middle of the root window and resizes along with it.

Is there any simple way to do that with using just .place() - and not using .grid() ?

Here is my code:

from tkinter import *

root= Tk()
root.geometry('200x200')

my_label= Label(root, text= 'Hello World!', font= ('Calibri', 20))
my_label.place(relx= 0.5, rely= 0.5, anchor= CENTER)

root.mainloop()

CodePudding user response:

You can track the window size change and proportionally change the font size on the label.

from tkinter import *

i = 12

def config(event):
    global i
    i = 12
    w = root.winfo_width()
    h = root.winfo_height()
    k = min(w, h) / 200
    i = int(i   i*k)
    my_label['font'] = ('Calibri', i)


root= Tk()
root.geometry('200x200')

root.bind("<Configure>", config)

my_label= Label(root, text= 'Hello World!', font= ('Calibri', i))
my_label.place(relx= 0.5, rely= 0.5, anchor= CENTER)

root.mainloop()
  • Related