使用 withdraw() 后无法再次显示 Tkinter Root Window

Can't Show Tkinter Root Window Again After Using withdraw()

在我的程序中,我从我的根 tkinter window 创建一个 window,并使用 .withdraw() 函数隐藏根。当我尝试通过调用根 class 再次显示根 window 时,它没有显示并且我的程序退出了。这是我描述问题的代码的粗略概述:

 class MainGUI:
    def __init__(self, master):
        self.master = master
        #....Create and .grid() all GUI Widgets....

        # Button for switching to other window
        button = Button(text="CLICKME", command=lambda: self.other_window())

        # Call and define show function at the end of __init__
        self.show()
        def show(self):
            self.master.update()
            self.master.deiconify()

        # Create other window and withdraw self on button click
        def other_window(self):
            OtherGUI(self.master)
            self.master.withdraw()


class OtherGUI:
    def __init__(self, master):
        # Function for returning to main window, calls MainGUI class
        # to create window and withdraws self.
        def main_window():
            MainGUI(self.master)
            self.master.withdraw()

        master = self.master = Toplevel(master)
        #....Create and .grid() all GUI Widgets....

        # Button for switching back to main window
        button = Button(text="CLICKME", command=lambda: self.main_window())

使用 MainGUI 中的打印功能,我能够看到当尝试切换回主 window 时,实际上调用了 show(),并且整个 class 确实看起来被输入。

这让我感到困惑,因为我只是从其他论坛帖子中真​​正学会了如何做到这一点,并且使用 root.update() 和 .deiconify() 似乎是大多数人的解决方案,但是我有不知道为什么这不起作用。

有没有人知道我哪里出错了?

由于多种原因,您提供的示例将无法运行。

#really you should build your gui as an inherited class as it makes things much easier to manage in tkinter.
class MainGUI:
    def __init__(self, master):
        self.master = master


        button = Button(text="CLICKME", command=lambda: self.other_window())
        # no need for lambda expressions here.
        # missing geometry layout... grid(), pack() or place()

        self.show()
        # self.show does nothing here because your show method is improperly indented.
        # your other_window method is also not properly indented.

        def show(self):
            self.master.update()
            self.master.deiconify()

        def other_window(self):
            OtherGUI(self.master)
            self.master.withdraw()


class OtherGUI:
    def __init__(self, master):
        # this function should be its own method.
        def main_window():
            MainGUI(self.master)
            self.master.withdraw()

        master = self.master = Toplevel(master)
        # this is not how you should be defining master.

        button = Button(text="CLICKME", command=lambda: self.main_window())
        # missing geometry layout... grid(), pack() or place()
        # your button command is using a lambda to call a class method but your define it as a function instead.

这是您正在尝试的一个更简单的版本:

import tkinter as tk


class MainGUI(tk.Tk):
    def __init__(self):
        super().__init__()
        tk.Button(self, text="Open Toplevel", command=self.open_toplevel_window).pack()

    def open_toplevel_window(self):
        OtherGUI(self)
        self.withdraw()


class OtherGUI(tk.Toplevel):
    def __init__(self, master):
        super().__init__()
        tk.Button(self, text="Close top and deiconify main", command=self.main_window).pack()

    def main_window(self):
        self.master.deiconify()
        self.destroy()


MainGUI().mainloop()

正如您在此处看到的那样,当您从控制主要 window 和顶层 windows 的 tkinter 类 继承时,管理它们变得更容易,执行任务的代码也更少.