Can this Tkinter script be simplified?
Greg Wilson
gvwilson at nevex.com
Sun Sep 5 09:51:33 EDT 1999
Hello. The enclosed script does what I want it to do, but looks overly
complex
to me --- I'd be grateful if someone could tell me whether/how it could be
simplified. In particular, can the IntVar() be initialized when it's
created, and
can actions be bound to state changes in active variables?
Thanks,
Greg
------------8<-----------------
Question = """
Create a GUI that displays the number of times a button has been
clicked, with a checkbox that controls whether the button can be
clicked.
"""
from Tkinter import *
class Counter(Frame):
def __init__(self, parent=None):
Frame.__init__(self); self.pack()
self.counter = 0
self.chkVar = IntVar()
self.chkVar.set(1)
self.check = Checkbutton(self, text="enable",
variable=self.chkVar,
command=self.setState)
self.check.pack(side=LEFT)
self.button = \
Button(self, text="click",
command=self.incr, state=NORMAL)
self.button.pack(side=LEFT)
self.caption = Label(self, text=('%d' % self.counter))
self.caption.pack(side=LEFT)
def setState(self):
if self.chkVar.get():
self.button.configure(state=NORMAL)
else:
self.button.configure(state=DISABLED)
def incr(self):
self.counter = self.counter + 1
self.caption.config(text=('%d' % self.counter))
root = Tk()
roller = Counter(root)
root.mainloop()
More information about the Python-list
mailing list