2

I've found an example of opening and closing windows in Tkinter, which I'm going to edit to fit my needs. The root window appears, you click the button, another Toplevel window appears while the root window disappears. When you close the second window the root should reappear. I've used .update() and .deiconify() but they've not worked.

Code is as below:

import Tkinter as Tk

########################################################################
class OtherFrame(Tk.Toplevel):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, original):
        """Constructor"""
        self.original_frame = original
        Tk.Toplevel.__init__(self)
        self.geometry("400x300")
        self.title("otherFrame")

        btn = Tk.Button(self, text="Close", command=self.onClose)
        btn.pack()

    #----------------------------------------------------------------------
    def onClose(self):
        """"""
        self.destroy()
        self.original_frame.show()

########################################################################
class MyApp(object):
    """"""

    #----------------------------------------------------------------------
    def __init__(self, parent):
        """Constructor"""
        self.root = parent
        self.root.title("Main frame")
        self.frame = Tk.Frame(parent)
        self.frame.pack()

        btn = Tk.Button(self.frame, text="Open Frame", command=self.openFrame)
        btn.pack()

    #----------------------------------------------------------------------
    def hide(self):
        """"""
        self.root.withdraw()

    #----------------------------------------------------------------------
    def openFrame(self):
        """"""
        self.hide()
        subFrame = OtherFrame(self)

    #----------------------------------------------------------------------
    def show(self):
        """"""
        self.root.update()
        self.root.deiconify()

#----------------------------------------------------------------------
if __name__ == "__main__":
    root = Tk.Tk()
    root.geometry("800x600")
    app = MyApp(root)
    root.mainloop()
  • 1
    On my computer, your program does work perfectly fine. – Henry Zhu Jan 21 '15 at 08:18
  • @Henry Sorry, I've just realised, on my computer the second window opens, but then gets pushed behind any other window open at the time. So you have to minimise to the desktop to see it... Is this normal for Tkinter? – ashleycollinge Jan 21 '15 at 08:19
  • This sounds like different behavior than the results on my computer. The second window is not pushed behind. I'm guessing that it renders slightly differently on various computers. – Henry Zhu Jan 21 '15 at 08:24
  • Win 7, Py 2.7 here, works fine also. To force your root window to come back on top on any system, you could try [using the `-topmost` attribute](http://stackoverflow.com/a/6795115/3714930) – fhdrsdg Jan 21 '15 at 09:36

0 Answers0