-
Notifications
You must be signed in to change notification settings - Fork 1
/
opaque.py
265 lines (235 loc) · 6.42 KB
/
opaque.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
import Tkinter as tk
class BaseDraw:
def __init__(self, **kws):
self.info = kws
self.pos = kws.pop('pos')
self.canvas = kws.pop('canvas')
self.width = kws.pop('width', None)
self.height = kws.pop('height', None)
self.name = kws.pop('name')
self.__tags = self.generate_tags(self.name)
self.styles = kws.pop('styles', {})
self.styles["normal"] = self.info
def switch_style(self, style_name):
'''
Switches the style of draw
'''
self.info = self.styles[style_name]
self.update()
def get_tags(self):
'''
Returns the 'private' tag list
'''
return self.__tags
def generate_tags(self, name):
'''
Returns a list with tags of the object
'''
raise NotImplementedError()
def update(self):
'''
Redraws the object
'''
raise NotImplementedError()
def bind(self, *args, **kws):
'''
Binds an event to draw
'''
for i in self.get_tags():
self.canvas.tag_bind(i, *args, **kws)
def delete(self):
'''
Deletes the draw
'''
for i in self.get_tags():
self.canvas.delete(i)
class Rectangle(BaseDraw):
'''
NW centered rectangle
options:
fill
outline
stipple
linewidth
'''
def __init__(self, **kws):
BaseDraw.__init__(self, **kws)
def generate_tags(self, name):
index = self.canvas.create_rectangle(
self.pos[0], self.pos[1],
self.pos[0]+self.width, self.pos[1]+self.height,
tag=name,
fill=self.info.get('fill') or "#000000",
outline=self.info.get('outline') or "#000000",
stipple=self.info.get('stipple') or '',
width=self.info.get('linewidth') or 1
)
return [index] if name is None else [name]
def update(self):
self.canvas.coords(
self.get_tags()[0],
self.pos[0], self.pos[1],
self.pos[0]+self.width, self.pos[1]+self.height
)
self.canvas.itemconfig(
self.get_tags()[0],
fill=self.info.get('fill') or "#000000",
outline=self.info.get('outline') or "#000000",
stipple=self.info.get('stipple') or '',
width=self.info.get('linewidth') or 1
)
class Circle(Rectangle):
'''
NW centered Oval
options:
fill
outline
stipple
linewidth
'''
def __init__(self, **kws):
Rectangle.__init__(self, **kws)
def set_radius(self, radius):
'''
Sets the radius of oval
'''
self.width = radius
self.height = radius
def generate_tags(self, name):
index = self.canvas.create_oval(
self.pos[0], self.pos[1],
self.pos[0]+self.width, self.pos[1]+self.height,
tag=name,
fill=self.info.get('fill') or "#000000",
outline=self.info.get('outline') or "#000000",
stipple=self.info.get('stipple') or '',
width=self.info.get('linewidth') or 1
)
return [index] if name is None else [name]
class Line(BaseDraw):
'''
NW centered line
options:
fill
stipple
linewidth
width and height ignored
'''
def __init__(self, **kws):
self.end_point = kws.get('end_point')
BaseDraw.__init__(self, **kws)
def generate_tags(self, name):
index = self.canvas.create_line(
self.pos[0], self.pos[1],
self.end_point[0], self.end_point[1],
tag=name,
fill=self.info.get('fill') or "#000000",
stipple=self.info.get('stipple') or '',
width=self.info.get('linewidth') or 1
)
return [index] if name is None else [name]
def update(self):
self.canvas.coords(
self.get_tags()[0],
self.pos[0], self.pos[1],
self.end_point[0], self.end_point[1]
)
self.canvas.itemconfig(
self.get_tags()[0],
fill=self.info.get('fill') or "#000000",
stipple=self.info.get('stipple') or '',
width=self.info.get('linewidth') or 1
)
class Text(BaseDraw):
'''
NW centered text
options:
fixme
'''
def __init__(self, **kws):
self.text = kws.pop('text')
BaseDraw.__init__(self, **kws)
def generate_tags(self, name):
index = self.canvas.create_text(
self.pos[0], self.pos[1],
tag=name,
fill=self.info.get('fill') or "#000000",
stipple=self.info.get('stipple') or '',
text=self.text, font=self.info.get('font'),
anchor=self.info.get('anchor')
)
return [index] if name is None else [name]
def update(self):
self.canvas.coords(
self.get_tags()[0],
self.pos[0], self.pos[1]
)
self.canvas.itemconfig(
self.get_tags()[0],
fill=self.info.get('fill') or "#000000",
font=self.info.get('font'),
anchor=self.info.get('anchor')
)
class OldWidget(BaseDraw):
def __init__(self, **kws):
self.widget = kws.pop('widget')
BaseDraw.__init__(self, **kws)
def generate_tags(self, name):
index = self.canvas.create_window(
self.pos[0], self.pos[1],
tag=name,
window=self.widget,
anchor=self.info.get('anchor')
)
return [index] if name is None else [name]
def bind(self, *args, **kws):
self.widget.bind(*args, **kws)
def update(self):
self.canvas.coords(
self.get_tags()[0],
self.pos[0], self.pos[1]
)
self.canvas.itemconfig(
self.get_tags()[0],
anchor=self.info.get('anchor'),
window=self.widget
)
def lerp(a,b,x):
return a + ((b-a)*x)
def get_x(a,b,lerp_result):
return (lerp_result-a) / (float(b)-a)
def get_rgb(r,g,b):
return "#%02x%02x%02x" % (r,g,b)
class Gradient(BaseDraw):
def __init__(self, **kws):
BaseDraw.__init__(self, **kws)
def generate_tags(self, name):
indexes = []
Dcolor = [i/256 for i in self.canvas.winfo_rgb(self.info.get('downcolor'))]
Ucolor = [i/256 for i in self.canvas.winfo_rgb(self.info.get('upcolor'))]
for ny in range(self.pos[1],self.height+self.pos[1]):
xlerp = get_x(self.pos[1], self.pos[1] + self.height, ny)
ncolor = [int(lerp(Ucolor[i], Dcolor[i], xlerp)) for i in range(3)]
indexes.append(self.canvas.create_line(self.pos[0],ny,self.pos[0]+self.width,ny, fill=get_rgb(*ncolor),tag=name))
if self.info.get('outline'):
indexes.append(self.canvas.create_rectangle(self.pos[0],self.pos[1],self.pos[0]+self.width, self.pos[1]+self.height, outline=self.info.get('outline'), tag=[name,name+'outline']))
if name:
return [name, "outline"+name]
else:
return indexes
def update(self):
self.delete()
self.generate_tags(self.name)
if __name__ == "__main__":
top = tk.Tk()
top.bind('<Escape>', lambda e:top.quit(), "+")
canvas = tk.Canvas(top, bg="white", bd=0, highlightthickness=0, width=640,height=480)
canvas.grid()
rec = Gradient(pos=[100,0],width=10,height=100,canvas=canvas,name="obj1",upcolor="#eee",downcolor="#ddd",styles={"light":{"upcolor":"#00aacc", "downcolor":"#00bbdd"}})
rec.pos[0] += 100
rec.update()
rec.pos[0] += 100
rec.update()
rec.bind('<Motion>', lambda e : rec.switch_style('light'), "+")
rec.bind('<Leave>', lambda e : rec.switch_style('normal'), "+")
top.mainloop()