Mercurial > code > home > repos > light9
annotate light8/uihelpers.py @ 101:a995fd1a8f03
result of 7.12 performance
author | dmcc |
---|---|
date | Sun, 14 Jul 2002 02:44:27 +0000 |
parents | 57319ec2bfad |
children | e04f7b552bcd |
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 |
68 | 8 windowlocations = { |
9 'sub' : '425x738+00+00', | |
10 'console' : '168x24+848+000', | |
11 'leveldisplay' : '144x340+870+400', | |
12 'cuefader' : '314x212+546+741', | |
13 'effect' : '24x24+0963+338', | |
69 | 14 'stage' : '823x683+37+030', |
15 'scenes' : '504x198+462+12', | |
68 | 16 } |
17 | |
0 | 18 def make_frame(parent): |
12
7adc65771676
big restructuring - moved lots of things (including most panels) to other files
drewp
parents:
0
diff
changeset
|
19 f = Frame(parent, bd=0) |
0 | 20 f.pack(side='left') |
21 return f | |
22 | |
23 def bindkeys(root,key, func): | |
24 root.bind(key, func) | |
25 for w in root.winfo_children(): | |
26 w.bind(key, func) | |
27 | |
84 | 28 |
29 def toplevel_savegeometry(tl,name): | |
30 try: | |
90
d34a4956417a
rsn has a separate thread that receives rlslider connections from a potserver.py process,
drewp
parents:
84
diff
changeset
|
31 geo = tl.geometry() |
d34a4956417a
rsn has a separate thread that receives rlslider connections from a potserver.py process,
drewp
parents:
84
diff
changeset
|
32 if not geo.startswith("1x1"): |
d34a4956417a
rsn has a separate thread that receives rlslider connections from a potserver.py process,
drewp
parents:
84
diff
changeset
|
33 f=open(".light9-window-geometry-%s" % name.replace(' ','_'),'w') |
d34a4956417a
rsn has a separate thread that receives rlslider connections from a potserver.py process,
drewp
parents:
84
diff
changeset
|
34 f.write(tl.geometry()) |
d34a4956417a
rsn has a separate thread that receives rlslider connections from a potserver.py process,
drewp
parents:
84
diff
changeset
|
35 # else the window never got mapped |
84 | 36 except: |
37 # it's ok if there's no saved geometry | |
38 pass | |
39 | |
40 # this would get called repeatedly for each child of the window (i | |
41 # dont know why) so we unbind after the first Destroy event | |
42 tl.unbind("<Destroy>",tl._toplevelat_funcid) | |
43 | |
78
0969d8a6729d
support for external sliders. fill in ExternalInput with real IO
dmcc
parents:
69
diff
changeset
|
44 def toplevelat(name): |
51
71489bb71528
- Meet Fader. He is going to grow up and be a crossfader some day
dmcc
parents:
47
diff
changeset
|
45 tl = Toplevel() |
67 | 46 |
84 | 47 try: |
48 f=open(".light9-window-geometry-%s" % name.replace(' ','_')) | |
49 windowlocations[name]=f.read() # file has no newline | |
50 except: | |
51 # it's ok if there's no saved geometry | |
52 pass | |
53 | |
78
0969d8a6729d
support for external sliders. fill in ExternalInput with real IO
dmcc
parents:
69
diff
changeset
|
54 if name in windowlocations: |
84 | 55 tl.geometry(windowlocations[name]) |
56 | |
57 tl._toplevelat_funcid=tl.bind("<Destroy>",lambda ev,tl=tl,name=name: toplevel_savegeometry(tl,name)) | |
68 | 58 |
12
7adc65771676
big restructuring - moved lots of things (including most panels) to other files
drewp
parents:
0
diff
changeset
|
59 return tl |
0 | 60 |
61 def toggle_slider(s): | |
62 if s.get() == 0: | |
63 s.set(100) | |
64 else: | |
65 s.set(0) | |
66 | |
67 # for lambda callbacks | |
68 def printout(t): | |
69 print t | |
58 | 70 |
71 def printevent(ev): | |
72 for k in dir(ev): | |
73 if not k.startswith('__'): | |
74 print k,getattr(ev,k) | |
75 print "" | |
0 | 76 |
58 | 77 def eventtoparent(ev,sequence): |
78 "passes an event to the parent" | |
79 evdict={} | |
80 for x in ['state', 'time', 'y', 'x', 'serial']: | |
81 evdict[x]=getattr(ev,x) | |
82 # evdict['button']=ev.num | |
83 par=ev.widget.winfo_parent() | |
84 if par!=".": | |
85 ev.widget.nametowidget(par).event_generate(sequence,**evdict) | |
86 #else the event made it all the way to the top, unhandled | |
87 | |
0 | 88 def colorlabel(label): |
89 """color a label based on its own text""" | |
90 txt=label['text'] or "0" | |
91 lev=float(txt)/100 | |
92 low=(80,80,180) | |
93 high=(255,55,050) | |
94 out = [int(l+lev*(h-l)) for h,l in zip(high,low)] | |
95 col="#%02X%02X%02X" % tuple(out) | |
96 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
|
97 |
34
411de8b46aef
the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents:
30
diff
changeset
|
98 # TODO: get everyone to use this |
411de8b46aef
the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents:
30
diff
changeset
|
99 def colorfade(low, high, percent): |
411de8b46aef
the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents:
30
diff
changeset
|
100 '''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
|
101 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
|
102 col="#%02X%02X%02X" % tuple(out) |
411de8b46aef
the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents:
30
diff
changeset
|
103 return col |
411de8b46aef
the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents:
30
diff
changeset
|
104 |
411de8b46aef
the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents:
30
diff
changeset
|
105 def colortotuple(anytkobj, colorname): |
411de8b46aef
the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents:
30
diff
changeset
|
106 '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
|
107 rgb = anytkobj.winfo_rgb(colorname) |
411de8b46aef
the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents:
30
diff
changeset
|
108 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
|
109 |
30
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
110 class Togglebutton(Button): |
47 | 111 """works like a single radiobutton, but it's a button so the |
112 label's on the button face, not to the side. the optional command | |
113 callback is called on button set, not on unset. takes a variable | |
114 just like a checkbutton""" | |
52 | 115 def __init__(self,parent,variable=None,command=None,downcolor='red',**kw): |
47 | 116 |
117 self.oldcommand = command | |
118 Button.__init__(self,parent,command=self.invoke,**kw) | |
119 | |
120 self._origbkg = self.cget('bg') | |
52 | 121 self.downcolor = downcolor |
47 | 122 |
123 self._variable = variable | |
124 if self._variable: | |
125 self._variable.trace('w',self._varchanged) | |
126 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
|
127 else: |
47 | 128 self._setstate(0) |
30
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
129 |
47 | 130 self.bind("<Return>",self.invoke) |
131 self.bind("<1>",self.invoke) | |
132 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
|
133 |
47 | 134 def _varchanged(self,*args): |
135 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
|
136 |
47 | 137 def invoke(self,*ev): |
138 if self._variable: | |
139 self._variable.set(not self.state) | |
140 else: | |
141 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
|
142 |
47 | 143 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
|
144 self.oldcommand() |
47 | 145 return "break" |
30
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
146 |
47 | 147 def _setstate(self,newstate): |
148 self.state = newstate | |
30
e9d2e7754fd9
sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents:
26
diff
changeset
|
149 if newstate: # set |
52 | 150 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
|
151 else: # unset |
47 | 152 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
|
153 return "break" |
47 | 154 |
95
d5deeed83228
FancyDoubleVar is a DoubleVar where you can temporarily disable the callbacks
drewp
parents:
90
diff
changeset
|
155 |
d5deeed83228
FancyDoubleVar is a DoubleVar where you can temporarily disable the callbacks
drewp
parents:
90
diff
changeset
|
156 class FancyDoubleVar(DoubleVar): |
d5deeed83228
FancyDoubleVar is a DoubleVar where you can temporarily disable the callbacks
drewp
parents:
90
diff
changeset
|
157 def __init__(self,master=None): |
d5deeed83228
FancyDoubleVar is a DoubleVar where you can temporarily disable the callbacks
drewp
parents:
90
diff
changeset
|
158 DoubleVar.__init__(self,master) |
101 | 159 self.callbacklist = {} # cbname : mode |
160 self.namedtraces = {} # name : cbname | |
95
d5deeed83228
FancyDoubleVar is a DoubleVar where you can temporarily disable the callbacks
drewp
parents:
90
diff
changeset
|
161 def trace_variable(self,mode,callback): |
97 | 162 """Define a trace callback for the variable. |
95
d5deeed83228
FancyDoubleVar is a DoubleVar where you can temporarily disable the callbacks
drewp
parents:
90
diff
changeset
|
163 |
97 | 164 MODE is one of "r", "w", "u" for read, write, undefine. |
165 CALLBACK must be a function which is called when | |
166 the variable is read, written or undefined. | |
95
d5deeed83228
FancyDoubleVar is a DoubleVar where you can temporarily disable the callbacks
drewp
parents:
90
diff
changeset
|
167 |
97 | 168 Return the name of the callback. |
169 """ | |
170 cbname = self._master._register(callback) | |
171 self._tk.call("trace", "variable", self._name, mode, cbname) | |
172 | |
173 # we build a list of the trace callbacks (the py functrions and the tcl functionnames) | |
101 | 174 self.callbacklist[cbname] = mode |
98 | 175 # print "added trace:",callback,cbname |
97 | 176 |
177 return cbname | |
178 trace=trace_variable | |
95
d5deeed83228
FancyDoubleVar is a DoubleVar where you can temporarily disable the callbacks
drewp
parents:
90
diff
changeset
|
179 def disable_traces(self): |
97 | 180 for cb,mode in self.callbacklist.items(): |
181 # DoubleVar.trace_vdelete(self,v[0],k) | |
182 self._tk.call("trace", "vdelete", self._name, mode,cb) | |
183 # but no master delete! | |
184 | |
95
d5deeed83228
FancyDoubleVar is a DoubleVar where you can temporarily disable the callbacks
drewp
parents:
90
diff
changeset
|
185 def recreate_traces(self): |
97 | 186 for cb,mode in self.callbacklist.items(): |
187 # self.trace_variable(v[0],v[1]) | |
188 self._tk.call("trace", "variable", self._name, mode,cb) | |
189 | |
101 | 190 def trace_named(self, name, callback): |
191 if name in self.namedtraces: | |
192 print "FancyDoubleVar: already had a trace named %s - replacing it" % name | |
193 self.delete_named(name) | |
194 | |
195 cbname = self.trace_variable('w',callback) # this will register in self.callbacklist too | |
196 | |
197 self.namedtraces[name] = cbname | |
198 return cbname | |
199 | |
200 def delete_named(self, name): | |
201 if name in self.namedtraces: | |
202 | |
203 cbname = self.namedtraces[name] | |
204 | |
205 self.trace_vdelete('w',cbname) | |
206 #self._tk.call("trace","vdelete",self._name,'w',cbname) | |
207 print "FancyDoubleVar: successfully deleted trace named %s" % name | |
208 else: | |
209 print "FancyDoubleVar: attempted to delete named %s which wasn't set to any function" % name | |
210 | |
211 | |
212 | |
95
d5deeed83228
FancyDoubleVar is a DoubleVar where you can temporarily disable the callbacks
drewp
parents:
90
diff
changeset
|
213 |
47 | 214 if __name__=='__main__': |
215 root=Tk() | |
216 root.tk_focusFollowsMouse() | |
217 iv=IntVar() | |
218 def cb(): | |
219 print "cb!" | |
220 t = Togglebutton(root,text="testbutton",command=cb,variable=iv) | |
221 t.pack() | |
222 Entry(root,textvariable=iv).pack() | |
223 root.mainloop() | |
101 | 224 |
225 | |
226 | |
227 | |
228 | |
229 | |
230 | |
231 | |
232 |