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()
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()
Using the same code as above, do not use the place. You can use a pack
or grid
.
from tkinter import *
root= Tk()
root.geometry('200x200')
my_label= Label(root, text= 'Hello World!', font= ('Calibri', 20))
my_label.pack(padx=20, pady=50, fill=BOTH, expand=True)
root.mainloop()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.