Exiting Tkinter when using IDLE

Kurt B. Kaiser kbk at shore.net
Mon Mar 15 02:03:28 CET 2004


Jason Harper <JasonHarper at pobox.com> writes:

> In a non-subprocess IDLE, you don't have to hit a breakpoint to be able
> to examine a running Tkinter program.  Both the shell window and the
> user program are fully responsive at the same time.  For example, you
> could type a command to change the appearance of a button, and
> IMMEDIATELY try out the button with its new appearance, since the
> mainloop is still running.  This ability is lost in a subprocess IDLE.

OK, another good reason to retain the non-subprocess capability.  The
user Tkinter code is mixed in with IDLE's Tkinkter code, but there is
a practical use for experimenting with layouts.  To get this to work
cleanly, you eliminate the mainloop() call and use destroy():

===================
IDLE without subprocess (-n switch):

from Tkinter import *

class App:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        self.button = Button(frame, text="QUIT", fg="red",
                             command=self.quit)
        self.button.pack(side=LEFT)

        self.hi_there = Button(frame, text="Hello",
                               command=self.say_hi)
        self.hi_there.pack(side=LEFT)

    def say_hi(self):
        print "hi there, everyone!"

    def quit(self):
        print "quitting"
        a = raw_input("prompt: ")
        print a
        root.destroy()

root = Tk()
app = App(root)

=======================

And, as you say, you can do amusing things like

>>> app.button["text"]
'QUIT'
>>> app.button["text"] = "REALLY QUIT"

-- 
KBK



More information about the Python-list mailing list