Open unique secondary window with Tkinter
<p>I'm having a trouble when i open a secondary window. Now I'm just creating a toplevel window with a button and I need to open the same secondary window If i click the button (not generate a new instance).</p>

<p>Which is the better way to generate single secondary window and not generating a new window instance?</p>

<p>I leave the code that I'm actually working on:</p>

<pre><code>import tkinter

class LogWindow():
    def __init__(self, parent):
        self.parent = parent
        self.frame = tkinter.Frame(self.parent)

class MainWindow(tkinter.Frame):
    def __init__(self, parent):
        tkinter.Frame.__init__(self, parent)
        self.parent = parent

        frame = tkinter.Frame(self, borderwidth=1)
        frame.pack(fill=tkinter.BOTH, expand=True, padx=5, pady=5)

        self.LogButton = tkinter.Button(frame, text="Log Viewer", command= self.openLogWindow)
        self.LogButton.grid(sticky=tkinter.E+tkinter.W)

        self.pack(fill=tkinter.BOTH,expand=True)

    def openLogWindow(self):
        self.logWindow = tkinter.Toplevel(self.parent)
        self.app = LogWindow(self.logWindow)

def main():
    global app, stopRead
    root = tkinter.Tk()
    root.geometry("300x300")
    app = MainWindow(root)
    root.mainloop()


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

<p>Maybe i need to have a single instance of a Toplevel class and call show and close to show or hide the secondary window.</p>

<p>Finally after some tests I've found how to solve that, thanks to the @furas response and some investigation about the Tkinter events with the protocol function.</p>

<p>I've got that working with:</p>

<pre><code>import tkinter

logWindowExists = False

class LogWindow():
    def __init__(self, parent):
        global logWindowExists, root
        logWindowExists = True

        self.parent = parent
        self.frame = tkinter.Frame(self.parent)

    def on_closing(self):
        global logWindowExists
        logWindowExists = False
        self.parent.destroy()

class MainWindow(tkinter.Frame):
    def __init__(self, parent):
        tkinter.Frame.__init__(self, parent)
        self.parent = parent

        frame = tkinter.Frame(self, borderwidth=1)
        frame.pack(fill=tkinter.BOTH, expand=True, padx=5, pady=5)

        self.LogButton = tkinter.Button(frame, text="Log Viewer", command= self.openLogWindow)
        self.LogButton.grid(sticky=tkinter.E+tkinter.W)

        self.pack(fill=tkinter.BOTH,expand=True)

    def openLogWindow(self):
        if not logWindowExists:
            self.logWindow = tkinter.Toplevel(self.parent)
            self.app = LogWindow(self.logWindow)
            self.logWindow.protocol("WM_DELETE_WINDOW", self.app.on_closing)
        else:
            self.logWindow.deiconify()

def main():
    global app, stopRead, root
    root = tkinter.Tk()
    root.geometry("300x300")
    app = MainWindow(root)
    root.mainloop()


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

<p>Using a boolean to know if the window exists or not i can handle when the window it's opened or not and just show the existing window or creating a new one.</p>

python tkinter