-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtktarget.py
More file actions
241 lines (186 loc) · 6.73 KB
/
tktarget.py
File metadata and controls
241 lines (186 loc) · 6.73 KB
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
import tkinter as tk
from tkinter import ttk
from utils import HTMLBuilder, ImmutableDict
class TkinterTarget:
"""
Core class to wrap Tkinter functionality and translate between an HTMLBuilder and
tkinter code.
"""
_REGISTERED_TAGS = {}
""" Dictionary of registered tags with their associated handlers """
@classmethod
def component(cls, comp_class):
"""
Decorator to register a class as a component tag with the same
name as the class.
NOTE: Class and tag names are converted to lowercase to match python's
HTMLParser implementation.
"""
if not issubclass(comp_class, Component):
raise TypeError("Component classes must inherit from Component")
cls._REGISTERED_TAGS[comp_class.__name__.lower()] = comp_class
return comp_class
@classmethod
def component_tree_from_html(cls, html: HTMLBuilder):
"""
Generate a component tree from an HTMLBuilder object.
"""
return cls.component_from_tag(html.root)
@classmethod
def component_from_tag(cls, tag):
""" Generate a component from a tag """
if tag.name in cls._REGISTERED_TAGS:
return cls._REGISTERED_TAGS[tag.name](tag)
else:
raise ValueError(f"Tag '{tag.name}' not registered")
@classmethod
def basic(cls):
""" Convenience method to create a basic TkinterTarget """
root = tk.Tk()
return cls(root)
@classmethod
def main(cls, **kwargs):
""" Convenience method to create a TkinterTarget with a 'main' component """
root = tk.Tk()
tktarget = cls(root)
main = cls.component_tree_from_html(
HTMLBuilder().tag("main", **kwargs).pop()
)
tktarget.mount_to_root(component=main)
return tktarget
def __init__(self, root: tk.Tk, **kwargs):
self.root = root
self.options = kwargs
def mount_to_root(self, component):
""" Mount a component as the top-level frame of the Tkinter window """
component.component_will_mount(self.root)
component.mount(self.root)
component.component_did_mount(self.root)
def start(self):
""" Start the Tkinter window """
self.root.mainloop()
def render(self, html: HTMLBuilder):
"""
Render an HTMLBuilder object into a Tkinter window.
"""
self._render(html.root, self.root)
return self
def _render(self, tag, parent=None):
""" Render a tag """
if parent is None:
parent = self.root
if tag.name in self._REGISTERED_TAGS:
comp = self._REGISTERED_TAGS[tag.name](tag)
return comp.render(parent, **tag.kwargs)
else:
raise ValueError(f"Tag '{tag.name}' not registered")
class Component:
""" Base class for all components """
@classmethod
def get_initial_state(cls):
""" Get the initial state of the component """
return {}
def __init__(self, tag):
self.props = ImmutableDict({
**tag.kwargs,
})
self.state = ImmutableDict({
**self.get_initial_state(),
})
self.tag = tag
self.children = [
TkinterTarget.component_from_tag(child)
for child in tag.children
]
self.render_root = None
self.widget = None
self.parent_widget = None
def change_props(self, **props):
""" Change the component's props """
old = self.props
self.props = ImmutableDict({
**props,
})
if self.props != old:
self.update()
self.component_did_update()
def set_state(self, update):
""" Update the component's state """
old = self.state
self.state = ImmutableDict({
**self.state,
**update,
})
if old != self.state:
self.update()
self.component_did_update()
def component_will_mount(self, widget):
pass
def component_did_mount(self, widget):
pass
def component_will_unmount(self):
pass
def component_did_update(self):
pass
def mount(self, widget):
""" Mount the component """
self.parent_widget = widget
self.render_root = TkinterTarget.component_tree_from_html(self.render())
self.render_root.component_will_mount(widget)
self.widget = self.render_root.mount(widget)
self.render_root.component_did_mount(widget)
for child in self.children:
child.component_will_mount(self.widget)
child.mount(self.widget)
child.component_did_mount(self.widget)
return self.widget
def update(self):
""" Update the component """
if self.widget is None:
return
self.render_root.component_will_unmount()
self.render_root = TkinterTarget.component_tree_from_html(self.render())
self.widget.destroy()
self.render_root.component_will_mount(self.parent_widget)
self.widget = self.render_root.mount(self.parent_widget)
self.render_root.component_did_mount(self.parent_widget)
def render(self) -> HTMLBuilder:
""" Render the component to an HTMLBuilder """
raise NotImplementedError("Components must implement render()")
class BaseComponent(Component):
"""
Base components are components that wrap a Tkinter widget;
they do not have a 'render' method and instead mount directly.
"""
def get_widget(self, widget):
""" Get the widget to render """
raise NotImplementedError("BaseComponents must implement get_widget()")
def render(self):
raise TypeError("Base components do not implement render()")
def mount(self, widget):
""" Mount the component """
self.widget = self.get_widget(widget)
for child in self.children:
child.component_will_mount(self.widget)
child.mount(self.widget)
child.component_did_mount(self.widget)
self.widget.pack()
return self.widget
@TkinterTarget.component
class Frame(BaseComponent):
""" Frame component """
def get_widget(self, widget):
""" Mount the component """
return ttk.Frame(widget, **self.props)
@TkinterTarget.component
class Label(BaseComponent):
""" Label component """
def get_widget(self, widget):
""" Mount the component """
return ttk.Label(widget, **self.props)
@TkinterTarget.component
class Button(BaseComponent):
""" Button component """
def get_widget(self, widget):
""" Mount the component """
return ttk.Button(widget, **self.props)