Changeset - 2ae11dc56b38
[Not reviewed]
default
0 1 0
drewp - 22 years ago 2002-07-07 15:11:48

did a nice job with Togglebutton
1 file changed with 44 insertions and 32 deletions:
0 comments (0 inline, 0 general)
light8/uihelpers.py
Show inline comments
 
"""all the tiny tk helper functions"""
 

	
 
from __future__ import nested_scopes
 
from Tkinter import *
 
from types import StringType
 

	
 
def make_frame(parent):
 
    f = Frame(parent, bd=0)
 
    f.pack(side='left')
 
@@ -51,47 +51,59 @@ def colorfade(low, high, percent):
 
def colortotuple(anytkobj, colorname):
 
    'pass any tk object and a color name, like "yellow"'
 
    rgb = anytkobj.winfo_rgb(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.bind("<Return>",self.invoke)
 
        self.bind("<1>",self.invoke)
 
        self.bind("<space>",self.invoke)
 

	
 
        self.state=0
 
        if self.variable:
 
            self.state = self.variable.get()
 

	
 
        self.setstate(self.state)
 
    def _varchanged(self,*args):
 
        self._setstate(self._variable.get())
 

	
 
        self.bind("<Enter>",lambda ev: self.setstate)
 
        self.bind("<Leave>",lambda ev: self.setstate)
 
    def invoke(self,*ev):
 
        if self._variable:
 
            self._variable.set(not self.state)
 
        else:
 
            self._setstate(not self.state)
 

	
 
    def varchanged(self,*args):
 
        self.setstate(self.variable.get())
 
        
 
    def invoke(self):
 
        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()
0 comments (0 inline, 0 general)