簡體   English   中英

如何使 Python Tkinter Label 文本隨根 window 自動調整大小?

[英]How can you make Python Tkinter Label text resize automatically along with the root window?

我正在嘗試制作一個 tkinter label 保留在根 window 的中間並隨之調整大小。

有什么簡單的方法可以只使用 .place( .place()而不使用.grid()來做到這一點?

這是我的代碼:

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()

您可以跟蹤窗口大小的變化並按比例更改標簽上的字體大小。

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()

使用與上面相同的代碼,不要使用該位置。 您可以使用packgrid

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM