内容が変化するウィンドウがあります。コンテンツがウィンドウよりも大きい場合があるため、ウィンドウはその子に合わせて拡大されます。ただし、「ジオメトリ」への呼び出しを使用してウィンドウを中央に配置すると、ウィンドウのサイズが変更されなくなりました。以下に、これを示すコードを示します。
遅延された center() 関数呼び出しをコメント アウトすると、ウィンドウがコンテンツに合わせて拡大されます。そのままにしておくと、ウィンドウは中央に配置されますが、コンテンツに合わせて拡大することはありません。
ウィンドウを中央に配置し、コンテンツに合わせてサイズを変更し続けることはできますか?
from Tkinter import *
import ttk
def center(root):
w = root.winfo_screenwidth()
h = root.winfo_screenheight()
rootsize = tuple(int(_) for _ in root.geometry().split('+')[0].split('x'))
x = w/2 - rootsize[0]/2
y = h/2 - rootsize[1]/2
root.geometry("%dx%d+%d+%d" % (rootsize + (x, y)))
root = Tk()
var = StringVar()
var.set('Small Text')
label = ttk.Label(root, textvariable=var)
label.grid(column=0, row=0)
# Change the text label in a couple of seconds.
def changeit():
var.set('BIG TXT - ' * 5)
root.after(2000, changeit)
# Comment out this center call and the label expands.
root.after(100, lambda: center(root))
root.mainloop()