473,624 Members | 2,453 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

TKinter Destroy Question

Rob
My first GUI so be gentle...

When I start my program I call a class that runs the initial window. While
in this class if a certain button is pressed it calls a function outside the
class. This function then initially calls another function to
"root.destroy() ". Basically I want the current window gone so the function I
just called can open it's own window. The problem I'm stuck with is that
once this function is done and I need to close the new window to go to the
next window i again call the function which performs the "root.destroy() ".
When I try to call it a second time it tells me:

TclError: can't invoke "destroy" command: application has been destroyed

How can it already be destroyed if I opened a new window in my function?
Should I slip in somewhere a new "root.mainloop( )" statment? It seems when I
try this I get some weird results (Every command after the new
root.mainloop() nothing happens, no buttons drawn, nothing). Is there an
easier way to clear windows than to use the root.destroy? I've basically run
into a nice brick wall...

Thanks for the help!
Rob
Jul 18 '05 #1
2 26230
Rob wrote:
My first GUI so be gentle...

When I start my program I call a class that runs the initial window. While
in this class if a certain button is pressed it calls a function outside the
class. This function then initially calls another function to
"root.destroy() ". Basically I want the current window gone so the function I
just called can open it's own window. The problem I'm stuck with is that
once this function is done and I need to close the new window to go to the
next window i again call the function which performs the "root.destroy() ".
When I try to call it a second time it tells me:

TclError: can't invoke "destroy" command: application has been destroyed

How can it already be destroyed if I opened a new window in my function?
Should I slip in somewhere a new "root.mainloop( )" statment? It seems when I
try this I get some weird results (Every command after the new
root.mainloop() nothing happens, no buttons drawn, nothing). Is there an
easier way to clear windows than to use the root.destroy? I've basically run
into a nice brick wall...


Destroying the root window in a Tkinter application actually means destroying
the whole application, not only the root window. The root window is the main
window for the application; it is the first to pop up and must be the last to
go. If I understand correctly, your application does not have an actual main
window: if a second window is opened from the initial window, closing the
initial window should not quit the application. Am I right?

If I am, the way to do that with Tkinter (or tcl/tk) is to create a fake root
window and hide it. This window will only be used to quit the application when
the last window is closed:

----------------------------------------------------------
from Tkinter import *

## Create main window
root = Tk()
## Hide it
root.withdraw()

## List of windows currently opened
windows = []

## Function to create a new window
def newWindow():
## Create window
wdw = Toplevel()
## Button in window to create another new window
Button(wdw, text='New window', command=newWind ow).pack()
## Remember window in list of opened ones
windows.append( wdw)
## When the user asks to close the window, call function closeWindow on it
wdw.protocol('W M_DELETE_WINDOW ', lambda wdw=wdw: closeWindow(wdw ))

## Function called when a window's close button is clicked
def closeWindow(wdw ):
## Actually close the window
wdw.destroy()
## Remove window from list of opened ones
if wdw in windows: windows.remove( wdw)
## If no more opened windows, quit application
if not windows: root.quit()

## Create first window in application
newWindow()

## Run appliction
root.mainloop()
----------------------------------------------------------

Run the script and create a few windows. You'll see that you can close any
window you want in any order: the application quits only when the last window is
closed.

HTH
--
- Eric Brunel <eric dot brunel at pragmadev dot com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

Jul 18 '05 #2
On Thu, 2003-11-20 at 04:59, Rob wrote:
My first GUI so be gentle...

When I start my program I call a class that runs the initial window. While
in this class if a certain button is pressed it calls a function outside the
class. This function then initially calls another function to
"root.destroy() ". Basically I want the current window gone so the function I
just called can open it's own window. The problem I'm stuck with is that
once this function is done and I need to close the new window to go to the
next window i again call the function which performs the "root.destroy() ".
When I try to call it a second time it tells me:

TclError: can't invoke "destroy" command: application has been destroyed

When you destroy a window it's gone! so can't be destroyed again.....
Without an example of what you are doing it's difficult to help but.
You could call withdraw() method on root this will unmap it from the
screen (but not destroy it) However why are you calling destroy on root
twice? Perhaps you mean to call destroy on the old window when you
create a new one (like a Wizard)
e.g (untested...)

root=Tk()

def createNext():
nextWindow = Toplevel()
nextWindow.titl e("Next Window")
b = Button(root, text="Create Next Window", command=createN ext)
b.pack()
root.withdraw()
root.title("The Main Root Window")
b = Button(root, text="Create Next Window", command=createN ext)
b.pack()
root.mainloop()
The problem here is you are not getting rid of the previous window when
the Next button is pressed just root!
Perhaps a better example would be:-
class MyApplication(T k):
def __init__(self):
Tk.__init__(sel f)
self.title("Mai n Application Window")
self.prevWindow = self
self.windowCoun t = 1
b = Button(self, text="Next", command=self.ne xtWindow)
b.pack()

def nextWindow(self ):
window = Toplevel()
self.windowCoun t = self.windowCoun t + 1
window.title("N ext Window %d" %self.windowCou nt)
b = Button(window, text="Next", command=self.ne xtWindow)
b.pack()
self.prevWindow .withdraw()
self.prevWindow = window


app = MyApplication()
app.mainloop()
However this will never finish! to close the whole application the
app.quit() method must be called..... or if it's inside the class
definition self.quit()

Basically explain what you want with some example code and we can help

Regards
Martin
--
Martin Franklin <mf********@gat wick.westerngec o.slb.com>
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
7196
by: Andrew Gregory | last post by:
Could someone help me out with these few lines of code: I would like to know why the Quit button in this application removes the buttons and causes "Quitting" to be printed, but does not close the outer frame. Andrew. # Demonstration TK interface Windows application # Runs ok from within IDLE
3
2330
by: Mickel Grönroos | last post by:
Hi everybody, I'm using QuickTimeTcl (3.1) to be able to play movie files in my Tkinter application (Python 2.3.2) on Windows 2000. I was planning to write a simple wrapper class, QuickTimeMovie, that would wrap up the QuickTimeTcl Tcl extension as a Python class. All seems to work pretty fine until the Tkinter application is closed, when the Python interpreter crashes with an error of the following kind: The instruction at...
6
17979
by: max(01)* | last post by:
hi people. when i create a widget, such as a toplevel window, and then i destroy it, how can i test that it has been destroyed? the problem is that even after it has been destroyed, the instance still exists and has a tkinter name, so testing for None is not feasible: >>> import Tkinter >>> fin = None >>> fin1 = Tkinter.Toplevel()
5
2993
by: max(01)* | last post by:
hello. i wrote a very simple tkinter demo program that uses menus, buttons, labels, entries, frames and secondary toplevels. it is a python version of a java program made by a colleague. the user can create ("Scrivi") a record with his second name, first name and date of birth, save ("Salva") the record to a file or read in ("Leggi") a previously created file. "Annulla" is to cancel. "Chiudi" is
0
2344
by: Stewart Midwinter | last post by:
I have a Tkinter app running on cygwin. It includes a Test menu item that does nothing more than fetch a directory listing and display it in a Toplevel window (I'd use a tkMessageBox showinfo widget, but for some reason the text is invisible on cygwin). After I close the Toplevel widget, all of the menus in my app behave as though they have no contents to them, i..e I can press on the File menu button, and see it depress, but the Exit...
1
3592
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that I am hoping someone knows what I am doing wrong: 1) Pressing the Settings.. Button multiple times, brings up many instances of the Settings Panel. I just want it to bring up one. Is there an easy way to do that?
3
2977
by: joshdw4 | last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes, it's that pesky root window and I have tried withdraw to no avail. I'm assuming this is because of the methods I'm using. I guess my question is two-fold. 1) How do I get rid of that window? 2) Any comments in general? I am just learning python (and coding with classes), so I'm sure there are things I should pound into my head before I learn bad habits. Here's the...
3
3910
by: J-Burns | last post by:
Hello. Im a bit new to using Tkinter and im not a real pro in programming itself... :P. Need some help here. Problem 1: How do I make something appear on 2 separate windows using Tkinter? By this I mean that the format would be something like this: You have Page1 : This has 2-3 buttons on it. Clicking on each button opens up a new window respectively having
0
3980
by: nicstel | last post by:
Hello. My script run fine within python but not in my program(SDS/2 wich is a software like Autocad). The problem is I got an error when the time comes to read the line14 to 19. (Source code come from Tkinter.py) class Tk(Misc, Wm): """Toplevel widget of Tk which represents mostly the main window of an appliation. It has an associated Tcl interpreter.""" _w = '.' def __init__(self, screenName=None, baseName=None,...
0
8175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
7168
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2610
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.