364,111 Members | 2009 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

How to close "Tk" Window without closing whole GUI

Daniel M
P: 7
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

Expand|Select|Wrap|Line Numbers
  1.  
  2. #Merthe's Ellipse Adjustment Program February 2012
  3.  
  4. import numpy
  5. import scipy
  6.  
  7. from Tkinter import *
  8. import tkFileDialog as tkf
  9.  
  10. filenames = {}
  11.  
  12. class App:
  13.     def __init__(self, master):
  14.  
  15.         top = Toplevel()
  16.         top.title("Adjust Ellipse")
  17.  
  18.         self.top = top
  19.  
  20.         self.L0 = Label(top, text = "Initial Data:").grid(row=0,column=0, sticky=E)
  21.         self.L1 = Label(top, text = "After Change #1:").grid(row=1,column=0, sticky=E)
  22.         self.L2 = Label(top, text = "After Change #2:").grid(row=2,column=0, sticky=E)
  23.         self.L3 = Label(top, text = "Current State:").grid(row=3,column=0, sticky=E)
  24.  
  25.         self.Name0 = Entry(top, state = "readonly", width=40).grid(row=0,column=1, sticky=W,columnspan=3)
  26.         self.Name1 = Entry(top, state = "readonly", width=40).grid(row=1,column=1, sticky=W,columnspan=3)
  27.         self.Name2 = Entry(top, state = "readonly", width=40).grid(row=2,column=1, sticky=W,columnspan=3)
  28.         self.Name3 = Entry(top, state = "readonly", width=40).grid(row=3,column=1, sticky=W,columnspan=3)
  29.  
  30.         self.Butt0 = Button(top, text = "Select", command=self.openfile0).grid(row=0,column=4)
  31.         self.Butt1 = Button(top, text = "Select", command=self.openfile1).grid(row=1,column=4)
  32.         self.Butt2 = Button(top, text = "Select", command=self.openfile2).grid(row=2,column=4)
  33.         self.Butt3 = Button(top, text = "Select", command=self.openfile3).grid(row=3,column=4)
  34.  
  35.     def openfile0(self):
  36.         tkf.askopenfile()
  37.  
  38.     def openfile1(self):
  39.         tkf.askopenfile()
  40.  
  41.     def openfile2(self):
  42.         tkf.askopenfile()
  43.  
  44.     def openfile3(self):
  45.         tkf.askopenfile()
  46.  
  47. root = Tk()
  48.  
  49. app = App(root)
  50.  
  51. root.mainloop()
  52.  
  53.  
Feb 15 '12 #1
Share this Question
Share on Google+
5 Replies


bvdet
Expert Mod 2.5K+
P: 2,509
master.withdraw() hides a top level window.
Feb 16 '12 #2

bvdet
Expert Mod 2.5K+
P: 2,509
This will hide the root window and return to it if the user chooses to close the TopLevel window:
Expand|Select|Wrap|Line Numbers
  1. class App:
  2.     def __init__(self, master=None):
  3.         self.master = master
  4.         master.state("withdraw")
  5.         top = Toplevel()
  6.         top.protocol("WM_DELETE_WINDOW", self.exit)
  7.         top.title("Adjust Ellipse")
  8.         self.top = top
Method exit:
Expand|Select|Wrap|Line Numbers
  1.     def exit(self):
  2.         self.master.deiconify()
  3.         self.top.destroy()
Feb 16 '12 #3

Daniel M
P: 7
Big thanks bvdet. This is the part that I ended up using:

Expand|Select|Wrap|Line Numbers
  1. top = Toplevel()
  2. top.title("Adjust Ellipse")
  3. top.protocol("WM_DELETE_WINDOW", self.exit)
  4.  
  5. self.master = master
  6. master.withdraw()
  7.  
Along with the method:

Expand|Select|Wrap|Line Numbers
  1. def exit(self):
  2.     self.master.deiconify()
  3.     self.master.destroy() #Exit all when 'top' is closed 
  4.  
This does exactly what I wanted. Thanks again.
Feb 16 '12 #4

bvdet
Expert Mod 2.5K+
P: 2,509
You're welcome. I learned a couple of things myself.
Feb 16 '12 #5

dwblas
Expert 100+
P: 335
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.
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. import tkFileDialog as tkf
  3.  
  4. filenames = {}
  5.  
  6. class App:
  7.      def __init__(self, top):
  8.  
  9. #         top = Toplevel()
  10.          top.title("Adjust Ellipse")
  11.  
  12.          self.top = top
  13.  
  14.          self.L0 = Label(top, text = "Initial Data:").grid(row=0,column=0, sticky=E)
  15.          self.L1 = Label(top, text = "After Change #1:").grid(row=1,column=0, sticky=E)
  16.          self.L2 = Label(top, text = "After Change #2:").grid(row=2,column=0, sticky=E)
  17.          self.L3 = Label(top, text = "Current State:").grid(row=3,column=0, sticky=E)
  18.  
  19.          self.Name0 = Entry(top, state = "readonly", width=40).grid(row=0,column=1, sticky=W,columnspan=3)
  20.          self.Name1 = Entry(top, state = "readonly", width=40).grid(row=1,column=1, sticky=W,columnspan=3)
  21.          self.Name2 = Entry(top, state = "readonly", width=40).grid(row=2,column=1, sticky=W,columnspan=3)
  22.          self.Name3 = Entry(top, state = "readonly", width=40).grid(row=3,column=1, sticky=W,columnspan=3)
  23.  
  24.          self.Butt0 = Button(top, text = "Select", command=self.openfile0).grid(row=0,column=4)
  25.          self.Butt1 = Button(top, text = "Select", command=self.openfile1).grid(row=1,column=4)
  26.          self.Butt2 = Button(top, text = "Select", command=self.openfile2).grid(row=2,column=4)
  27.          self.Butt3 = Button(top, text = "Select", command=self.openfile3).grid(row=3,column=4)
  28.  
  29.      def openfile0(self):
  30.          tkf.askopenfile()
  31.  
  32.      def openfile1(self):
  33.          tkf.askopenfile()
  34.  
  35.      def openfile2(self):
  36.          tkf.askopenfile()
  37.  
  38.      def openfile3(self):
  39.          tkf.askopenfile()
  40.  
  41. root = Tk()
  42.  
  43. app = App(root)
  44.  
  45. root.mainloop() 
Feb 17 '12 #6

Post your reply

Help answer this question



Didn't find the answer to your Python question?

You can also browse similar questions: Python tkinter