Tkinter 框架调整大小

use*_*134 4 python user-interface tkinter

我已经被困了几天,试图弄清楚如何使用这种方法动态调整 TKInter 中的框架大小。

class SampleApp(tk.Tk):


    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (StartPage, PageOne, PageTwo):
            page_name = F.__name__
            frame = F(container, self)
            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("StartPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is the start page", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        button1 = tk.Button(self, text="Go to Page One",
                            command=lambda: controller.show_frame("PageOne"))
        button2 = tk.Button(self, text="Go to Page Two",
                            command=lambda: controller.show_frame("PageTwo"))
        button1.pack()
        button2.pack()


class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 1", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 2", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()
Run Code Online (Sandbox Code Playgroud)

我从Switch between two frames in tkinter复制了这段代码,因为我遵循相同的方法。

我面临的问题是,使用这种方法,所有框架都堆叠在一个容器中,并且该容器的大小是其最大框架的大小。从一帧移动到另一帧不会动态调整相应窗口的大小,并会在小帧中产生巨大的可用空间。我尝试了不同的技术来使容器中的所有帧动态调整大小,但没有取得多大成功。有人可以建议我能做什么吗?

Bry*_*ley 6

不要堆叠框架,而是确保一次只有一个由网格管理。您可以通过调用grid_remove()当前帧然后grid()在新帧上执行此操作。或者,由于懒惰,您可以调用grid_remove()所有内容,这样您就不必记住哪个页面是当前页面。

def show_frame(self, page_name):
    '''Show a frame for the given page name'''
    for frame in self.frames.values():
        frame.grid_remove()
    frame = self.frames[page_name]
    frame.grid()
Run Code Online (Sandbox Code Playgroud)

注意:如果您使用geometry根窗口上的方法为主窗口设置固定大小,或者用户手动调整窗口大小,则自动调整大小将停止工作。这是因为 tkinter 假设如果某些内容明确请求窗口大小,则应该遵守该大小。

如果您总是希望窗口调整大小,则应将几何图形重置为空字符串。您可以将其添加为方法中的最后一条语句show_frame

frame.winfo_toplevel().geometry("")
Run Code Online (Sandbox Code Playgroud)