473,385 Members | 1,397 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
5 15733
bvdet
2,851 Expert Mod 2GB
master.withdraw() hides a top level window.
Feb 16 '12 #2
bvdet
2,851 Expert Mod 2GB
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
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
2,851 Expert Mod 2GB
You're welcome. I learned a couple of things myself.
Feb 16 '12 #5
dwblas
626 Expert 512MB
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

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: John Benson | last post by:
Hi, I'm using Tkinter and would like to know how to replace the red "Tk" logo in the left corner of the title bar of the root window with some other icon. Another question for curiosity's sake:...
1
by: george | last post by:
Greetings to all experts. I'm running Python 2.3/Pythonwin 1.63 on Windows 98. I would like to create a window with Tkinter, that is always on top of the screen. In Tkinter.py, Line 1375 f.,...
5
by: engsolnorm | last post by:
I'm playing with a sudoku GUI...just to learn more about python. I've made 81 'cells'...actually small canvases Part of my scheme to write the cells (all 81 of them in the gui) to a file (using...
21
by: comp.lang.tcl | last post by:
set php {<? print_r("Hello World"); ?>} puts $php; # PRINTS OUT <? print_r("Hello World"); ?> puts When I try this within TCL I get the following error:
1
by: Joe Peterson | last post by:
I've been doing a lot of searching on the topic of one of Python's more disturbing issues (at least to me): the fact that if a __del__ finalizer is defined and a cyclic (circular) reference is...
0
by: Adam | last post by:
Hello, I have a small app I am creating to crawl a directory and check that if it is moved to another a location it's path will not break a character limit. Usually the Windows path limit. ...
3
by: Miki | last post by:
Hello, Tk.lift doesn't seem to work on OSX (Python 2.5.1). The below starts OK, but the window is the behind all other windows. from Tkinter import * root = Tk() Button(root, text="OK",...
1
by: Pierre Dagenais | last post by:
from Tkinter import * win = Tk() If I type those two lines at the command prompt (in WindowsXP) I get a new window on my screen. Yet if I copy those lines in a file called test.py and then...
2
by: merrygreat | last post by:
#!/usr/bin/perl use Tk; my $window = MainWindow->new; $window->title("Host Access Report"); ($lab = $window->Label(-text => "Results go here"))->pack; $window->Entry(-textvariable => \$nnn...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.