打开 Toplevel 窗口时隐藏根窗口,并在 Toplevel 销毁时使其重新出现 [英] Hide the root window when a Toplevel window is opened and make it reappear when the Toplevel is destroyed

查看:30
本文介绍了打开 Toplevel 窗口时隐藏根窗口,并在 Toplevel 销毁时使其重新出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在打开 SECOND 窗口时隐藏 MAIN 窗口,然后在关闭 SECOND 窗口时重新出现 MAIN 窗口?

How can I hide the MAIN window when the SECOND window is opened and then make the MAIN window reappear when the SECOND window is closed?

我了解withdraw() 和deiconify() 的用法,但不确定如何在这种情况下应用它们.

I understand the use of withdraw() and deiconify() but not sure how to apply them in this situation.

这样做的原因是最终会创建一个程序,该程序将主窗口用作菜单,当从它打开其他窗口时隐藏并在退出这些其他窗口时重新出现.

The reason for this would be to eventually create a program which the MAIN window acts as a menu which hides when other windows are opened from it and reappears when these other windows are exit.

from tkinter import *

class Main():

    def __init__(self, master):
        self.master = master
        self.title = "Main Window"

        self.button1 = Button(self.master, text="Click Me", command = self.Open)
        self.button1.grid(row=0, column=0, sticky=W)
        self.button2 = Button(self.master, text="Close", command = self.Close)
        self.button2.grid(row=1, column=0, sticky=W)


    def Open(self):
        second_window = Toplevel(self.master)
        window2 = Second(second_window)

    def Close(self):
        self.master.destroy()


class Second():

    def __init__(self, master):
        self.master = master
        self.title = "Second Window"



root = Tk()
main_window = Main(root)
root.mainloop()

任何帮助将不胜感激.

推荐答案

您可以在第二个窗口的 上放置一个绑定,该绑定将调用将调用 deiconify 的函数 在母版上.

You can put a binding on <Destroy> of the second window which will call a function that will call deiconify on the master.

如果 SecondToplevel 的子类,这在您的代码中会更容易.如果这样做,您可以将此代码隐藏在 Second 的定义中.例如:

This would be easier in your code if Second was a subclass of Toplevel. If you did that, you could hide this code inside the definition of Second. For example:

...
def Open(self):
    second_window = Second(self.master)
...

class Second(Toplevel):

    def __init__(self, master):
        Toplevel.__init__(self, master)
        self.master = master
        self.master.withdraw()
        self.bind("<Destroy>", self.on_destroy)

    def on_destroy(self, event):
        if event.widget == self:
            self.master.deiconify()

这篇关于打开 Toplevel 窗口时隐藏根窗口,并在 Toplevel 销毁时使其重新出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆