comparison light8/uihelpers.py @ 47:2ae11dc56b38

did a nice job with Togglebutton
author drewp
date Sun, 07 Jul 2002 15:11:48 +0000
parents 411de8b46aef
children 71489bb71528
comparison
equal deleted inserted replaced
46:cfb00e8cc0ea 47:2ae11dc56b38
1 """all the tiny tk helper functions""" 1 """all the tiny tk helper functions"""
2 2 from __future__ import nested_scopes
3 from Tkinter import * 3 from Tkinter import *
4 from types import StringType 4 from types import StringType
5 5
6 def make_frame(parent): 6 def make_frame(parent):
7 f = Frame(parent, bd=0) 7 f = Frame(parent, bd=0)
52 'pass any tk object and a color name, like "yellow"' 52 'pass any tk object and a color name, like "yellow"'
53 rgb = anytkobj.winfo_rgb(colorname) 53 rgb = anytkobj.winfo_rgb(colorname)
54 return [v / 256 for v in rgb] 54 return [v / 256 for v in rgb]
55 55
56 class Togglebutton(Button): 56 class Togglebutton(Button):
57 """works like a single radiobutton, but it's a button so the label's on the button face, not to the side""" 57 """works like a single radiobutton, but it's a button so the
58 def __init__(self,parent,**kw): 58 label's on the button face, not to the side. the optional command
59 if kw['variable']: 59 callback is called on button set, not on unset. takes a variable
60 self.variable = kw['variable'] 60 just like a checkbutton"""
61 self.variable.trace('w',self.varchanged) 61 def __init__(self,parent,variable=None,command=None,**kw):
62 del kw['variable'] 62
63 self.oldcommand = command
64 Button.__init__(self,parent,command=self.invoke,**kw)
65
66 self._origbkg = self.cget('bg')
67
68 self._variable = variable
69 if self._variable:
70 self._variable.trace('w',self._varchanged)
71 self._setstate(self._variable.get())
63 else: 72 else:
64 self.variable=None 73 self._setstate(0)
65 self.oldcommand = kw.get('command',None)
66 kw['command'] = self.invoke
67 Button.__init__(self,parent,**kw)
68 74
69 self.origbkg = self.cget('bg') 75 self.bind("<Return>",self.invoke)
76 self.bind("<1>",self.invoke)
77 self.bind("<space>",self.invoke)
70 78
71 self.state=0 79 def _varchanged(self,*args):
72 if self.variable: 80 self._setstate(self._variable.get())
73 self.state = self.variable.get() 81
82 def invoke(self,*ev):
83 if self._variable:
84 self._variable.set(not self.state)
85 else:
86 self._setstate(not self.state)
87
88 if self.oldcommand and self.state: # call command only when state goes to 1
89 self.oldcommand()
90 return "break"
74 91
75 self.setstate(self.state) 92 def _setstate(self,newstate):
93 self.state = newstate
94 if newstate: # set
95 self.config(bg='red',relief='sunken')
96 else: # unset
97 self.config(bg=self._origbkg,relief='raised')
98 return "break"
76 99
77 self.bind("<Enter>",lambda ev: self.setstate) 100 if __name__=='__main__':
78 self.bind("<Leave>",lambda ev: self.setstate) 101 root=Tk()
79 102 root.tk_focusFollowsMouse()
80 def varchanged(self,*args): 103 iv=IntVar()
81 self.setstate(self.variable.get()) 104 def cb():
82 105 print "cb!"
83 def invoke(self): 106 t = Togglebutton(root,text="testbutton",command=cb,variable=iv)
84 self.setstate(not self.state) 107 t.pack()
85 108 Entry(root,textvariable=iv).pack()
86 if self.oldcommand: 109 root.mainloop()
87 self.oldcommand()
88
89 def setstate(self,newstate):
90 self.variable.set(newstate)
91 if newstate: # set
92 self.tk.call('tkButtonDown',self)
93 self.config(bg='green')
94 else: # unset
95 self.tk.call('tkButtonUp',self)
96 self.config(bg=self.origbkg)
97 return "break"