annotate light8/uihelpers.py @ 68:57b3c454465a

result of 7.8.2002 run-through
author dmcc
date Wed, 10 Jul 2002 00:39:14 +0000
parents 0bf7e664f913
children ab0be21b549b
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"""
51
71489bb71528 - Meet Fader. He is going to grow up and be a crossfader some day
dmcc
parents: 47
diff changeset
2
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
3 from __future__ import nested_scopes
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
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
45b12307c695 Initial revision
drewp
parents:
diff changeset
7
68
57b3c454465a result of 7.8.2002 run-through
dmcc
parents: 67
diff changeset
8 windowlocations = {
57b3c454465a result of 7.8.2002 run-through
dmcc
parents: 67
diff changeset
9 'sub' : '425x738+00+00',
57b3c454465a result of 7.8.2002 run-through
dmcc
parents: 67
diff changeset
10 'console' : '168x24+848+000',
57b3c454465a result of 7.8.2002 run-through
dmcc
parents: 67
diff changeset
11 'leveldisplay' : '144x340+870+400',
57b3c454465a result of 7.8.2002 run-through
dmcc
parents: 67
diff changeset
12 'cuefader' : '314x212+546+741',
57b3c454465a result of 7.8.2002 run-through
dmcc
parents: 67
diff changeset
13 'effect' : '24x24+0963+338',
57b3c454465a result of 7.8.2002 run-through
dmcc
parents: 67
diff changeset
14 'stage' : '823x683+117+030',
57b3c454465a result of 7.8.2002 run-through
dmcc
parents: 67
diff changeset
15 }
57b3c454465a result of 7.8.2002 run-through
dmcc
parents: 67
diff changeset
16
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
17 def make_frame(parent):
12
7adc65771676 big restructuring - moved lots of things (including most panels) to other files
drewp
parents: 0
diff changeset
18 f = Frame(parent, bd=0)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
19 f.pack(side='left')
45b12307c695 Initial revision
drewp
parents:
diff changeset
20 return f
45b12307c695 Initial revision
drewp
parents:
diff changeset
21
45b12307c695 Initial revision
drewp
parents:
diff changeset
22 def bindkeys(root,key, func):
45b12307c695 Initial revision
drewp
parents:
diff changeset
23 root.bind(key, func)
45b12307c695 Initial revision
drewp
parents:
diff changeset
24 for w in root.winfo_children():
45b12307c695 Initial revision
drewp
parents:
diff changeset
25 w.bind(key, func)
45b12307c695 Initial revision
drewp
parents:
diff changeset
26
67
0bf7e664f913 window pos saving
dmcc
parents: 61
diff changeset
27 # def toplevelat(x,y,w=None,h=None):
68
57b3c454465a result of 7.8.2002 run-through
dmcc
parents: 67
diff changeset
28 def toplevelat(name, windowpos=None):
51
71489bb71528 - Meet Fader. He is going to grow up and be a crossfader some day
dmcc
parents: 47
diff changeset
29 tl = Toplevel()
67
0bf7e664f913 window pos saving
dmcc
parents: 61
diff changeset
30
68
57b3c454465a result of 7.8.2002 run-through
dmcc
parents: 67
diff changeset
31 tl.wm_geometry(windowlocations[name])
57b3c454465a result of 7.8.2002 run-through
dmcc
parents: 67
diff changeset
32
57b3c454465a result of 7.8.2002 run-through
dmcc
parents: 67
diff changeset
33 # if name in windowpos:
57b3c454465a result of 7.8.2002 run-through
dmcc
parents: 67
diff changeset
34 # tkname, geom = windowpos[name]
57b3c454465a result of 7.8.2002 run-through
dmcc
parents: 67
diff changeset
35 # tl.wm_geometry(geom)
57b3c454465a result of 7.8.2002 run-through
dmcc
parents: 67
diff changeset
36 # windowpos[name] = str(tl), geom
67
0bf7e664f913 window pos saving
dmcc
parents: 61
diff changeset
37
0bf7e664f913 window pos saving
dmcc
parents: 61
diff changeset
38 # if w and h:
0bf7e664f913 window pos saving
dmcc
parents: 61
diff changeset
39 # tl.wm_geometry("%dx%d+%d+%d" % (w,h,x,y))
0bf7e664f913 window pos saving
dmcc
parents: 61
diff changeset
40 # else:
0bf7e664f913 window pos saving
dmcc
parents: 61
diff changeset
41 # 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
42 return tl
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
43
45b12307c695 Initial revision
drewp
parents:
diff changeset
44 def toggle_slider(s):
45b12307c695 Initial revision
drewp
parents:
diff changeset
45 if s.get() == 0:
45b12307c695 Initial revision
drewp
parents:
diff changeset
46 s.set(100)
45b12307c695 Initial revision
drewp
parents:
diff changeset
47 else:
45b12307c695 Initial revision
drewp
parents:
diff changeset
48 s.set(0)
45b12307c695 Initial revision
drewp
parents:
diff changeset
49
45b12307c695 Initial revision
drewp
parents:
diff changeset
50 # for lambda callbacks
45b12307c695 Initial revision
drewp
parents:
diff changeset
51 def printout(t):
45b12307c695 Initial revision
drewp
parents:
diff changeset
52 print t
58
a4d000f759b4 new event handling utils
drewp
parents: 52
diff changeset
53
a4d000f759b4 new event handling utils
drewp
parents: 52
diff changeset
54 def printevent(ev):
a4d000f759b4 new event handling utils
drewp
parents: 52
diff changeset
55 for k in dir(ev):
a4d000f759b4 new event handling utils
drewp
parents: 52
diff changeset
56 if not k.startswith('__'):
a4d000f759b4 new event handling utils
drewp
parents: 52
diff changeset
57 print k,getattr(ev,k)
a4d000f759b4 new event handling utils
drewp
parents: 52
diff changeset
58 print ""
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
59
58
a4d000f759b4 new event handling utils
drewp
parents: 52
diff changeset
60 def eventtoparent(ev,sequence):
a4d000f759b4 new event handling utils
drewp
parents: 52
diff changeset
61 "passes an event to the parent"
a4d000f759b4 new event handling utils
drewp
parents: 52
diff changeset
62 evdict={}
a4d000f759b4 new event handling utils
drewp
parents: 52
diff changeset
63 for x in ['state', 'time', 'y', 'x', 'serial']:
a4d000f759b4 new event handling utils
drewp
parents: 52
diff changeset
64 evdict[x]=getattr(ev,x)
a4d000f759b4 new event handling utils
drewp
parents: 52
diff changeset
65 # evdict['button']=ev.num
a4d000f759b4 new event handling utils
drewp
parents: 52
diff changeset
66 par=ev.widget.winfo_parent()
a4d000f759b4 new event handling utils
drewp
parents: 52
diff changeset
67 if par!=".":
a4d000f759b4 new event handling utils
drewp
parents: 52
diff changeset
68 ev.widget.nametowidget(par).event_generate(sequence,**evdict)
a4d000f759b4 new event handling utils
drewp
parents: 52
diff changeset
69 #else the event made it all the way to the top, unhandled
a4d000f759b4 new event handling utils
drewp
parents: 52
diff changeset
70
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
71 def colorlabel(label):
45b12307c695 Initial revision
drewp
parents:
diff changeset
72 """color a label based on its own text"""
45b12307c695 Initial revision
drewp
parents:
diff changeset
73 txt=label['text'] or "0"
45b12307c695 Initial revision
drewp
parents:
diff changeset
74 lev=float(txt)/100
45b12307c695 Initial revision
drewp
parents:
diff changeset
75 low=(80,80,180)
45b12307c695 Initial revision
drewp
parents:
diff changeset
76 high=(255,55,050)
45b12307c695 Initial revision
drewp
parents:
diff changeset
77 out = [int(l+lev*(h-l)) for h,l in zip(high,low)]
45b12307c695 Initial revision
drewp
parents:
diff changeset
78 col="#%02X%02X%02X" % tuple(out)
45b12307c695 Initial revision
drewp
parents:
diff changeset
79 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
80
34
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
81 # TODO: get everyone to use this
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
82 def colorfade(low, high, percent):
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
83 '''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
84 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
85 col="#%02X%02X%02X" % tuple(out)
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
86 return col
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
87
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
88 def colortotuple(anytkobj, colorname):
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
89 '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
90 rgb = anytkobj.winfo_rgb(colorname)
411de8b46aef the famous you-are-in-the-process-of-changing-this-light indicator.
dmcc
parents: 30
diff changeset
91 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
92
30
e9d2e7754fd9 sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents: 26
diff changeset
93 class Togglebutton(Button):
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
94 """works like a single radiobutton, but it's a button so the
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
95 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
96 callback is called on button set, not on unset. takes a variable
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
97 just like a checkbutton"""
52
065896b0913c emergency commit
dmcc
parents: 51
diff changeset
98 def __init__(self,parent,variable=None,command=None,downcolor='red',**kw):
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 self.oldcommand = command
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
101 Button.__init__(self,parent,command=self.invoke,**kw)
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
102
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
103 self._origbkg = self.cget('bg')
52
065896b0913c emergency commit
dmcc
parents: 51
diff changeset
104 self.downcolor = downcolor
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
105
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
106 self._variable = variable
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
107 if self._variable:
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
108 self._variable.trace('w',self._varchanged)
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
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 else:
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
111 self._setstate(0)
30
e9d2e7754fd9 sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents: 26
diff changeset
112
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
113 self.bind("<Return>",self.invoke)
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
114 self.bind("<1>",self.invoke)
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
115 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
116
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
117 def _varchanged(self,*args):
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
118 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
119
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
120 def invoke(self,*ev):
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
121 if self._variable:
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
122 self._variable.set(not self.state)
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
123 else:
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
124 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
125
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
126 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
127 self.oldcommand()
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
128 return "break"
30
e9d2e7754fd9 sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents: 26
diff changeset
129
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
130 def _setstate(self,newstate):
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
131 self.state = newstate
30
e9d2e7754fd9 sideways subs, new x/y buttons (which don't draw right, but they work)
drewp
parents: 26
diff changeset
132 if newstate: # set
52
065896b0913c emergency commit
dmcc
parents: 51
diff changeset
133 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
134 else: # unset
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
135 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
136 return "break"
47
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
137
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
138 if __name__=='__main__':
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
139 root=Tk()
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
140 root.tk_focusFollowsMouse()
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
141 iv=IntVar()
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
142 def cb():
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
143 print "cb!"
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
144 t = Togglebutton(root,text="testbutton",command=cb,variable=iv)
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
145 t.pack()
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
146 Entry(root,textvariable=iv).pack()
2ae11dc56b38 did a nice job with Togglebutton
drewp
parents: 34
diff changeset
147 root.mainloop()