Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 什么控制Tkinter中的自动窗口大小调整?_Python_Tkinter - Fatal编程技术网

Python 什么控制Tkinter中的自动窗口大小调整?

Python 什么控制Tkinter中的自动窗口大小调整?,python,tkinter,Python,Tkinter,Tkinter顶级窗口似乎有两种“模式”:大小由应用程序决定,以及用户控制大小。考虑这个代码: 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()

Tkinter顶级窗口似乎有两种“模式”:大小由应用程序决定,以及用户控制大小。考虑这个代码:

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()
每次我按下按钮,可见窗口的大小都会改变以适应附加标签。但是,如果我手动调整窗口的大小(使用鼠标),那么它将停止这种自动调整大小的行为,从那时起,我必须手动更改窗口的大小,以便在添加新按钮时查看它们

什么决定了顶级窗口的大小是由应用程序控制还是由用户控制


用户手动调整大小后,应用程序如何恢复自动调整大小?

规则非常简单-顶级窗口在给定固定大小时具有固定大小,否则它会“收缩以适应”

有两种方法可以给顶层窗口指定一个固定的大小:用户可以手动调整它的大小,或者应用程序代码可以在启动时调用为它指定一个大小

若要重置原始行为,请为窗口指定一个空几何体。例如:

def __init__(self,parent):
    ...
    self.b2 = Button(self, text="Reset", command=self.b2Press)
    self.b2.pack()

def b2Press(self):
    self.winfo_toplevel().wm_geometry("")

很好-确实是一个足够简单的答案,但我一直在努力寻找。