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