什么控制 Tkinter 中的自动窗口大小调整? [英] What controls automated window resizing in Tkinter?
问题描述
Tkinter 顶级窗口似乎有两种模式":大小由应用程序确定,以及用户控制大小.考虑以下代码:
Tkinter top level windows seem to have two "modes": where the size is being determined by the application, and where the user controls the size. Consider this code:
from tkinter import *
class Test(Frame):
def __init__(self,parent):
Frame.__init__(self,parent)
self.b1 = Button(self, text="Button 1",command=self.b1Press)
self.b1.pack()
def b1Press(self):
print("b1Press")
label = Label(self, text="Label")
label.pack()
root = Tk()
ui = Test(root)
ui.pack(fill='both', expand=1)
root.mainloop()
每次我按下按钮时,可见窗口都会改变大小以适应附加标签.但是,如果我手动(使用鼠标)调整窗口大小,则会停止这种自动调整大小的行为,从那时起,我必须手动更改窗口大小以查看添加的新按钮.
Each time I press the button, the visible window changes size to fit an additional label. However, if I resize the window manually (with the mouse), then it stops this auto resizing behaviour, and from then on I have to manually change the size of the window to view new buttons as they are added.
什么决定顶层窗口的大小是由应用程序控制还是由用户控制?
What determines whether the size of a toplevel window is under control of the application or the user?
用户手动调整大小后,应用程序如何恢复自动调整大小?
How can the application regain automatic sizing after the user has manually resized?
推荐答案
规则非常简单 - 顶层窗口在被赋予固定大小时具有固定大小,否则它会缩小以适应".
The rule is pretty simple - a toplevel window has a fixed size whenever it has been given a fixed size, otherwise it "shrinks to fit".
有两种方法可以给顶层窗口一个固定的大小:用户可以手动调整它的大小,或者你的应用程序代码可以调用 wm_geometry
在启动时给它一个大小.
There are two ways to give the top level window a fixed size: the user can resize it manually, or your application code can call wm_geometry
to give it a size at startup.
要重置原始行为,请为窗口提供一个空几何体.例如:
To reset the original behavior, give the window a null geometry. For example:
def __init__(self,parent):
...
self.b2 = Button(self, text="Reset", command=self.b2Press)
self.b2.pack()
def b2Press(self):
self.winfo_toplevel().wm_geometry("")
这篇关于什么控制 Tkinter 中的自动窗口大小调整?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!