How to close "Tk" Window without closing whole GUI

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Daniel M
    New Member
    • Feb 2012
    • 7

    How to close "Tk" Window without closing whole GUI

    Hi, I am writing a program, in which everything interesting happens in a Toplevel window that I have made. However, whenever I run the program, I get both the standard Tk window and the Toplevel window that I made. How do I get rid of the first Tk window, without closing out of the program completely?

    Thanks

    Code:
    #Merthe's Ellipse Adjustment Program February 2012
    
    import numpy
    import scipy
    
    from Tkinter import *
    import tkFileDialog as tkf
    
    filenames = {}
    
    class App:
        def __init__(self, master):
    
            top = Toplevel()
            top.title("Adjust Ellipse")
    
            self.top = top
                
            self.L0 = Label(top, text = "Initial Data:").grid(row=0,column=0, sticky=E)
            self.L1 = Label(top, text = "After Change #1:").grid(row=1,column=0, sticky=E)
            self.L2 = Label(top, text = "After Change #2:").grid(row=2,column=0, sticky=E)
            self.L3 = Label(top, text = "Current State:").grid(row=3,column=0, sticky=E)
    
            self.Name0 = Entry(top, state = "readonly", width=40).grid(row=0,column=1, sticky=W,columnspan=3)
            self.Name1 = Entry(top, state = "readonly", width=40).grid(row=1,column=1, sticky=W,columnspan=3)
            self.Name2 = Entry(top, state = "readonly", width=40).grid(row=2,column=1, sticky=W,columnspan=3)
            self.Name3 = Entry(top, state = "readonly", width=40).grid(row=3,column=1, sticky=W,columnspan=3)
    
            self.Butt0 = Button(top, text = "Select", command=self.openfile0).grid(row=0,column=4)
            self.Butt1 = Button(top, text = "Select", command=self.openfile1).grid(row=1,column=4)
            self.Butt2 = Button(top, text = "Select", command=self.openfile2).grid(row=2,column=4)
            self.Butt3 = Button(top, text = "Select", command=self.openfile3).grid(row=3,column=4)
    
        def openfile0(self):
            tkf.askopenfile()
    
        def openfile1(self):
            tkf.askopenfile()
    
        def openfile2(self):
            tkf.askopenfile()
    
        def openfile3(self):
            tkf.askopenfile()
    
    root = Tk()
    
    app = App(root)
    
    root.mainloop()
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    master.withdraw () hides a top level window.

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      This will hide the root window and return to it if the user chooses to close the TopLevel window:
      Code:
      class App:
          def __init__(self, master=None):
              self.master = master
              master.state("withdraw")
              top = Toplevel()
              top.protocol("WM_DELETE_WINDOW", self.exit)
              top.title("Adjust Ellipse")
              self.top = top
      Method exit:
      Code:
          def exit(self):
              self.master.deiconify()
              self.top.destroy()

      Comment

      • Daniel M
        New Member
        • Feb 2012
        • 7

        #4
        Big thanks bvdet. This is the part that I ended up using:

        Code:
        top = Toplevel()
        top.title("Adjust Ellipse")
        top.protocol("WM_DELETE_WINDOW", self.exit)
        
        self.master = master
        master.withdraw()
        Along with the method:

        Code:
        def exit(self):
            self.master.deiconify()
            self.master.destroy() #Exit all when 'top' is closed
        This does exactly what I wanted. Thanks again.

        Comment

        • bvdet
          Recognized Expert Specialist
          • Oct 2006
          • 2851

          #5
          You're welcome. I learned a couple of things myself.

          Comment

          • dwblas
            Recognized Expert Contributor
            • May 2008
            • 626

            #6
            The simple solution is to use "master" instead of creating a new Toplevel. Then you don't have to go through the additional gyrations to close the master GUI.
            Code:
            from Tkinter import *
            import tkFileDialog as tkf
             
            filenames = {}
             
            class App:
                 def __init__(self, top):
             
            #         top = Toplevel()
                     top.title("Adjust Ellipse")
             
                     self.top = top
             
                     self.L0 = Label(top, text = "Initial Data:").grid(row=0,column=0, sticky=E)
                     self.L1 = Label(top, text = "After Change #1:").grid(row=1,column=0, sticky=E)
                     self.L2 = Label(top, text = "After Change #2:").grid(row=2,column=0, sticky=E)
                     self.L3 = Label(top, text = "Current State:").grid(row=3,column=0, sticky=E)
             
                     self.Name0 = Entry(top, state = "readonly", width=40).grid(row=0,column=1, sticky=W,columnspan=3)
                     self.Name1 = Entry(top, state = "readonly", width=40).grid(row=1,column=1, sticky=W,columnspan=3)
                     self.Name2 = Entry(top, state = "readonly", width=40).grid(row=2,column=1, sticky=W,columnspan=3)
                     self.Name3 = Entry(top, state = "readonly", width=40).grid(row=3,column=1, sticky=W,columnspan=3)
             
                     self.Butt0 = Button(top, text = "Select", command=self.openfile0).grid(row=0,column=4)
                     self.Butt1 = Button(top, text = "Select", command=self.openfile1).grid(row=1,column=4)
                     self.Butt2 = Button(top, text = "Select", command=self.openfile2).grid(row=2,column=4)
                     self.Butt3 = Button(top, text = "Select", command=self.openfile3).grid(row=3,column=4)
             
                 def openfile0(self):
                     tkf.askopenfile()
             
                 def openfile1(self):
                     tkf.askopenfile()
             
                 def openfile2(self):
                     tkf.askopenfile()
             
                 def openfile3(self):
                     tkf.askopenfile()
             
            root = Tk()
             
            app = App(root)
             
            root.mainloop()

            Comment

            Working...