Mercurial > code > home > repos > light9
annotate Widgets/FlyingFader.py @ 20:6eafd86930b5
fix precision bug
author | dmcc |
---|---|
date | Sun, 07 Jul 2002 06:58:03 +0000 |
parents | 053889940418 |
children | 626aa2179630 |
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): | |
15
c76b62eccdec
put **kw back in, they now go to the scale and can override the defaults.
dmcc
parents:
11
diff
changeset
|
5 def __init__(self, master, variable, label, time=1.5, font=('Arial', 8), |
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 | |
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'}) | |
15
c76b62eccdec
put **kw back in, they now go to the scale and can override the defaults.
dmcc
parents:
11
diff
changeset
|
15 scaleopts = {'variable' : variable, 'showvalue' : 0, 'from' : 100, |
c76b62eccdec
put **kw back in, they now go to the scale and can override the defaults.
dmcc
parents:
11
diff
changeset
|
16 'to' : 0, 'res' : 0.1, 'width' : 20, 'length' : 200} |
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) |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
20 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
|
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 | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
51 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
|
52 if length is None: |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
53 length = self.fadelength |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
54 mult = 1 |
0 | 55 |
56 if evt.state & 8 and evt.state & 4: mult = 0.25 # both | |
57 elif evt.state & 8: mult = 0.5 # alt | |
58 elif evt.state & 4: mult = 2 # control | |
59 | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
60 now = time() |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
61 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
|
62 self.curfade = (self.variable.get(), newlevel) |
0 | 63 |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
64 self.scale['troughcolor'] = 'red' |
0 | 65 |
66 self.gofade() | |
67 | |
68 def gofade(self): | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
69 now = time() |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
70 start, end = self.fadetimes |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
71 lstart, lend = self.curfade |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
72 if now > end: |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
73 self.fadetimes = 0, 0 |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
74 self.variable.set(lend) |
0 | 75 self.scale['troughcolor'] = self.oldtrough |
76 return | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
77 percent = (now - start) / (end - start) |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
78 newvalue = (percent * (lend - lstart)) + lstart |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
79 self.variable.set(newvalue) |
11
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
80 colorfade(self.scale, percent) |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
81 self.after(10, self.gofade) |
0 | 82 |
83 def updatelabel(self, *args): | |
18
053889940418
some changes to allow integration into rsn. more needed
dmcc
parents:
15
diff
changeset
|
84 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
|
85 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
|
86 self.vlabel['fg'] = 'black' |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
87 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
|
88 self.vlabel['fg'] = 'red' |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
89 else: |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
90 self.vlabel['fg'] = 'blue' |
0 | 91 |
11
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
92 def get(self): |
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
93 return self.scale.get() |
0 | 94 |
11
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
95 def set(self, val): |
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
96 self.scale.set(val) |
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
97 |
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
98 |
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
99 def colorfade(scale, lev): |
0 | 100 low = (255, 255, 255) |
101 high = (0, 0, 0) | |
11
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
102 out = [int(l+lev*(h-l)) for h, l in zip(high,low)] |
0 | 103 col="#%02X%02X%02X" % tuple(out) |
104 scale.config(troughcolor=col) | |
105 | |
106 if __name__ == '__main__': | |
107 root = Tk() | |
108 root.tk_focusFollowsMouse() | |
109 | |
110 FlyingFader(root, variable=DoubleVar(), label="suck").pack(side=LEFT, | |
111 expand=1, fill=BOTH) | |
112 FlyingFader(root, variable=DoubleVar(), label="moof").pack(side=LEFT, | |
113 expand=1, fill=BOTH) | |
114 FlyingFader(root, variable=DoubleVar(), label="zarf").pack(side=LEFT, | |
115 expand=1, fill=BOTH) | |
116 FlyingFader(root, variable=DoubleVar(), | |
117 label="long name goes here. got it?").pack(side=LEFT, expand=1, | |
118 fill=BOTH) | |
119 | |
120 root.mainloop() |