8
from withtk import HasProperties
11
def __init__(self, spec):
13
self.window = pyglet.window.Window(caption='withgui')
16
theme = kytten.Theme(os.path.join(os.getcwd(), 'theme'), override={
17
"gui_color": [128, 128, 128, 255],
20
top_frame = kytten.VerticalLayout([Implementation.create(child, self).kytten_widget
21
for child in spec.children])
23
self.dialog = kytten.Dialog(top_frame, window=self.window, theme=theme)
24
self.dialog.do_layout()
25
self.dialog.needs_layout = True
26
self.window.set_size(self.dialog.width, self.dialog.height)
28
self.window.push_handlers(self)
35
self.dialog.on_update(dt)
38
pyglet.clock.schedule(self.update)
41
def stop(self, data=None):
45
class Implementation(HasProperties):
46
'''Wrapper around a kytten widget allowing read/write access
47
to the widget's properties including its current value.
49
implementation_classes = {}
51
def register_class(cls, klass, name=None):
54
cls.implementation_classes[name] = klass
57
def create(cls, spec, parent):
58
klass = cls.implementation_classes[spec.__class__.__name__]
59
return klass(spec, parent)
61
def __init__(self, spec, parent):
65
self.kytten_widget = None
66
self.args = self.generate_properties(spec)
70
# set up event handlers
71
if 'on_mouse' in spec.event_handlers:
73
spec.event_handlers['on_mouse']
75
self.spec.implementation = self
77
def create_widget(self):
78
'''Create the underlying kytten_widget
80
raise NotImplementeError('create_widget must be implemented')
82
def set_value(self, value):
83
# TODO no value modification in kytten yet
86
return self.kytten_widget.get_value()
87
def set_property(self, name, value):
88
name, value = self.translate_value(name, value)
93
self.kytten_widget.x = value
94
self.kytten_widget.place(x=value)
96
self.kytten_widget.y = value
97
self.kytten_widget.place(y=value)
99
# TODO no property modification in kytten yet
101
def get_property(self, name):
103
return self.get_value()
105
return self.kytten_widget.x
107
return self.kytten_widget.y
109
return getattr(self.kytten_widget, name)
112
self.spec.implementation = None
116
self.kytten_widget.delete()
117
self.kytten_widget = None
119
class Frame(Implementation):
120
def create_widget(self):
122
width = self.args.get('width')
123
height = self.args.get('height')
125
image = self.args.get('image')
126
if image is not None:
128
width = image.width()
130
height = image.height()
132
self.kytten_widget = kytten.FreeLayout(width, height)
134
# XXX do somehing with image
136
for child in spec.children:
137
self.add_child(child)
139
def add_child(self, spec):
140
i = Implementation.create(child, self)
141
anchor = i.args['anchor']
144
self.kytten_widget.add(anchor, x, y, i.kytten_widget)
145
Implementation.register_class(Frame)
147
class Column(Implementation):
148
def create_widget(self):
149
self.kytten_widget = kytten.VerticalLayout([
150
Implementation.create(child, self).kytten_widget
151
for child in self.spec.children])
152
def add_child(self, spec):
153
i = Implementation.create(child, self)
154
self.add(i.kytten_widget)
155
Implementation.register_class(Column)
157
class Row(Implementation):
158
def create_widget(self):
159
self.kytten_widget = kytten.HorizontalLayout([
160
Implementation.create(child, self).kytten_widget
161
for child in self.spec.children])
162
def add_child(self, spec):
163
i = Implementation.create(child, self)
164
self.add(i.kytten_widget)
165
Implementation.register_class(Row)
167
class Label(Implementation):
168
def create_widget(self):
169
self.kytten_widget = kytten.Label(self.args['value'])
170
Implementation.register_class(Label)
172
class Button(Implementation):
173
def create_widget(self):
174
def click(spec=self.spec):
175
return spec.event_handlers['on_click'](spec)
176
self.kytten_widget = kytten.Button(self.args['value'], on_click=click)
177
Implementation.register_class(Button)
179
class Selection(Implementation):
181
options=('options', HasProperties.prop_passthrough),
182
value=('value', HasProperties.prop_passthrough),
184
def create_widget(self):
185
options = self.args['options']
186
value = self.args.get('value', options[0])
187
self.kytten_widget = kytten.Dropdown(options=options, selected=value)
188
Implementation.register_class(Selection)
190
class Text(Implementation):
191
def create_widget(self):
192
value = self.args.get('value', '')
193
self.kytten_widget = kytten.Input(text=text)
194
def set_value(self, value):
195
self.kytten_widget.set_text(value)
196
Implementation.register_class(Text)