-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkwad.py
79 lines (59 loc) · 2 KB
/
kwad.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
'''
This module extends the Kivy library's Widget class with a method
that can be used to quickly draw rectangles around widgets to
see the space they are using. This is helpful for debugging
layout design errors
'''
__author__ = 'Mika Kunnas'
__version__ = '1.0.4'
from kivy.uix.widget import Widget
from kivy.graphics.vertex_instructions import Rectangle
from kivy.graphics.context_instructions import Color
class InvalidColorError(Exception):
pass
def show_area(self, color='green', alpha=0.5, group=None):
'''
Draws a rectangle on the widget's canvas
to display its position and proportions.
'''
max_ = 255.0
color_names = {'blue': [0, 0, 1],
'cyan': [0, 1, 1],
'fuchsia': [1, 0, 1],
'green': [0, 1, 0],
'orange': [1, 165/max_, 0],
'pink': [1, 192/max_, 203/max_],
'red': [1, 0, 0],
'yellow': [1, 1, 0],
}
rgba = None
if type(color) == str:
for name in color_names:
if name.startswith(color):
rgba = color_names[name]
break
try:
rgba += [alpha]
except TypeError:
raise InvalidColorError('No color match for color string "' + color + '"')
elif isinstance(color, (list, tuple)):
if len(color) == 4:
rgba = color
else:
rgba = list(color) + [alpha]
attr = self.canvas
if group == 'after':
attr = self.canvas.after
elif group == 'before':
attr = self.canvas.before
with attr:
Color(*rgba)
self.area = Rectangle(pos=self.pos, size=self.size)
self.bind(pos=self.update_area)
self.bind(size=self.update_area)
def update_area(self, *args):
self.area.pos = self.pos
self.area.size = self.size
def attach():
Widget.update_area = update_area
Widget.show_area = show_area