By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,097 Members | 1,580 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,097 IT Pros & Developers. It's quick & easy.

Appropriate way to quit Tkinter

P: n/a
I've just started playing with Python. Installed 2.5 on Windows XP
system. I'm working through some of the examples in Programming Python
3ed by Mark Lutz. Given the following example when the Quit All button
action is assigned to root.quit the windows aren't dismissed or
destroyed. The application appears to terminate, but the windows
remain. When the action is assigned to root.destroy, the windows are
closed. Questions:
1) Which way is preferred / correct?
2) Is something wrong with 2.5? (I haven't tried older versions of
python)
3) Is there something wrong with my installation?
################################################## ###########################
# popup three new window, with style
# destroy() kills one window, quit() kills all windows and app;
top-level
# windows have title, icon, iconify/deiconify and protocol for wm
events;
# there always is an app root window, whether by default or created as
an
# explicit Tk() object; all top-level windows are containers, but never

# packed/gridded; Toplevel is like frame, but new window, and can have
menu;
################################################## ###########################

from Tkinter import *
root = Tk() # explicit root

trees = [('The Larch!', 'light blue'),
('The Pine!', 'light green'),
('The Giant Redwood!', 'red')]

for (tree, color) in trees:
win = Toplevel(root)
# new window
win.title('Sing...')
# set border
win.protocol('WM_DELETE_WINDOW', lambda:0) # ignore close
win.iconbitmap('py-blue-trans-out.ico') # not red Tk

msg = Button(win, text=tree, command=win.destroy) # kills one win
msg.pack(expand=YES, fill=BOTH)
msg.config(padx=10, pady=10, bd=10, relief=RAISED)
msg.config(bg='black', fg=color, font=('times', 30, 'bold italic'))

root.title('Lumberjack demo')
Label(root, text='Main window', width=30).pack()
#Button(root, text='Quit All', command=root.quit).pack() # kills
all app
Button(root, text='Quit All', command=root.destroy).pack() # kills all
app
root.mainloop()

Oct 12 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
mzdude wrote:
I've just started playing with Python. Installed 2.5 on Windows XP
system. I'm working through some of the examples in Programming Python
3ed by Mark Lutz. Given the following example when the Quit All button
action is assigned to root.quit the windows aren't dismissed or
destroyed. The application appears to terminate, but the windows
remain. When the action is assigned to root.destroy, the windows are
closed. Questions:
1) Which way is preferred / correct?
quit() causes the mainloop() call to return. if calling mainloop() is
the last thing you do in your program, the program terminates.

destroy() destroys the given window. if that's the last window, the
mainloop() is terminated as well.
2) Is something wrong with 2.5? (I haven't tried older versions of
python)
not that I can see.
3) Is there something wrong with my installation?
works for me. are you perhaps running this under some kind of IDE that
keeps the process running even after the program has terminated?

</F>

Oct 12 '06 #2

P: n/a

Fredrik Lundh wrote:
[snip]
works for me. are you perhaps running this under some kind of IDE that
keeps the process running even after the program has terminated?
It works the same way if I run from IDLE or from the DOS command prompt.

Oct 13 '06 #3

P: n/a
On 2006-10-13, mzdude <js****@cox.netwrote:
>
Fredrik Lundh wrote:
[snip]
>works for me. are you perhaps running this under some kind of
IDE that keeps the process running even after the program has
terminated?

It works the same way if I run from IDLE or from the DOS
command prompt.
I had some fun trying to run Tkinter from from the Python
embedded in Vim. My advice: Do not do that.

--
Neil Cerutti
The majority of time, it seems to be one thing or the other.
--Ron Mercer
Oct 13 '06 #4

P: n/a
mzdude wrote:
>works for me. are you perhaps running this under some kind of IDE that
keeps the process running even after the program has terminated?
It works the same way if I run from IDLE or from the DOS command prompt.
I find very hard to believe that a Python interpreter run from the DOS
command prompt wouldn't terminate when it reaches the end of the main
program.

what Python distribution are you using?

</F>

Oct 13 '06 #5

P: n/a

Fredrik Lundh wrote:
mzdude wrote:
works for me. are you perhaps running this under some kind of IDE that
keeps the process running even after the program has terminated?
It works the same way if I run from IDLE or from the DOS command prompt.

I find very hard to believe that a Python interpreter run from the DOS
command prompt wouldn't terminate when it reaches the end of the main
program.

what Python distribution are you using?

</F>
Not sure I understand the question. I downloaded python-2.5.msi from
python.org/download.
>From the IDLE
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) [MSC v.1310 32 bit
(Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

Oct 13 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.