38004400e2cba3491167f6ddbc5b5f08a349fada
1 """These widgets are all grouped together because they are non-interactive widgets.
9 # Turns a descriptive string or a tuple into a pygame color
10 def parse_color(desc
):
14 elif (desc
and desc
[0] == "#"):
15 # Because of a bug in pygame 1.8.1 we need to explicitly define the
16 # alpha value otherwise it will default to transparent.
19 return pygame
.Color(desc
)
21 # Determines if the given object is a pygame-compatible color or not
23 # In every version of pygame (up to 1.8.1 so far) will interpret
25 if (type(col
) == tuple):
27 if (hasattr(pygame
, "Color") and type(pygame
.Color
) == type):
28 # This is a recent version of pygame that uses a proper type
29 # instance for colors.
30 return (isinstance(col
, pygame
.Color
))
31 # Otherwise, this version of pygame only supports tuple colors
34 class Spacer(widget
.Widget
):
37 <pre>Spacer(width,height)</pre>
40 def __init__(self
,width
,height
,**params
):
41 params
.setdefault('focusable',False)
42 widget
.Widget
.__init
__(self
,width
=width
,height
=height
,**params
)
45 class Color(widget
.Widget
):
48 <p>The color can be changed at run-time.</p>
50 <pre>Color(value=None)</pre>
52 <strong>Example</strong>
61 def __init__(self
,value
=None,**params
):
62 params
.setdefault('focusable',False)
63 if value
!= None: params
['value']=value
64 widget
.Widget
.__init
__(self
,**params
)
67 if hasattr(self
,'value'): s
.fill(self
.value
)
69 def __setattr__(self
,k
,v
):
70 if k
== 'value' and type(v
) == str:
72 _v
= self
.__dict
__.get(k
,NOATTR
)
74 if k
== 'value' and _v
!= NOATTR
and _v
!= v
:
78 class Label(widget
.Widget
):
81 <pre>Label(value)</pre>
84 <dt>value<dd>text to be displayed
87 <strong>Example</strong>
89 w = Label(value="I own a rubber chicken!")
91 w = Label("3 rubber chickens")
94 def __init__(self
,value
,**params
):
95 params
.setdefault('focusable',False)
96 params
.setdefault('cls','label')
97 widget
.Widget
.__init
__(self
,**params
)
99 self
.font
= self
.style
.font
100 self
.style
.width
, self
.style
.height
= self
.font
.size(self
.value
)
103 s
.blit(self
.font
.render(self
.value
, 1, self
.style
.color
),(0,0))
105 class Image(widget
.Widget
):
108 <pre>Image(value)</pre>
111 <dt>value<dd>a file name or a pygame.Surface
115 def __init__(self
,value
,**params
):
116 params
.setdefault('focusable',False)
117 widget
.Widget
.__init
__(self
,**params
)
118 if type(value
) == str: value
= pygame
.image
.load(value
)
120 ow
,oh
= iw
,ih
= value
.get_width(),value
.get_height()
121 sw
,sh
= self
.style
.width
,self
.style
.height
130 if (ow
,oh
) != (iw
,ih
):
131 value
= pygame
.transform
.scale(value
,(iw
,ih
))
132 self
.style
.width
,self
.style
.height
= iw
,ih
136 s
.blit(self
.value
,(0,0))