Close main window after opening a new one
Question: I found this example of code here on stackoverflow and I would like to make
the first window close when a new one is opened. So what I would like is when
a new window is opened, the main one should be closed automatically.

    
    
    #!/usr/bin/env python
    import Tkinter as tk
    
    from Tkinter import *
    
    class windowclass():
    
            def __init__(self,master):
                    self.master = master
                    self.frame = tk.Frame(master)
                    self.lbl = Label(master , text = "Label")
                    self.lbl.pack()
                    self.btn = Button(master , text = "Button" , command = self.command )
                    self.btn.pack()
                    self.frame.pack()
    
            def command(self):
                    print 'Button is pressed!'
    
                    self.newWindow = tk.Toplevel(self.master)
                    self.app = windowclass1(self.newWindow)
    
    class windowclass1():
    
            def __init__(self , master):
                    self.master = master
                    self.frame = tk.Frame(master)
                    master.title("a")
                    self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25 , command = self.close_window)
                    self.quitButton.pack()
                    self.frame.pack()
    
    
            def close_window(self):
                    self.master.destroy()
    
    
    root = Tk()
    
    root.title("window")
    
    root.geometry("350x50")
    
    cls = windowclass(root)
    
    root.mainloop()
    


Answer: You would withdraw the main window, but you have no way to close the program
after the button click in the Toplevel, when the main window is still open but
doesn't show Also pick one or the other of (but don't use both)

    
    
    import Tkinter as tk
    
    from Tkinter import *
    

This opens a 2nd Toplevel which allows you to exit the program

    
    
    import Tkinter as tk
    
    class windowclass():
    
            def __init__(self,master):
                    self.master = master
                    ##self.frame = tk.Frame(master)  not used
                    self.lbl = tk.Label(master , text = "Label")
                    self.lbl.pack()
                    self.btn = tk.Button(master , text = "Button" , command = self.command )
                    self.btn.pack()
                    ##self.frame.pack()  not used
    
            def command(self):
                    print 'Button is pressed!'
    
                    self.master.withdraw()
                    toplevel=tk.Toplevel(self.master)
                    tk.Button(toplevel, text="Exit the program",
                              command=self.master.quit).pack()
                    self.newWindow = tk.Toplevel(self.master)
                    self.app = windowclass1(self.newWindow)
    
    class windowclass1():
    
            def __init__(self , master):
                    """ note that "master" here refers to the TopLevel
                    """
                    self.master = master
                    self.frame = tk.Frame(master)
                    master.title("a")
                    self.quitButton = tk.Button(self.frame,
                                      text = 'Quit this TopLevel',
                                      width = 25 , command = self.close_window)
                    self.quitButton.pack()
                    self.frame.pack()
    
            def close_window(self):
                    self.master.destroy()  ## closes this TopLevel only
    
    root = tk.Tk()
    
    root.title("window")
    
    root.geometry("350x50")
    
    cls = windowclass(root)
    root.mainloop()
    

