user3500082
user3500082

Reputation: 43

How do you bring back a main window?

Using classes, I have created a window with a button that hides the window and creates a new window. My question is how to I make a button in the new window that destroys itself and brings back the main window?

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

Upvotes: 1

Views: 764

Answers (1)

Alok
Alok

Reputation: 2689

What you are looking for is 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()

Upvotes: 1

Related Questions