annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
1 """all the tiny tk helper functions"""
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
2 from __future__ import nested_scopes
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
3 from Tkinter import *
34
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
4 from types import StringType
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
5
45b12307c695 Initial revision
drewp
parents:
diff changeset
6 def make_frame(parent):
12
7adc65771676 big restructuring - moved lots of things (including most panels) to other files
drewp
parents: 0
diff changeset
7 f = Frame(parent, bd=0)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
8 f.pack(side='left')
45b12307c695 Initial revision
drewp
parents:
diff changeset
9 return f
45b12307c695 Initial revision
drewp
parents:
diff changeset
10
45b12307c695 Initial revision
drewp
parents:
diff changeset
11 def bindkeys(root,key, func):
45b12307c695 Initial revision
drewp
parents:
diff changeset
12 root.bind(key, func)
45b12307c695 Initial revision
drewp
parents:
diff changeset
13 for w in root.winfo_children():
45b12307c695 Initial revision
drewp
parents:
diff changeset
14 w.bind(key, func)
45b12307c695 Initial revision
drewp
parents:
diff changeset
15
12
7adc65771676 big restructuring - moved lots of things (including most panels) to other files
drewp
parents: 0
diff changeset
16 def toplevelat(x,y,w=None,h=None):
7adc65771676 big restructuring - moved lots of things (including most panels) to other files
drewp
parents: 0
diff changeset
17 tl=Toplevel()
7adc65771676 big restructuring - moved lots of things (including most panels) to other files
drewp
parents: 0
diff changeset
18 if w and h:
7adc65771676 big restructuring - moved lots of things (including most panels) to other files
drewp
parents: 0
diff changeset
19 tl.wm_geometry("%dx%d+%d+%d"%(w,h,x,y))
7adc65771676 big restructuring - moved lots of things (including most panels) to other files
drewp
parents: 0
diff changeset
20 else:
7adc65771676 big restructuring - moved lots of things (including most panels) to other files
drewp
parents: 0
diff changeset
21 tl.wm_geometry("+%d+%d"%(x,y))
7adc65771676 big restructuring - moved lots of things (including most panels) to other files
drewp
parents: 0
diff changeset
22 return tl
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
23
45b12307c695 Initial revision
drewp
parents:
diff changeset
24 def toggle_slider(s):
45b12307c695 Initial revision
drewp
parents:
diff changeset
25 if s.get() == 0:
45b12307c695 Initial revision
drewp
parents:
diff changeset
26 s.set(100)
45b12307c695 Initial revision
drewp
parents:
diff changeset
27 else:
45b12307c695 Initial revision
drewp
parents:
diff changeset
28 s.set(0)
45b12307c695 Initial revision
drewp
parents:
diff changeset
29
45b12307c695 Initial revision
drewp
parents:
diff changeset
30 # for lambda callbacks
45b12307c695 Initial revision
drewp
parents:
diff changeset
31 def printout(t):
45b12307c695 Initial revision
drewp
parents:
diff changeset
32 print t
45b12307c695 Initial revision
drewp
parents:
diff changeset
33
45b12307c695 Initial revision
drewp
parents:
diff changeset
34 def colorlabel(label):
45b12307c695 Initial revision
drewp
parents:
diff changeset
35 """color a label based on its own text"""
45b12307c695 Initial revision
drewp
parents:
diff changeset
36 txt=label['text'] or "0"
45b12307c695 Initial revision
drewp
parents:
diff changeset
37 lev=float(txt)/100
45b12307c695 Initial revision
drewp
parents:
diff changeset
38 low=(80,80,180)
45b12307c695 Initial revision
drewp
parents:
diff changeset
39 high=(255,55,050)
45b12307c695 Initial revision
drewp
parents:
diff changeset
40 out = [int(l+lev*(h-l)) for h,l in zip(high,low)]
45b12307c695 Initial revision
drewp
parents:
diff changeset
41 col="#%02X%02X%02X" % tuple(out)
45b12307c695 Initial revision
drewp
parents:
diff changeset
42 label.config(bg=col)
30
e9d2e7754fd9 sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents: 26
diff changeset
43
34
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
44 # TODO: get everyone to use this
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
45 def colorfade(low, high, percent):
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
46 '''not foolproof. make sure 0 < percent < 1'''
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
47 out = [int(l+percent*(h-l)) for h,l in zip(high,low)]
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
48 col="#%02X%02X%02X" % tuple(out)
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
49 return col
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
50
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
51 def colortotuple(anytkobj, colorname):
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
52 'pass any tk object and a color name, like "yellow"'
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
53 rgb = anytkobj.winfo_rgb(colorname)
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
54 return [v / 256 for v in rgb]
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
55
30
e9d2e7754fd9 sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents: 26
diff changeset
56 class Togglebutton(Button):
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
57 """works like a single radiobutton, but it's a button so the
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
58 label's on the button face, not to the side. the optional command
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
59 callback is called on button set, not on unset. takes a variable
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
60 just like a checkbutton"""
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
61 def __init__(self,parent,variable=None,command=None,**kw):
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
62
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
63 self.oldcommand = command
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
64 Button.__init__(self,parent,command=self.invoke,**kw)
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
65
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
66 self._origbkg = self.cget('bg')
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
67
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
68 self._variable = variable
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
69 if self._variable:
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
70 self._variable.trace('w',self._varchanged)
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
71 self._setstate(self._variable.get())
30
e9d2e7754fd9 sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents: 26
diff changeset
72 else:
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
73 self._setstate(0)
30
e9d2e7754fd9 sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents: 26
diff changeset
74
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
75 self.bind("<Return>",self.invoke)
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
76 self.bind("<1>",self.invoke)
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
77 self.bind("<space>",self.invoke)
30
e9d2e7754fd9 sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents: 26
diff changeset
78
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
79 def _varchanged(self,*args):
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
80 self._setstate(self._variable.get())
30
e9d2e7754fd9 sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents: 26
diff changeset
81
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
82 def invoke(self,*ev):
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
83 if self._variable:
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
84 self._variable.set(not self.state)
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
85 else:
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
86 self._setstate(not self.state)
30
e9d2e7754fd9 sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents: 26
diff changeset
87
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
88 if self.oldcommand and self.state: # call command only when state goes to 1
30
e9d2e7754fd9 sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents: 26
diff changeset
89 self.oldcommand()
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
90 return "break"
30
e9d2e7754fd9 sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents: 26
diff changeset
91
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
92 def _setstate(self,newstate):
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
93 self.state = newstate
30
e9d2e7754fd9 sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents: 26
diff changeset
94 if newstate: # set
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
95 self.config(bg='red',relief='sunken')
30
e9d2e7754fd9 sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents: 26
diff changeset
96 else: # unset
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
97 self.config(bg=self._origbkg,relief='raised')
30
e9d2e7754fd9 sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents: 26
diff changeset
98 return "break"
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
99
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
100 if __name__=='__main__':
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
101 root=Tk()
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
102 root.tk_focusFollowsMouse()
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
103 iv=IntVar()
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
104 def cb():
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
105 print "cb!"
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
106 t = Togglebutton(root,text="testbutton",command=cb,variable=iv)
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
107 t.pack()
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
108 Entry(root,textvariable=iv).pack()
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
109 root.mainloop()