# HG changeset patch # User drewp # Date 2002-07-07 15:11:48 # Node ID 2ae11dc56b381d8dd9cd40355df8033980c45471 # Parent cfb00e8cc0ea31320e976675faa6ec64565b9862 did a nice job with Togglebutton diff --git a/light8/uihelpers.py b/light8/uihelpers.py --- a/light8/uihelpers.py +++ b/light8/uihelpers.py @@ -1,5 +1,5 @@ """all the tiny tk helper functions""" - +from __future__ import nested_scopes from Tkinter import * from types import StringType @@ -54,44 +54,56 @@ def colortotuple(anytkobj, colorname): return [v / 256 for v in rgb] class Togglebutton(Button): - """works like a single radiobutton, but it's a button so the label's on the button face, not to the side""" - def __init__(self,parent,**kw): - if kw['variable']: - self.variable = kw['variable'] - self.variable.trace('w',self.varchanged) - del kw['variable'] + """works like a single radiobutton, but it's a button so the + label's on the button face, not to the side. the optional command + callback is called on button set, not on unset. takes a variable + just like a checkbutton""" + def __init__(self,parent,variable=None,command=None,**kw): + + self.oldcommand = command + Button.__init__(self,parent,command=self.invoke,**kw) + + self._origbkg = self.cget('bg') + + self._variable = variable + if self._variable: + self._variable.trace('w',self._varchanged) + self._setstate(self._variable.get()) else: - self.variable=None - self.oldcommand = kw.get('command',None) - kw['command'] = self.invoke - Button.__init__(self,parent,**kw) + self._setstate(0) - self.origbkg = self.cget('bg') - - self.state=0 - if self.variable: - self.state = self.variable.get() - - self.setstate(self.state) + self.bind("",self.invoke) + self.bind("<1>",self.invoke) + self.bind("",self.invoke) - self.bind("",lambda ev: self.setstate) - self.bind("",lambda ev: self.setstate) - - def varchanged(self,*args): - self.setstate(self.variable.get()) + def _varchanged(self,*args): + self._setstate(self._variable.get()) - def invoke(self): - self.setstate(not self.state) + def invoke(self,*ev): + if self._variable: + self._variable.set(not self.state) + else: + self._setstate(not self.state) - if self.oldcommand: + if self.oldcommand and self.state: # call command only when state goes to 1 self.oldcommand() + return "break" - def setstate(self,newstate): - self.variable.set(newstate) + def _setstate(self,newstate): + self.state = newstate if newstate: # set - self.tk.call('tkButtonDown',self) - self.config(bg='green') + self.config(bg='red',relief='sunken') else: # unset - self.tk.call('tkButtonUp',self) - self.config(bg=self.origbkg) + self.config(bg=self._origbkg,relief='raised') return "break" + +if __name__=='__main__': + root=Tk() + root.tk_focusFollowsMouse() + iv=IntVar() + def cb(): + print "cb!" + t = Togglebutton(root,text="testbutton",command=cb,variable=iv) + t.pack() + Entry(root,textvariable=iv).pack() + root.mainloop()