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 this:</p>

<pre><code>import tkinter as tk

class windowclass():
    def __init__(self, master):
        self.master = master
        self.btn = tk.Button(master, text="Button", command=self.command)
        self.btn.pack()

    def command(self):
        self.master.withdraw()
        toplevel = tk.Toplevel(self.master)
        toplevel.geometry("350x350")
        app = Demo2(toplevel)

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

root = tk.Tk()
root.title("window")
root.geometry("350x350")
cls = windowclass(root)
root.mainloop()
</code></pre>

<p>Or maybe sometimes,you want to do something else,you can hide it.</p>

<pre><code>def __init__(self, parent):
    self.root = parent
    self.root.title("Main")
    self.frame = Tk.Frame(parent)
    self.frame.pack()
    btn = Tk.Button(self.frame, text="New", command=self.openFrame)
    btn.pack()

def hide(self):
    self.root.withdraw()

def openFrame(self):
    self.hide()
    otherFrame = Tk.Toplevel()
    otherFrame.geometry("400x300")
    handler = lambda: self.CloseOtherFrame(otherFrame)
    btn = Tk.Button(otherFrame, text="Close", command=handler)
    btn.pack()


def CloseOtherFrame(self, otherFrame):
    otherFrame.destroy()
    self.show()
</code></pre>

<p><strong>Hope this helps.<br>
If this works for you,please accept this answer.</strong></p>

python tkinter