Mercurial > code > home > repos > light9
annotate Widgets/FlyingFader.py @ 22:626aa2179630
some naming and defaults tweaks
some naming and defaults tweaks
interrupted fades now go to the new destination at the same rate they were traveling.
not very cool
author | drewp |
---|---|
date | Sun, 07 Jul 2002 07:22:12 +0000 |
parents | 6eafd86930b5 |
children | 548d3aa2660f |
rev | line source |
---|---|
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
1 from Tkinter import * |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
2 from time import time |
0 | 3 |
4 class FlyingFader(Frame): | |
22 | 5 def __init__(self, master, variable, label, fadedur=1.5, font=('Arial', 8), |
15
c76b62eccdec
put **kw back in, they now go to the scale and can override the defaults.
dmcc
parents:
11
diff
changeset
|
6 **kw): |
0 | 7 Frame.__init__(self, master) |
8 self.name = label | |
9 self.variable = variable | |
22 | 10 self.fadedur = fadedur |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
11 self.curfade = None |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
12 self.fadetimes = 0, 0 |
0 | 13 |
14 self.config({'bd':1, 'relief':'raised'}) | |
22 | 15 scaleopts = {'variable' : variable, 'showvalue' : 0, 'from' : 1.0, |
16 'to' : 0, 'res' : 0.001, 'width' : 20, 'length' : 200} | |
15
c76b62eccdec
put **kw back in, they now go to the scale and can override the defaults.
dmcc
parents:
11
diff
changeset
|
17 scaleopts.update(kw) |
c76b62eccdec
put **kw back in, they now go to the scale and can override the defaults.
dmcc
parents:
11
diff
changeset
|
18 |
c76b62eccdec
put **kw back in, they now go to the scale and can override the defaults.
dmcc
parents:
11
diff
changeset
|
19 self.scale = Scale(self, scaleopts) |
22 | 20 self.vlabel = Label(self, text="0.0", width=6, font=font) |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
21 self.label = Label(self, text=label, wraplength=40, font=font) |
0 | 22 |
23 self.oldtrough = self.scale['troughcolor'] | |
24 | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
25 self.scale.pack(side=TOP, expand=1, fill=BOTH, anchor='c') |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
26 self.vlabel.pack(side=BOTTOM, expand=0, fill=X) |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
27 self.label.pack(side=BOTTOM, expand=0, fill=X) |
0 | 28 |
29 for k in range(1, 10): | |
30 self.scale.bind("<Key-%d>" % k, | |
20 | 31 lambda evt, k=k: self.newfade(k / 10.0, evt)) |
0 | 32 |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
33 self.scale.bind("<Key-0>", lambda evt: self.newfade(100, evt)) |
0 | 34 self.scale.bind("<grave>", lambda evt: self.newfade(0, evt)) |
35 | |
36 self.scale.bind("<1>", self.cancelfade) | |
11
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
37 self.scale.bind("<2>", self.cancelfade) |
0 | 38 self.scale.bind("<3>", self.mousefade) |
39 | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
40 self.variable.trace('w', self.updatelabel) |
0 | 41 |
42 def cancelfade(self, evt): | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
43 self.fadetimes = 0, 0 |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
44 self.curfade = 0, self.variable.get() |
0 | 45 self.scale['troughcolor'] = self.oldtrough |
46 | |
47 def mousefade(self, evt): | |
48 target = float(self.tk.call(self.scale, 'get', evt.x, evt.y)) | |
49 self.newfade(target, evt) | |
50 | |
22 | 51 def isfading(self): |
52 return self.fadetimes[0] or self.fadetimes[1] | |
53 | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
54 def newfade(self, newlevel, evt=None, length=None): |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
55 if length is None: |
22 | 56 length = self.fadedur |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
57 mult = 1 |
0 | 58 |
59 if evt.state & 8 and evt.state & 4: mult = 0.25 # both | |
60 elif evt.state & 8: mult = 0.5 # alt | |
61 elif evt.state & 4: mult = 2 # control | |
62 | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
63 now = time() |
22 | 64 if not self.isfading(): |
65 self.fadetimes = (now, now + (mult * length)) | |
66 self.curfade = (self.variable.get(), newlevel) | |
67 else: | |
68 # already fading | |
69 t1,t2=self.fadetimes | |
70 v1,v2=self.curfade | |
71 rate = abs((v2-v1)/(t2-t1)) | |
72 | |
73 vnow=self.variable.get() | |
74 newdist=abs(newlevel-vnow) | |
75 newdur=newdist/rate | |
76 | |
77 self.fadetimes = (now,now+newdur) | |
78 self.curfade=(vnow,newlevel) | |
79 | |
80 | |
0 | 81 |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
82 self.scale['troughcolor'] = 'red' |
0 | 83 |
84 self.gofade() | |
85 | |
86 def gofade(self): | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
87 now = time() |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
88 start, end = self.fadetimes |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
89 lstart, lend = self.curfade |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
90 if now > end: |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
91 self.fadetimes = 0, 0 |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
92 self.variable.set(lend) |
0 | 93 self.scale['troughcolor'] = self.oldtrough |
94 return | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
95 percent = (now - start) / (end - start) |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
96 newvalue = (percent * (lend - lstart)) + lstart |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
97 self.variable.set(newvalue) |
11
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
98 colorfade(self.scale, percent) |
22 | 99 self.after(30, self.gofade) |
0 | 100 |
101 def updatelabel(self, *args): | |
18
053889940418
some changes to allow integration into rsn. more needed
dmcc
parents:
15
diff
changeset
|
102 self.vlabel['text'] = "%.3f" % self.variable.get() |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
103 if self.fadetimes[1] == 0: # no fade |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
104 self.vlabel['fg'] = 'black' |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
105 elif self.curfade[1] > self.curfade[0]: |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
106 self.vlabel['fg'] = 'red' |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
107 else: |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
108 self.vlabel['fg'] = 'blue' |
0 | 109 |
11
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
110 def get(self): |
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
111 return self.scale.get() |
0 | 112 |
11
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
113 def set(self, val): |
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
114 self.scale.set(val) |
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
115 |
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
116 |
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
117 def colorfade(scale, lev): |
0 | 118 low = (255, 255, 255) |
119 high = (0, 0, 0) | |
11
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
120 out = [int(l+lev*(h-l)) for h, l in zip(high,low)] |
0 | 121 col="#%02X%02X%02X" % tuple(out) |
122 scale.config(troughcolor=col) | |
123 | |
124 if __name__ == '__main__': | |
125 root = Tk() | |
126 root.tk_focusFollowsMouse() | |
127 | |
128 FlyingFader(root, variable=DoubleVar(), label="suck").pack(side=LEFT, | |
129 expand=1, fill=BOTH) | |
130 FlyingFader(root, variable=DoubleVar(), label="moof").pack(side=LEFT, | |
131 expand=1, fill=BOTH) | |
132 FlyingFader(root, variable=DoubleVar(), label="zarf").pack(side=LEFT, | |
133 expand=1, fill=BOTH) | |
134 FlyingFader(root, variable=DoubleVar(), | |
135 label="long name goes here. got it?").pack(side=LEFT, expand=1, | |
136 fill=BOTH) | |
137 | |
138 root.mainloop() |