python – Tkinter框架调整大小

我已经被困了几天试图弄清楚如何使用这种方法动态调整TKIn​​ter中的帧.

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()

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

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

而不是堆叠帧,确保一次只有一个网格管理.您可以通过调用当前帧的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()

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

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

frame.winfo_toplevel().geometry("")
https://stackoverflow.com/questions/35991126/tkinter-frame-resize

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:python – Tkinter框架调整大小