簡體   English   中英

Tkinter 框架調整大小

[英]Tkinter Frame Resize

我已經被困了幾天,試圖弄清楚如何使用這種方法動態調整 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復制了這段代碼,因為我遵循相同的方法。

我面臨的問題是,使用這種方法,所有框架都堆疊在一個容器中,並且該容器的大小是其最大框架的大小。 從一幀移動到另一幀不會動態調整相應窗口的大小,並會在小幀中產生巨大的可用空間。 我嘗試了不同的技術來使容器中的所有幀動態調整大小,但沒有取得多大成功。 有人可以建議我能做什么嗎?

不要堆疊框架,而是確保一次只有一個由網格管理。 您可以通過調用做到這一點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("")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM