-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathui_generics.py
459 lines (341 loc) · 14.2 KB
/
ui_generics.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
import tkinter as tk
import tkinter.filedialog
from tkinter import ttk
import os
def validate_int(p):
if str.isdigit(p) or p == '':
return True
else:
return False
def validate_float(p):
if p == '':
return True
else:
try:
float(p)
return True
except ValueError:
return False
class CheckBox(tk.Frame):
bool_variable = None
callback_function = None
button = None
def __init__(self, text, callback, master=None):
super().__init__()
tk.Frame.__init__(self, master)
self.callback_function = callback
self.bool_variable = tk.BooleanVar()
self.button = tk.Checkbutton(self, text=text, variable=self.bool_variable, command=self.state_callback,
onvalue=True, offvalue=False)
self.button.grid(column=0, row=0, sticky='news')
def state_callback(self):
if self.callback_function is not None:
self.callback_function(self.get_value())
def get_value(self) -> bool:
return self.bool_variable.get()
def set_value(self, value):
self.bool_variable.set(value)
if self.callback_function is not None:
self.callback_function(self.get_value())
class ScrollableListbox(tk.LabelFrame):
data = None
listbox = None
scrollbar = None
onclick_callback = None
def __init__(self, label, master=None):
super().__init__()
tk.LabelFrame.__init__(self, master, text=label)
self.listbox = tk.Listbox(self)
self.listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
self.scrollbar = tk.Scrollbar(self)
self.scrollbar.pack(side=tk.RIGHT, fill=tk.BOTH)
self.listbox.config(yscrollcommand=self.scrollbar.set)
self.scrollbar.config(command=self.listbox.yview)
def clear(self):
self.listbox.delete(0, tk.END)
def get_list_length(self):
return self.listbox.size()
def set_data(self, data):
self.data = data
for item in self.data:
self.listbox.insert(tk.END, item[0])
def bind_onclick(self, callback):
self.onclick_callback = callback
self.listbox.bind('<<ListboxSelect>>', callback)
def get_widget(self):
return self.listbox
class LabelEntryFileBrowse(tk.LabelFrame):
text_variable = None
last_path = ''
filetypes = None
def __init__(self, label, master=None, callback=None, filetypes=None):
super().__init__()
tk.LabelFrame.__init__(self, master, text=label)
self.callback_f = callback
self.filetypes = filetypes
self.text_variable = tk.StringVar()
self.columnconfigure(0, weight=3)
self.columnconfigure(1, weight=1)
entry = tk.Entry(self, textvariable=self.text_variable)
entry.grid(column=0, row=0, sticky='news')
button = tk.Button(self, text='Browse', command=self.open_browse_dialogue)
button.grid(column=1, row=0, sticky='news')
def open_browse_dialogue(self):
if self.last_path == '':
initialpath = os.path.expanduser('~')
else:
initialpath = self.last_path
if self.filetypes:
path = tk.filedialog.askopenfilename(filetypes=self.filetypes, initialdir=initialpath)
else:
path = tk.filedialog.askopenfilename(initialdir=initialpath)
if path:
self.last_path = path
self.text_variable.set(path)
if self.callback_f is not None:
self.callback_f(self.last_path)
def get_value(self):
return self.text_variable.get()
def set_value(self, value):
self.text_variable.set(value)
self.last_path = value
def clear(self):
self.text_variable.set('')
self.last_path = ''
class LabelEntryFolderBrowse(tk.LabelFrame):
text_variable = None
last_path = ''
callback_f = None
def __init__(self, label, master=None, callback=None):
super().__init__()
tk.LabelFrame.__init__(self, master, text=label)
self.callback_f = callback
self.text_variable = tk.StringVar()
self.columnconfigure(0, weight=3)
self.columnconfigure(1, weight=1)
entry = tk.Entry(self, textvariable=self.text_variable)
entry.grid(column=0, row=0, sticky='news')
button = tk.Button(self, text='Browse', command=self.open_browse_dialogue)
button.grid(column=1, row=0, sticky='news')
def open_browse_dialogue(self):
if self.last_path == '':
initialpath = os.path.expanduser('~')
else:
initialpath = self.last_path
path = tk.filedialog.askdirectory(initialdir=initialpath)
if path:
self.last_path = path
self.text_variable.set(path)
if self.callback_f is not None:
self.callback_f(self.last_path)
def get_value(self):
return self.text_variable.get()
def set_value(self, value):
self.text_variable.set(value)
self.last_path = value
def clear(self):
self.text_variable.set('')
self.last_path = ''
class LabelEntryText(tk.LabelFrame):
text_variable = None
entry = None
def __init__(self, label, master=None):
tk.LabelFrame.__init__(self, master, text=label)
self.text_variable = tk.StringVar()
self.rowconfigure(0, weight=1)
self.columnconfigure(0, weight=1)
self.entry = tk.Entry(self, textvariable=self.text_variable)
self.entry.grid(column=0, row=0, sticky='news')
def get_value(self):
return self.text_variable.get()
def set_value(self, text):
self.text_variable.set(text)
def clear(self):
self.text_variable.set('')
def enable(self):
self.entry.config(state='normal')
def disable(self):
self.entry.config(state='disabled')
class LabelEntryInt(tk.LabelFrame):
text_variable = None
entry = None
def __init__(self, label, master=None, initial_value=0):
tk.LabelFrame.__init__(self, master, text=label)
self.text_variable = tk.StringVar()
self.text_variable.set(str(initial_value))
self.columnconfigure(0, weight=1)
vcmd = (self.register(validate_int))
self.entry = tk.Entry(self, textvariable=self.text_variable, validate='all',
validatecommand=(vcmd, '%P'))
self.entry.grid(column=0, row=0, sticky='news', padx=5, pady=5)
def get_value(self):
return int(self.text_variable.get())
def set_value(self, value):
self.text_variable.set(str(value))
def enable(self):
self.entry.config(state='normal')
def disable(self):
self.entry.config(state='disabled')
class LabelEntryFloat(tk.LabelFrame):
text_variable = None
entry = None
def __init__(self, label, master=None, initial_value=0.0):
tk.LabelFrame.__init__(self, master, text=label)
self.text_variable = tk.StringVar()
self.text_variable.set(str(initial_value))
self.columnconfigure(0, weight=1)
vcmd = (self.register(validate_float))
self.entry = tk.Entry(self, textvariable=self.text_variable, validate='all',
validatecommand=(vcmd, '%P'))
self.entry.grid(column=0, row=0, sticky='news')
def enable(self):
self.entry.config(state='normal')
def disable(self):
self.entry.config(state='disabled')
def get_value(self):
return float(self.text_variable.get())
def set_value(self, value):
self.text_variable.set(str(value))
class SingleLineConsole(tk.Frame):
console_label = None
console_stringvar = None
def __init__(self, master=None):
super().__init__()
tk.Frame.__init__(self, master)
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.init_ui()
def init_ui(self):
self.console_stringvar = tk.StringVar()
self.console_label = tk.Label(self, textvariable=self.console_stringvar, bg='black', fg='green')
self.console_label.pack(expand=True, fill=tk.BOTH)
def write_info(self, text):
self.console_stringvar.set('INFO: ' + text)
self.update_idletasks()
def write_error(self, text):
self.console_stringvar.set('ERROR: ' + text)
self.update_idletasks()
class Console(tk.LabelFrame):
console_listbox = None
console_scrollbar = None
def __init__(self, master=None, text='Console'):
super().__init__()
tk.LabelFrame.__init__(self, master, text=text)
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
self.init_ui()
def init_ui(self):
self.console_listbox = tk.Listbox(self, bg='black', fg='green', highlightcolor='black',
selectbackground='green', activestyle=tk.NONE)
self.console_scrollbar = tk.Scrollbar(self.console_listbox)
self.console_listbox.config(yscrollcommand=self.console_scrollbar.set)
self.console_scrollbar.config(command=self.console_listbox.yview)
self.console_scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
self.console_listbox.pack(expand=True, fill=tk.BOTH)
def write_info(self, text):
self.console_listbox.insert(tk.END, 'INFO: ' + text)
self.console_listbox.yview(tk.END)
self.update_idletasks()
def write_error(self, text):
self.console_listbox.insert(tk.END, 'ERROR: ' + text)
self.console_listbox.yview(tk.END)
self.update_idletasks()
class ListboxWithControls(tk.Frame):
listbox = None
up_cmd = None
down_cmd = None
add_cmd = None
remove_cmd = None
clear_cmd = None
process_cmd = None
item_selected_cmd = None
def __init__(self, master=None, title='', up_cmd=None, down_cmd=None, add_cmd=None, remove_cmd=None, clear_cmd=None,
process_cmd=None, item_selected_cmd=None):
super().__init__()
tk.Frame.__init__(self, master)
self.up_cmd = up_cmd
self.down_cmd = down_cmd
self.add_cmd = add_cmd
self.remove_cmd = remove_cmd
self.clear_cmd = clear_cmd
self.process_cmd = process_cmd
self.item_selected_cmd = item_selected_cmd
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
main_frame = tk.Frame(self) if (title == '') else tk.LabelFrame(self, text=title)
main_frame.grid(column=0, row=0, sticky='news')
main_frame.columnconfigure(0, weight=1)
main_frame.rowconfigure(0, weight=1)
self.listbox = tk.Listbox(main_frame, selectmode=tk.SINGLE)
self.listbox.grid(column=0, row=0, sticky='news')
button_frame = tk.Frame(main_frame)
button_frame.grid(column=0, row=1, sticky='news')
button_frame.columnconfigure(0, weight=1)
button_frame.columnconfigure(1, weight=1)
button_frame.columnconfigure(2, weight=1)
button_frame.rowconfigure(0, weight=1)
button_frame.rowconfigure(1, weight=1)
if self.up_cmd is not None:
up_button = tk.Button(button_frame, text='UP', command=self.up_cmd)
up_button.grid(column=0, row=0, sticky='news')
if self.down_cmd is not None:
down_button = tk.Button(button_frame, text='DOWN', command=self.down_cmd)
down_button.grid(column=0, row=1, sticky='news')
if self.add_cmd is not None:
add_button = tk.Button(button_frame, text='ADD', command=self.add_cmd)
add_button.grid(column=1, row=0, sticky='news')
if self.remove_cmd is not None:
remove_button = tk.Button(button_frame, text='REMOVE', command=self.remove_cmd)
remove_button.grid(column=1, row=1, sticky='news')
if self.clear_cmd is not None:
clear_button = tk.Button(button_frame, text='CLEAR', command=self.clear_cmd)
clear_button.grid(column=2, row=0, sticky='news')
if self.process_cmd is not None:
process_button = tk.Button(button_frame, text='PROCESS', command=self.process_cmd)
process_button.grid(column=2, row=1, sticky='news')
if self.item_selected_cmd is not None:
self.listbox.bind('<<ListboxSelect>>', self.item_selected_cmd)
def update(self, data=None):
if data is None:
data = []
self.listbox.delete(0, tk.END)
for i in data:
self.listbox.insert(tk.END, i)
def get_selected_item(self):
return self.listbox.get(self.listbox.curselection())
class ComboboxWithDetails(tk.Frame):
combobox = None
console = None
current_selected_item = None
item_list = None
get_item_details_frame_cmd = None
details_frame = None
current_frame = None
def __init__(self, master=None, text='', console=None, item_list=None, get_item_details_frame_cmd=None):
super().__init__()
tk.Frame.__init__(self, master)
self.console = console
self.item_list = item_list
self.get_item_details_frame_cmd = get_item_details_frame_cmd
self.columnconfigure(0, weight=1)
self.rowconfigure(1, weight=1)
self.current_selected_item = tk.StringVar()
self.current_selected_item.set(self.item_list[0])
self.combobox = ttk.Combobox(self, textvariable=self.current_selected_item)
self.combobox['values'] = self.item_list
self.combobox['state'] = 'readonly'
self.combobox.bind('<<ComboboxSelected>>', self.set_details_frame)
self.combobox.grid(column=0, row=0, sticky='news')
self.details_frame = tk.Frame(self) if (text == '') else tk.LabelFrame(self, text=text)
self.details_frame.grid(column=0, row=1, sticky='news')
self.set_details_frame(self.current_selected_item.get())
return
def set_details_frame(self, event):
new_frame = self.get_item_details_frame_cmd(self.details_frame, self.current_selected_item.get())
if self.current_frame is not None:
self.current_frame.destroy()
self.current_frame = new_frame
self.current_frame.grid(column=0, row=0, sticky='news', padx=5, pady=5)
return
def get_details_data(self):
return