-
Notifications
You must be signed in to change notification settings - Fork 0
/
TKApplication.py
251 lines (200 loc) · 6.89 KB
/
TKApplication.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
# TKApplication.py
import os, sys, re
import tkinter as tk
from tkinter import ttk, font as tkfont
from pprint import pprint
from turtle import TurtleScreen, RawTurtle
import smokesignal as ss
from myutils import isSeparator
from GUIApplication import GUIApplication
from PLLParser import parsePLL
from TreeNode import TreeNode
from TurtleNode import TurtleNode
from PythonNode import PythonNode
import TKWidgets
# --- Generally supported keys in hOptions:
# label - a text string
# text - user editable text
# handler - function to handle clicking
current_dir = os.getcwd() # current working directory
reWidgetDef = re.compile(r'^\s*(\S+)\s*(.*)$')
class TKApplication(GUIApplication):
def __init__(self, appDesc): # we probably don't need this
super().__init__(appDesc)
# ------------------------------------------------------------------------
def getMainWindow(self, appNode, hSubTrees, *, debug=False):
# --- base constructor will call this to create the main window
root = tk.Tk()
root.resizable(False, False)
root.title(appNode['label'])
root.option_add('*tearOff', False) # no tearoff menus
# --- Set up the menu bar, if any
menuBarNode = hSubTrees.get('menubar', None)
if menuBarNode:
self.addMenuBar(root, menuBarNode)
# --- layout window content, if any
layoutNode = hSubTrees.get('layout', None)
if layoutNode:
self.addLayout(root, layoutNode)
return root # will become self.mainWindow
# ---------------------------------------------------------------------------
def run(self):
tk.mainloop()
# ---------------------------------------------------------------------------
def exit(self):
self.mainWindow.destroy()
# ------------------------------------------------------------------------
def addMenuBar(
self,
window, # the window to add the menubar to, or None
node, # tree description of the menu
*, # --- following arguments must be called by name
debug=False):
assert isinstance(node, TreeNode)
if debug:
node.printTree("Menu Bar")
self.menubar = tk.Menu(window)
for subtree in node.trueChildren():
self.addMenu(self.menubar, subtree)
# --- Set as the window's menu bar
window['menu'] = self.menubar
# ------------------------------------------------------------------------
def addMenu(
self,
parent,
node,
*,
debug=False):
assert isinstance(node, TreeNode)
label = node['label']
hOptions = node.getOptions()
emitName = hOptions.get('emit', None)
isSep = isSeparator(label, '-')
if debug:
print(f"CALL addMenu('{label}')")
if node.hasTrueChildren():
if debug:
print(f"...has children")
if isSep:
raise Exception("separator cannot have children")
menu = tk.Menu(parent)
parent.add_cascade(menu=menu, label=label)
for child in node.trueChildren():
self.addMenu(menu, child)
elif isSep:
if debug:
print(f"...is separator")
parent.add_separator()
else:
if debug:
print(f"...emitName = '{emitName}'")
if emitName:
# --- All handlers will expect an "event" argument,
# so we just pass None here
handler = lambda *args, **kwargs: (
ss.emit(emitName, None, *args, **kwargs)
)
else:
handler = lambda *args, **kwargs: (
print(f"{type} '{label}' activated")
)
# handler = TKWidgets.getHandler('Menu Item', label, hOptions)
assert handler and callable(handler)
# handler = lambda: ss.emit(emitName)
parent.add_command(label=label, command=handler)
# ------------------------------------------------------------------------
def addLayout(
self,
window, # the window to add content to, or None
node, # tree description of the window content
*, # --- following arguments must be called by name
debug=False):
if debug:
node.printTree("Layout")
child = node.firstChild
if child:
label = child['label']
if label == 'row':
self.addRow(window, child, debug=debug)
elif label == 'col':
self.addCol(window, child, debug=debug)
else:
raise Exception(f"Bad layout label: '{label}'")
# ------------------------------------------------------------------------
def addRow(self, window, rowNode, *, debug=False):
if debug:
rowNode.printTree('Row')
# --- We already know that rowNode's label is 'row'
r, c = 0, 0
for child in rowNode.children():
label = child['label']
if isSeparator(label, '-'):
c += 1
elif isSeparator(label, '='):
r += 1
c = 0
else:
if label == 'row':
widget = self.newWidget(window, 'frame', debug=debug)
self.addRow(widget.tkWidget, child)
elif label == 'col':
widget = self.newWidget(window, 'frame', debug=debug)
self.addCol(widget.tkWidget, child)
else:
widget = self.widgetFromNode(window, child)
widget.grid(r, c)
c += 1
# ------------------------------------------------------------------------
def addCol(self, window, colNode, *, debug=False):
if debug:
colNode.printTree('Col')
# --- We already know that rowNode's label is 'col'
r, c = 0, 0
for child in colNode.children():
label = child['label']
if isSeparator(label, '-'):
r += 1
elif isSeparator(label, '='):
c += 1
r = 0
else:
if label == 'row':
widget = self.newWidget(window, 'frame', debug=debug)
self.addRow(widget.tkWidget, child)
elif label == 'col':
widget = self.newWidget(window, 'frame', debug=debug)
self.addCol(widget.tkWidget, child)
else:
widget = self.widgetFromNode(window, child)
widget.grid(r, c)
r += 1
# ------------------------------------------------------------------------
def centerWindow(self, window=None):
if not window:
window = self.mainWindow
window.update_idletasks()
# Gets the requested values of the height and width.
windowWidth = window.winfo_reqwidth()
windowHeight = window.winfo_reqheight()
# Gets both half the screen width/height and window width/height
positionRight = int(window.winfo_screenwidth()/2 - windowWidth/2)
positionDown = int(window.winfo_screenheight()/2 - windowHeight/2)
# --- Positions the window in the center of the page.
window.geometry("+{}+{}".format(positionRight, positionDown))
# ------------------------------------------------------------------------
def getWidgetConstructor(self, type):
return {
'frame': TKWidgets.FrameWidget,
'label': TKWidgets.LabelWidget,
'button': TKWidgets.ButtonWidget,
'editfield': TKWidgets.EditFieldWidget,
'checkbox': TKWidgets.CheckBoxWidget,
'radiobutton': TKWidgets.RadioButtonWidget,
'canvas': TKWidgets.CanvasWidget,
'editor': TKWidgets.ProgramEditorWidget,
'turtle': TKWidgets.TurtleWidget,
'notebook': TKWidgets.NotebookWidget,
}.get(type, None)
# ---------------------------------------------------------------------------
# Unit Tests
# ---------------------------------------------------------------------------