Tkinter框架调整大小

时间:2016-03-14 15:06:48

标签: python user-interface 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复制了此代码,因为我采用了相同的方法。

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

1 个答案:

答案 0 :(得分:3)

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