Mercurial > code > home > repos > light9
annotate light8/uihelpers.py @ 67:0bf7e664f913
window pos saving
author | dmcc |
---|---|
date | Tue, 09 Jul 2002 09:27:47 +0000 |
parents | 2508c6b7a4e0 |
children | 57b3c454465a |
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 | |
67 | 18 # def toplevelat(x,y,w=None,h=None): |
19 def toplevelat(name, windowpos): | |
51
71489bb71528
- Meet Fader. He is going to grow up and be a crossfader some day
dmcc
parents:
47
diff
changeset
|
20 tl = Toplevel() |
67 | 21 |
22 if name in windowpos: | |
23 tkname, geom = windowpos[name] | |
24 tl.wm_geometry(geom) | |
25 windowpos[name] = str(tl), geom | |
12
7adc65771676
big restructuring - moved lots of things (including most panels) to other files
drewp
parents:
0
diff
changeset
|
26 else: |
67 | 27 windowpos[name] = str(tl), '+0+0' |
28 | |
29 # if w and h: | |
30 # tl.wm_geometry("%dx%d+%d+%d" % (w,h,x,y)) | |
31 # else: | |
32 # 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
|
33 return tl |
0 | 34 |
35 def toggle_slider(s): | |
36 if s.get() == 0: | |
37 s.set(100) | |
38 else: | |
39 s.set(0) | |
40 | |
41 # for lambda callbacks | |
42 def printout(t): | |
43 print t | |
58 | 44 |
45 def printevent(ev): | |
46 for k in dir(ev): | |
47 if not k.startswith('__'): | |
48 print k,getattr(ev,k) | |
49 print "" | |
0 | 50 |
58 | 51 def eventtoparent(ev,sequence): |
52 "passes an event to the parent" | |
53 evdict={} | |
54 for x in ['state', 'time', 'y', 'x', 'serial']: | |
55 evdict[x]=getattr(ev,x) | |
56 # evdict['button']=ev.num | |
57 par=ev.widget.winfo_parent() | |
58 if par!=".": | |
59 ev.widget.nametowidget(par).event_generate(sequence,**evdict) | |
60 #else the event made it all the way to the top, unhandled | |
61 | |
0 | 62 def colorlabel(label): |
63 """color a label based on its own text""" | |
64 txt=label['text'] or "0" | |
65 lev=float(txt)/100 | |
66 low=(80,80,180) | |
67 high=(255,55,050) | |
68 out = [int(l+lev*(h-l)) for h,l in zip(high,low)] | |
69 col="#%02X%02X%02X" % tuple(out) | |
70 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
|
71 |
34
411de8b46aef
the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents:
30
diff
changeset
|
72 # TODO: get everyone to use this |
411de8b46aef
the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents:
30
diff
changeset
|
73 def colorfade(low, high, percent): |
411de8b46aef
the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents:
30
diff
changeset
|
74 '''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
|
75 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
|
76 col="#%02X%02X%02X" % tuple(out) |
411de8b46aef
the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents:
30
diff
changeset
|
77 return col |
411de8b46aef
the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents:
30
diff
changeset
|
78 |
411de8b46aef
the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents:
30
diff
changeset
|
79 def colortotuple(anytkobj, colorname): |
411de8b46aef
the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents:
30
diff
changeset
|
80 '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
|
81 rgb = anytkobj.winfo_rgb(colorname) |
411de8b46aef
the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents:
30
diff
changeset
|
82 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
|
83 |
30
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
84 class Togglebutton(Button): |
47 | 85 """works like a single radiobutton, but it's a button so the |
86 label's on the button face, not to the side. the optional command | |
87 callback is called on button set, not on unset. takes a variable | |
88 just like a checkbutton""" | |
52 | 89 def __init__(self,parent,variable=None,command=None,downcolor='red',**kw): |
47 | 90 |
91 self.oldcommand = command | |
92 Button.__init__(self,parent,command=self.invoke,**kw) | |
93 | |
94 self._origbkg = self.cget('bg') | |
52 | 95 self.downcolor = downcolor |
47 | 96 |
97 self._variable = variable | |
98 if self._variable: | |
99 self._variable.trace('w',self._varchanged) | |
100 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
|
101 else: |
47 | 102 self._setstate(0) |
30
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
103 |
47 | 104 self.bind("<Return>",self.invoke) |
105 self.bind("<1>",self.invoke) | |
106 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
|
107 |
47 | 108 def _varchanged(self,*args): |
109 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
|
110 |
47 | 111 def invoke(self,*ev): |
112 if self._variable: | |
113 self._variable.set(not self.state) | |
114 else: | |
115 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
|
116 |
47 | 117 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
|
118 self.oldcommand() |
47 | 119 return "break" |
30
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
120 |
47 | 121 def _setstate(self,newstate): |
122 self.state = newstate | |
30
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
123 if newstate: # set |
52 | 124 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
|
125 else: # unset |
47 | 126 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
|
127 return "break" |
47 | 128 |
129 if __name__=='__main__': | |
130 root=Tk() | |
131 root.tk_focusFollowsMouse() | |
132 iv=IntVar() | |
133 def cb(): | |
134 print "cb!" | |
135 t = Togglebutton(root,text="testbutton",command=cb,variable=iv) | |
136 t.pack() | |
137 Entry(root,textvariable=iv).pack() | |
138 root.mainloop() |