How to close a current window and open a new window at the same time?
<p>This code opens a button, which links to another button. The other button can close its self, but the first button can't close itself and open a new one at the same time, How do I fix this?</p>

<pre><code>import tkinter as tk

class Demo1:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.HelloButton = tk.Button(self.frame, text = 'Hello', width = 25, command = self.new_window,)
        self.HelloButton.pack()
        self.frame.pack()
    def close_windows(self):
        self.master.destroy()
        self.new_window
    def new_window(self):
        self.new_window = tk.Toplevel(self.master)
        self.app = Demo2(self.new_window)


class Demo2:
    def __init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25, command = self.close_windows)
        self.quitButton.pack()
        self.frame.pack()
    def close_windows(self):
        self.master.destroy()

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

if __name__ == '__main__':
    main()
</code></pre>

<p>Try redefine your <code>Demo1.new_window()</code> as below:</p>

<pre><code>def new_window(self):
    self.master.destroy() # close the current window
    self.master = tk.Tk() # create another Tk instance
    self.app = Demo2(self.master) # create Demo2 window
    self.master.mainloop()
</code></pre>

python tkinter