我目前正在使用tkinter开发一个GUI,它允许用户在不同的框架之间导航。我的代码结构基于this user response。在
我现在的问题是在框架发生变化时自动调整tkinter的窗口以适应其内容。我希望窗口的边框适合红色箭头后面的小部件,如here和{a3}(下面示例代码的输出),这样每个帧中都没有这样的“空白”。在
代码的工作方式是show_frame方法允许在两个定义的帧之间进行更改。在from tkinter import *
class WindowController(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
container = Frame(root)
container.grid()
self.frames = {}
for F in (FirstWindow, SecondWindow):
frame = F(parent=container, controller=self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(FirstWindow)
self.destroy()
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class FirstWindow(Frame):
def __init__(self, parent, controller):
self.controller = controller
self.parent = parent
Frame.__init__(self, parent)
# Widgets:
# "Do Nothing" Button
self.Title0 = Button(self, text='Do nothing')
self.Title0.grid(row=0, column=0)
# "Do Nothing" Button
self.Title1 = Button(self, text='Do Nothing')
self.Title1.grid(row=1, column=0)
# Change Frame Button
self.Title2 = Button(self, text='To Second Window', command=lambda: self.controller.show_frame(SecondWindow))
self.Title2.grid(row=2, column=0)
class SecondWindow(Frame):
def __init__(self, parent, controller):
self.controller = controller
self.parent = parent
Frame.__init__(self, parent)
# Widgets:
# "Do Nothing" Button
self.Title0 = Button(self, text='Do nothing')
self.Title0.grid(row=0, column=0)
# "Do Nothing" Button
self.Title1 = Button(self, text='Do Nothing')
self.Title1.grid(row=0, column=1)
# Change Frame Button
self.Title2 = Button(self, text='To First Window', command=lambda: self.controller.show_frame(FirstWindow))
self.Title2.grid(row=0, column=2)
if __name__ == "__main__":
root = Tk()
root.resizable(width=False, height=False)
app = WindowController()
app.mainloop()
我的问题似乎与here中的问题相似,但没有得到解决。在
这是我在这里的第一个问题,所以非常感谢批评。在
如果觉得《python窗口大小动态变化_如何在tkinter中动态调整窗口大小以“自动调整”其内容?...》对你有帮助,请点赞、收藏,并留下你的观点哦!