你怎么带回主窗口?

时间:2014-05-14 09:47:05

标签: python class tkinter

使用类,我创建了一个窗口,其中包含一个隐藏窗口并创建新窗口的按钮。我的问题是如何在新窗口中创建一个破坏自身并带回主窗口的按钮?

from tkinter import *

class Demo1:
    def __init__(self, master):
        self.master = master
        self.button1 = Button(self.master, text = 'New Window', width = 25, command = self.new_window)
        self.button1.pack()
    def new_window(self):
        self.master.withdraw() #Hides main window
        self.newWindow = Toplevel()
        app = Demo2(self.newWindow)

class Demo2:
    def __init__(self, master):
        self.master = master
        self.quitButton = Button(self.master, text = 'Quit', width = 25, command = self.close_windows)
        self.quitButton.pack()
    def close_windows(self):
        #What do I put here to bring back the main window?
        self.master.destroy()

def main(): 
    root = Tk()
    app = Demo1(root)
    root.mainloop()

main()

1 个答案:

答案 0 :(得分:1)

您要找的是deiconify

from tkinter import *

class Demo1:
    def __init__(self, master):
        self.master = master
        self.button1 = Button(self.master, text = 'New Window', width = 25, command = self.new_window)
        self.button1.pack()

    def new_window(self):
        self.master.withdraw() #Hides main window
        self.newWindow = Toplevel()
        app = Demo2(self.newWindow,self.master)

class Demo2:
    def __init__(self, master,oldmaster):
        self.master = master
        self.quitButton = Button(self.master, text = 'Quit', width = 25, command = lambda:self.close_windows(oldmaster))
        self.quitButton.pack()

    def close_windows(self,oldmaster):
        #What do I put here to bring back the main window?
        oldmaster.deiconify()
        self.master.destroy()

def main(): 
    root = Tk()
    app = Demo1(root)
    root.mainloop()

main()