【问题标题】:Tkinter Frame ResizeTkinter 帧调整大小
【发布时间】:2016-06-29 16:52:30
【问题描述】:

几天来,我一直在试图弄清楚如何使用这种方法在 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()

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

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

【问题讨论】:

    标签: python user-interface tkinter


    【解决方案1】:

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

    【讨论】:

    • 成功了!非常感谢,布莱恩。但是,我正在考虑分别为每个框架设置窗口大小。例如 frame1 - 300x300 , frame2 -400x550 等。有什么办法可以做到吗?
    • 其实,这比我想象的要容易。 show_frame 中的 if 语句很少能完成这项工作。
    • @user134:最简单的方法是在每一帧上将overrideredirect 设置为false。这样,您可以以正常方式直接为框架分配宽度和高度。这通常不是最好的方法。相反,将内部小部件配置为您想要的大小,并让框架扩大或缩小以适应小部件。如果您明确设置宽度和高度,如果用户更改字体或在具有不同分辨率的系统上运行,UI 可能无法很好地工作。
    • @BryanOakley 据您所知,有没有办法让这种过渡看起来更顺畅?虽然从功能上讲,它完成了工作,但我发现在删除当前帧然后添加新帧的过程中有一点闪光,这没有吸引力。我尝试使用 winfo_reqwidth/reqheight 属性,但在设置几何图形和重新调整小部件时加载似乎有很多滞后。
    • @BryanOakley:上面提到的jthree8问题你有解决方案吗?
    猜你喜欢
    • 2012-11-07
    • 1970-01-01
    • 2011-03-31
    • 2021-11-29
    • 1970-01-01
    • 2020-11-22
    • 2013-05-07
    • 2017-12-29
    • 1970-01-01
    相关资源
    最近更新 更多