Tkinter收缩窗口以与扩展框架相同的方式适合框架吗?

时间:2019-08-27 14:28:02

标签: python tkinter

编辑:我找到了解决方法! grid_forget()将您来自的帧并网格化您要去的帧。使用try / except,以便程序启动时不会立即失败。

以下是该修复程序的代码:

def show_frame(self, page_name):
    try:
        self.frame.grid_forget()
    except:
        print("Frame not found")
    frame = self.frames[page_name]
    frame.grid(row=0, column=0, sticky="nsew")
    self.frame = frame

原始帖子:

我正在使用this approach处理帧并在它们之间切换。

我有多个要切换的框架,当切换框架时,我希望窗口调整大小以适应looked like this before.

当框架发生变化并且结果为looks like this

时,我会遍历网格以调整元素的大小。

I mocked up the final result I'm looking for

任何帮助都太棒了!

这是用于调整网格大小以适合框架中的元素的代码

    for i in range(frame.grid_size()[0]):
        frame.grid_columnconfigure(i, minsize=0, weight=0)

    for j in range(frame.grid_size()[1]):
        frame.grid_rowconfigure(j, minsize=0, weight=0)

这是我尝试过的各种各样的东西(对它们都未加注释)

    self.update_idletasks()
    self.update()
    self.minsize(width=0, height=0)

    self.geometry((str(frame.winfo_width()) + 'x' + str(frame.winfo_height())))
    self.container.pack(side="top", fill="both", expand=True, shrink=True)
    self.resizable(0, 0)
    frame.pack_propagate(0)
    self.pack_propagate(0)
    self.container.pack_forget()
    self.container.pack(side="top", fill="both", expand=True)

这是更换框架的全部代码

class frameLauncher(tk.Tk):

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

        container = tk.Frame(self)
        self.container = container
        container.pack(side="top", fill="both", expand=True)

        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        # container.grid_rowconfigure(1, weight=1)
        # container.grid_columnconfigure(1, weight=1)
        # container.grid_rowconfigure(2, weight=1)
        # container.grid_columnconfigure(2, weight=1)

        self.frames = {}
        for F in (mainWindow,
                  modifyEventWindow,
                  oneOffEventModifierWindow,
                  repeatableEventModificationWindow,
                  cycleEventModificationWindow,
                  settingsWindow):
            page_name = F.__name__

            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("mainWindow", None)

    def show_frame(self, page_name, event):
        '''Show a frame for the given page name'''

        frame = self.frames[page_name]
        editorList = ["oneOffEventModifierWindow", "repeatableEventModificationWindow", "cycleEventModificationWindow"]
        if page_name in editorList:
            frame.loadEvent(event)
        for i in range(frame.grid_size()[0]):
            frame.grid_columnconfigure(i, minsize=0, weight=0)
        for j in range(frame.grid_size()[1]):
            frame.grid_rowconfigure(j, minsize=0, weight=0)

        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)
        self.container.grid_rowconfigure(0, weight=1)
        self.container.grid_columnconfigure(0, weight=1)

        frame.blankFrame()  #Deletes the elements in the frame to reload
        frame.tkraise()

1 个答案:

答案 0 :(得分:0)

如果您将所有小部件打包/网格化在不同的Frame对象上并在它们之间切换,然后执行以下操作:

root.rowconfigure(0, weight = 1)
root.columnconfigure(0, weight = 1)

然后,您只需要raise窗口的re-grid中的Framegrid space (0, 0)对象