我目前正在使用 tkinter 开发一个 GUI,它允许用户在不同的帧之间导航。我的代码结构基于此用户响应。我现在的问题是在更改框架时自动将 tkinter 的窗口调整为其内容。我希望窗口的边框适合红色箭头后面的小部件,如此处和此处(下面示例代码的输出),以便每一帧中都没有这样的“空白空间”。该代码的工作方式是该方法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)
添加回答
举报
0/150
提交
取消