使用withdraw()后无法再次显示Tkinter根窗口

问题描述 投票:0回答:1

在我的程序中,我从root tkinter窗口创建一个窗口,并使用.withdraw()函数隐藏root。当我尝试通过调用根类再次显示根窗口时,它不显示并且我的程序退出。这是我描述问题的代码的大致轮廓:

 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中的打印功能,我能够看到当尝试切换回主窗口时,实际上调用了show(),并且似乎输入了整个类。

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

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

python user-interface tkinter window show
1个回答
2
投票

您提供的示例由于多种原因不起作用。

#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()

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

© www.soinside.com 2019 - 2024. All rights reserved.