Appropriate way to quit Tkinter

M

mzdude

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()
 
F

Fredrik Lundh

mzdude said:
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>
 
M

mzdude

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.
 
N

Neil Cerutti

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.
 
F

Fredrik Lundh

mzdude said:
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>
 
M

mzdude

Fredrik said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top