Mercurial > code > home > repos > light9
annotate light8/uihelpers.py @ 34:411de8b46aef
the famous you-are-in-the-process-of-changing-this-light indicator.
the famous you-are-in-the-process-of-changing-this-light indicator.
red = going up
blue = going down
also, a generic color fader in uihelpers.py -- unused (as of now)
author | dmcc |
---|---|
date | Sun, 07 Jul 2002 12:06:16 +0000 |
parents | e9d2e7754fd9 |
children | 2ae11dc56b38 |
rev | line source |
---|---|
0 | 1 """all the tiny tk helper functions""" |
2 | |
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 | 5 |
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 | 8 f.pack(side='left') |
9 return f | |
10 | |
11 def bindkeys(root,key, func): | |
12 root.bind(key, func) | |
13 for w in root.winfo_children(): | |
14 w.bind(key, func) | |
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 | 23 |
24 def toggle_slider(s): | |
25 if s.get() == 0: | |
26 s.set(100) | |
27 else: | |
28 s.set(0) | |
29 | |
30 # for lambda callbacks | |
31 def printout(t): | |
32 print t | |
33 | |
34 def colorlabel(label): | |
35 """color a label based on its own text""" | |
36 txt=label['text'] or "0" | |
37 lev=float(txt)/100 | |
38 low=(80,80,180) | |
39 high=(255,55,050) | |
40 out = [int(l+lev*(h-l)) for h,l in zip(high,low)] | |
41 col="#%02X%02X%02X" % tuple(out) | |
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): |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
57 """works like a single radiobutton, but it's a button so the label's on the button face, not to the side""" |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
58 def __init__(self,parent,**kw): |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
59 if kw['variable']: |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
60 self.variable = kw['variable'] |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
61 self.variable.trace('w',self.varchanged) |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
62 del kw['variable'] |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
63 else: |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
64 self.variable=None |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
65 self.oldcommand = kw.get('command',None) |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
66 kw['command'] = self.invoke |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
67 Button.__init__(self,parent,**kw) |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
68 |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
69 self.origbkg = self.cget('bg') |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
70 |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
71 self.state=0 |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
72 if self.variable: |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
73 self.state = self.variable.get() |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
74 |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
75 self.setstate(self.state) |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
76 |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
77 self.bind("<Enter>",lambda ev: self.setstate) |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
78 self.bind("<Leave>",lambda ev: self.setstate) |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
79 |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
80 def varchanged(self,*args): |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
81 self.setstate(self.variable.get()) |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
82 |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
83 def invoke(self): |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
84 self.setstate(not self.state) |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
85 |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
86 if self.oldcommand: |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
87 self.oldcommand() |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
88 |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
89 def setstate(self,newstate): |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
90 self.variable.set(newstate) |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
91 if newstate: # set |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
92 self.tk.call('tkButtonDown',self) |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
93 self.config(bg='green') |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
94 else: # unset |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
95 self.tk.call('tkButtonUp',self) |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
96 self.config(bg=self.origbkg) |
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
97 return "break" |