Mercurial > code > home > repos > light9
annotate Widgets/FlyingFader.py @ 9:342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
The FlyingFader will accept keyboard values and fade to them over 1.5
seconds. Combinations of control and alt change that speed. RMB
also creates a fade and LMB will cancel them. Colors are pretty
and informative. Fades can be created manually with the newfade()
function.
author | dmcc |
---|---|
date | Sun, 07 Jul 2002 05:13:12 +0000 |
parents | 45b12307c695 |
children | c65119b66b00 |
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): | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
5 def __init__(self, master, variable, label, time=1.5, font=('Arial', 8), |
0 | 6 **kw): |
7 Frame.__init__(self, master) | |
8 self.name = label | |
9 self.variable = variable | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
10 self.fadelength = time |
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'}) | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
15 self.scale = Scale(self, variable=variable, showvalue=0, from_=100, |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
16 to_=0, res=0.1, width=20, length=200) |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
17 self.vlabel = Label(self, text="0.0", font=font) |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
18 self.label = Label(self, text=label, wraplength=40, font=font) |
0 | 19 |
20 self.oldtrough = self.scale['troughcolor'] | |
21 | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
22 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
|
23 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
|
24 self.label.pack(side=BOTTOM, expand=0, fill=X) |
0 | 25 |
26 for k in range(1, 10): | |
27 self.scale.bind("<Key-%d>" % k, | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
28 lambda evt, k=k: self.newfade(k * 10, evt)) |
0 | 29 |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
30 self.scale.bind("<Key-0>", lambda evt: self.newfade(100, evt)) |
0 | 31 self.scale.bind("<grave>", lambda evt: self.newfade(0, evt)) |
32 | |
33 self.scale.bind("<1>", self.cancelfade) | |
34 self.scale.bind("<3>", self.mousefade) | |
35 | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
36 self.variable.trace('w', self.updatelabel) |
0 | 37 |
38 def cancelfade(self, evt): | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
39 self.fadetimes = 0, 0 |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
40 self.curfade = 0, self.variable.get() |
0 | 41 self.scale['troughcolor'] = self.oldtrough |
42 | |
43 def mousefade(self, evt): | |
44 target = float(self.tk.call(self.scale, 'get', evt.x, evt.y)) | |
45 self.newfade(target, evt) | |
46 | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
47 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
|
48 if length is None: |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
49 length = self.fadelength |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
50 mult = 1 |
0 | 51 |
52 if evt.state & 8 and evt.state & 4: mult = 0.25 # both | |
53 elif evt.state & 8: mult = 0.5 # alt | |
54 elif evt.state & 4: mult = 2 # control | |
55 | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
56 now = time() |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
57 self.fadetimes = (now, now + (mult * length)) |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
58 self.curfade = (self.variable.get(), newlevel) |
0 | 59 |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
60 self.scale['troughcolor'] = 'red' |
0 | 61 |
62 self.gofade() | |
63 | |
64 def gofade(self): | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
65 now = time() |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
66 start, end = self.fadetimes |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
67 lstart, lend = self.curfade |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
68 if now > end: |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
69 self.fadetimes = 0, 0 |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
70 self.variable.set(lend) |
0 | 71 self.scale['troughcolor'] = self.oldtrough |
72 return | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
73 percent = (now - start) / (end - start) |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
74 newvalue = (percent * (lend - lstart)) + lstart |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
75 self.variable.set(newvalue) |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
76 colortrough(self.scale, percent) |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
77 self.after(10, self.gofade) |
0 | 78 |
79 def updatelabel(self, *args): | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
80 self.vlabel['text'] = "%.1f" % self.variable.get() |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
81 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
|
82 self.vlabel['fg'] = 'black' |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
83 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
|
84 self.vlabel['fg'] = 'red' |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
85 else: |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
86 self.vlabel['fg'] = 'blue' |
0 | 87 |
88 | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
89 def colortrough(scale, lev): |
0 | 90 low = (255, 255, 255) |
91 high = (0, 0, 0) | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
92 out = [int(l+lev*(h-l)) for h,l in zip(high,low)] |
0 | 93 col="#%02X%02X%02X" % tuple(out) |
94 scale.config(troughcolor=col) | |
95 | |
96 if __name__ == '__main__': | |
97 root = Tk() | |
98 root.tk_focusFollowsMouse() | |
99 | |
100 FlyingFader(root, variable=DoubleVar(), label="suck").pack(side=LEFT, | |
101 expand=1, fill=BOTH) | |
102 FlyingFader(root, variable=DoubleVar(), label="moof").pack(side=LEFT, | |
103 expand=1, fill=BOTH) | |
104 FlyingFader(root, variable=DoubleVar(), label="zarf").pack(side=LEFT, | |
105 expand=1, fill=BOTH) | |
106 FlyingFader(root, variable=DoubleVar(), | |
107 label="long name goes here. got it?").pack(side=LEFT, expand=1, | |
108 fill=BOTH) | |
109 | |
110 root.mainloop() |