Tkinter programming problem
Andrew Gregory
andrew.gregory at npl.co.uk
Thu Aug 7 05:18:53 EDT 2003
"Mark Daley" <mark at diversiform.com> wrote in message news:<mailman.1060190417.25846.python-list at python.org>...
> I have problems with sys.exit() when I'm running under IDLE, since it seems
> to cause ALL mainloops to exit, killing IDLE. I can't really give a whole
> lot of detail on this, since I tend to use the command line to launch my
> GUIs, anyway.
>
Using tkinter seems to require a bit of lateral thinking at times (and
I think I must have failed the test!!!). I suppose the difficulty lies
in getting the right structure (generally true of OOP). Anyway, I did
fix the problem and working code is given below. In effect, root does
have to be destroyed.
Andrew.
# Demonstration TK interface Windows application
# Runs ok from within IDLE
#
# A. Gregory, 4th August 2003
#
from Tkinter import *
class CommonStuff: # to get common access to variables and functions
def __init__(self, master):
self.master=master
self.frame = Frame(master)
self.frame.bind("<Destroy>",self.CleanUp)
def say_hi(self):
print "Hello all"
def myquit(self):
self.master.destroy()
def CleanUp(self, event):
print "Cleaning up after Tk windows closed"
class MyWidgets(Frame, CommonStuff):
def __init__(self, CS):
Frame.__init__(self, CS.frame)
quitbutton = Button(self, text='Quit', fg='red',
command=CS.myquit)
quitbutton.pack(side=LEFT)
self.hi_there = Button(self, text="Hello", command=CS.say_hi)
self.hi_there.pack(side=LEFT)
class Application:
def __init__(self, CS):
displayedwidget=MyWidgets(CS)
displayedwidget.grid(row=0, column=0)
CS.frame.grid(row=0, column=0)
CS.frame.columnconfigure(0)
CS.frame.update()
root = Tk()
CS = CommonStuff(root)
mainWin = Application(CS)
# If running from IDLE in Python 2.2 or earlier must use
root.wait_window(CS.frame)
# Otherwise can use
# root.mainloop()
More information about the Python-list
mailing list