Tkinter new windows won't close properly
<p>I want my GUI to have a 'new window' option that will be just the same as the first one.</p>

<p>The problem is that it also has an exit(quit) button that won't work as it should - whenever I open the new window and then press the button, in the first click nothing happens and in the second one it closes both windows (if 3 windows are open so it'll close everything in the third click and so on).</p>

<p>This the relevant code:</p>

<pre><code>from Tkinter import *
from ttk import *


class Application(Tk):
    def __init__(self):
        self.root = Tk()
        self.root.geometry("250x150")
        self.app = Frame(self.root)
        self.app.grid()
        self.create_menu()
        self.create_widgets()
        self.root.mainloop()

    def create_menu(self):
        menu = Menu(self.root)
        self.root.config(menu=menu)
        sub_menu = Menu(menu)
        menu.add_cascade(label="File", menu=sub_menu)
        sub_menu.add_command(label="New", command=self.__init__)
        sub_menu.add_command(label="Run", command=self.enter)
        sub_menu.add_separator()
        sub_menu.add_command(label="Exit", command=self.app.quit)
</code></pre>

<p>I also tried to change:</p>

<pre><code>sub_menu.add_command(label="New", command=self.__init__)
</code></pre>

<p>to:</p>

<pre><code>sub_menu.add_command(label="New", command=self.new window)
</code></pre>

<p>Where:</p>

<pre><code>def new_window(self):
    class App(Application):
        Application.__init__(self)
</code></pre>

<p>Both do the same thing.</p>

<p>How can I fix it?</p>

<p>In a Tkinter-Application there may only be one Tk-object. If the object is destroyed or destroyed by the garbage collector, Tkinter will be disabled. Use Toplevel for other other windows instead.</p>

<p>Try this instead:</p>

<pre><code>from Tkinter import *
from ttk import *


class Application(object):
    def __init__(self, master):
        self.root = master
        self.root.geometry("250x150")
        self.app = Frame(self.root)
        self.app.grid()
        self.create_menu()
        self.create_widgets()

    def create_menu(self):
        menu = Menu(self.root)
        self.root.config(menu=menu)
        sub_menu = Menu(menu)
        menu.add_cascade(label="File", menu=sub_menu)
        sub_menu.add_command(label="New", command=self.new)
        sub_menu.add_command(label="Run", command=self.enter)
        sub_menu.add_separator()
        sub_menu.add_command(label="Exit", command=self.quit)

    def new(self):
        window = Toplevel(tk)
        return Application(window)

    def quit(self):
        tk.destroy()

tk = Tk()
Application(tk)
tk.mainloop()
</code></pre>

python python-2.7 tkinter