-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtkinter_utils.py
1232 lines (1081 loc) · 56.4 KB
/
tkinter_utils.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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import _tkinter
import gc
import os.path
import pathlib
import threading
import time
import tkinter
import traceback
import webbrowser
from tkinter import TOP, W, N, NW, messagebox, END, ttk, filedialog, INSERT, NE
from tkinter.ttk import Style, Combobox
from tkinter.ttk import Treeview
import numpy as np
from PIL import ImageTk as itk
from github import Github
from logger_util import parse_log
from tkvideoutils import ImageLabel
from ui_params import treeview_default_tag_dict, cometrics_ver_root
from pat import survey
def center(toplevel, y_offset=-20):
toplevel.update_idletasks()
# Tkinter way to find the screen resolution
screen_width = toplevel.winfo_screenwidth()
screen_height = toplevel.winfo_screenheight()
size = tuple(int(_) for _ in toplevel.geometry().split('+')[0].split('x'))
x = screen_width / 2 - size[0] / 2
y = screen_height / 2 - size[1] / 2
y += y_offset
toplevel.geometry("+%d+%d" % (x, y))
def get_display_size():
root = tkinter.Tk()
root.update_idletasks()
root.attributes('-fullscreen', True)
root.state('iconic')
height = root.winfo_screenheight()
width = root.winfo_screenwidth()
root.attributes('-fullscreen', False)
root.deiconify()
root.focus_force()
return root, height, width
def get_slider_style(root):
fig_color = '#%02x%02x%02x' % (240, 240, 237)
style = ttk.Style(root)
style.theme_use('clam')
# self.style.element_create('Horizontal.Scale.trough', 'image', self.trough_img)
# create custom layout
style.layout('custom.Horizontal.TScale',
[('Horizontal.Scale.trough',
{'sticky': 'nswe',
'children': [('custom.Horizontal.Scale.slider',
{'side': 'left', 'sticky': ''})]})])
style.configure('custom.Horizontal.TScale', background=fig_color)
return 'custom.Horizontal.TScale', style
def get_treeview_style(name="mystyle.Treeview", font=('Purisa', 12), heading_font=('Purisa', 12, 'bold'),
rowheight=25, h_thickness=0, bd=0):
style = Style()
style.configure(name, highlightthickness=h_thickness, bd=bd, font=font) # Modify the font of the body
style.configure("mystyle.Treeview.Heading", font=heading_font) # Modify the font of the headings
style.configure('Treeview', rowheight=rowheight)
style.map('Treeview', foreground=fixed_map('foreground'), background=fixed_map('background'))
return style
def build_treeview(root, x, y, height, width, heading_dict, column_dict=None, selectmode='browse', t_height=18,
filescroll=True, button_1_bind=None, double_bind=None, button_3_bind=None, style="mystyle.Treeview",
anchor=NW, tag_dict=treeview_default_tag_dict, fs_offset=18):
treeview = Treeview(root, style=style, height=t_height, selectmode=selectmode)
treeview.place(x=x, y=y, height=height, width=width, anchor=anchor)
# Define header
heading = heading_dict["#0"]
treeview.heading("#0", text=heading[0], anchor=heading[1])
treeview.column("#0", width=heading[2], stretch=heading[3], anchor=heading[4])
# Define columns
if column_dict:
treeview["columns"] = list(column_dict.keys())
for col in column_dict:
treeview.heading(col, text=column_dict[col][0], anchor=column_dict[col][1])
treeview.column(col, width=column_dict[col][2], stretch=column_dict[col][3], anchor=column_dict[col][4])
for tag in tag_dict:
treeview.tag_configure(tag, background=tag_dict[tag])
if button_1_bind:
treeview.bind("<ButtonRelease-1>", button_1_bind)
if double_bind:
treeview.bind("<Double-Button-1>", double_bind)
if button_3_bind:
treeview.bind("<ButtonRelease-3>", button_3_bind)
if filescroll:
file_scroll = tkinter.Scrollbar(root, orient="vertical", command=treeview.yview)
file_scroll.place(x=(x - fs_offset), y=y, height=height, anchor=anchor)
treeview.configure(yscrollcommand=file_scroll.set)
else:
file_scroll = None
return treeview, file_scroll
def clear_treeview(widget: Treeview):
for children in widget.get_children():
widget.delete(children)
def scroll_to(widget: Treeview, selection):
widget.yview_moveto(int(selection) / (len(widget.get_children())))
def select_focus(widget: Treeview, selection):
selection = str(selection)
widget.selection_set(selection)
widget.focus(selection)
def fixed_map(option):
# https://stackoverflow.com/a/62011081
# Fix for setting text colour for Tkinter 8.6.9
# From: https://core.tcl.tk/tk/info/509cafafae
#
# Returns the style map for 'option' with any styles starting with
# ('!disabled', '!selected', ...) filtered out.
# style.map() returns an empty list for missing options, so this
# should be future-safe.
style = Style()
return [elm for elm in style.map('Treeview', query_opt=option) if
elm[:2] != ('!disabled', '!selected')]
class LoadingWindow:
def __init__(self, objects):
self.objects = objects
self.root = root = tkinter.Tk()
root.config(bd=-2)
root.overrideredirect(True)
root.geometry("250x100")
center(self.root, y_offset=0)
self.checkmark_img = tkinter.PhotoImage(file=os.path.join(os.getcwd(), r'images/checkmark.png'))
self.label_var = tkinter.StringVar(root, value='Cleaning up resources')
self.text_label = tkinter.Label(root, textvariable=self.label_var, font=('Purisa', 11))
self.label = tkinter.Label(root)
self.label.place(x=10, y=50, anchor=tkinter.W)
self.text_label.place(x=70, y=50, anchor=tkinter.W)
self.image_label = ImageLabel(os.path.join(os.getcwd(), r'images\loading.gif'), self.label, (100, 100))
root.after(500, self.update_text)
self.dots = 0
self.done = False
gc_thread = threading.Thread(target=self.perform_gc)
gc_thread.daemon = True
gc_thread.start()
root.mainloop()
def perform_gc(self):
del self.objects
gc.collect()
time.sleep(1)
self.image_label.unload()
self.image_label.label.config(image=self.checkmark_img)
time.sleep(1)
self.done = True
def update_text(self):
try:
self.dots += 1
if self.dots > 3:
self.dots = 0
self.label_var.set(f"Cleaning up resources{'.' * self.dots}")
if self.done:
self.close()
else:
self.root.after(500, self.update_text)
except _tkinter.TclError:
pass
def close(self):
self.image_label.unload()
self.label.destroy()
self.root.quit()
self.root.destroy()
class ExternalButtonPopup:
def __init__(self, root, caller, field_font=('Purisa', 12)):
self.caller = caller
self.key = None
self.popup_root = popup_root = tkinter.Toplevel(root)
popup_root.config(bg="white", bd=-2)
popup_root.geometry("300x200")
popup_root.title("External Button Setup")
key_tag = tkinter.Label(popup_root, text="Key", bg='white', font=('Purisa', 12))
key_tag.place(x=50, y=20)
self.key_entry = tkinter.Entry(popup_root, bd=2, width=17, font=('Purisa', 12))
self.key_entry.place(x=90, y=20)
freq_label = tkinter.Label(popup_root, font=field_font, text="Frequency Key Association", bg='white')
freq_label.place(x=50, y=50)
self.freq_var = tkinter.StringVar(popup_root)
freq_box = Combobox(popup_root, textvariable=self.freq_var, font=field_font)
freq_box['values'] = ['None'] + self.caller.ovu.key_view.bindings
freq_box['state'] = 'readonly'
freq_box.config(font=field_font)
freq_box.place(x=50, y=75, width=200)
freq_box.option_add('*TCombobox*Listbox.font', field_font)
dur_label = tkinter.Label(popup_root, font=field_font, text="Duration Key Association", bg='white')
dur_label.place(x=50, y=100)
self.dur_var = tkinter.StringVar(popup_root)
dur_box = Combobox(popup_root, textvariable=self.dur_var, font=field_font)
dur_box['values'] = ['None'] + self.caller.ovu.key_view.dur_bindings
dur_box['state'] = 'readonly'
dur_box.config(font=field_font)
dur_box.place(x=50, y=125, width=200)
dur_box.option_add('*TCombobox*Listbox.font', field_font)
ok_button = tkinter.Button(popup_root, text="OK", command=self.on_closing, font=('Purisa', 12))
ok_button.place(x=150, y=160, anchor=N)
def set_value(self, key):
self.key = key
self.key_entry.delete(0, END)
self.key_entry.insert(0, str(key))
def on_closing(self):
if self.key:
self.caller.ext_raw = self.key
if self.dur_var.get():
if self.dur_var.get() != 'None':
self.caller.ext_dur_val = self.dur_var.get()[0]
if self.freq_var.get():
if self.freq_var.get() != 'None':
self.caller.ext_freq_val = self.freq_var.get()[0]
self.caller.button_input_handler = None
self.popup_root.destroy()
class EditEventPopup:
def __init__(self, root, caller, dur_bindings, freq_bindings, selected_event, selection,
field_font=('Purisa', 12)):
self.caller = caller
self.selection = selection
self.selected_event = selected_event
self.dur_bindings, self.freq_bindings = dur_bindings, freq_bindings
self.key = None
self.popup_root = popup_root = tkinter.Toplevel(root)
popup_root.config(bg="white", bd=-2)
popup_root.geometry("300x200")
popup_root.title("Edit Event")
center(popup_root)
freq_label = tkinter.Label(popup_root, font=field_font, text="Frequency Key Association", bg='white')
freq_label.place(x=50, y=20)
self.freq_var = tkinter.StringVar(popup_root)
freq_box = Combobox(popup_root, textvariable=self.freq_var, font=field_font)
freq_box['values'] = ['None'] + freq_bindings
freq_box['state'] = 'readonly'
freq_box.config(font=field_font)
freq_box.place(x=50, y=45, width=200)
freq_box.option_add('*TCombobox*Listbox.font', field_font)
dur_label = tkinter.Label(popup_root, font=field_font, text="Duration Key Association", bg='white')
dur_label.place(x=50, y=70)
self.dur_var = tkinter.StringVar(popup_root)
dur_box = Combobox(popup_root, textvariable=self.dur_var, font=field_font)
dur_box['values'] = ['None'] + dur_bindings
dur_box['state'] = 'readonly'
dur_box.config(font=field_font)
dur_box.place(x=50, y=95, width=200)
dur_box.option_add('*TCombobox*Listbox.font', field_font)
if type(selected_event[1]) is list:
freq_val, dur_val = self.get_key_binding(selected_event)
self.freq_var.set(freq_val)
self.dur_var.set(str(dur_val[0]) + " {" + str(dur_val[1]) + "}")
freq_box['state'] = 'disabled'
else:
freq_val, dur_val = self.get_key_binding(selected_event)
self.freq_var.set(str(freq_val[0]) + " {" + str(freq_val[1]) + "}")
self.dur_var.set(dur_val)
dur_box['state'] = 'disabled'
ok_button = tkinter.Button(popup_root, text="OK", command=self.on_closing, font=field_font)
ok_button.place(x=150, y=130, anchor=N)
def get_key_binding(self, event):
if type(event[1]) is list:
for bind in self.dur_bindings:
if event[0] == bind[1]:
return 'None', bind
else:
for bind in self.freq_bindings:
if event[0] == bind[1]:
return bind, 'None'
messagebox.showerror("Error", f"Something was wrong with selected event!\n{event}")
return 'None', 'None'
def on_closing(self):
if self.dur_var.get() != 'None' and self.freq_var.get() != 'None':
messagebox.showerror("Error", "Event cannot have two tags! Set either duration or frequency to 'None'!")
self.popup_root.focus_force()
return
elif self.dur_var.get() == 'None' and self.freq_var.get() == 'None':
messagebox.showerror("Error", "Event must have a value! Set either duration or frequency to an event!")
self.popup_root.focus_force()
return
elif self.dur_var.get() != 'None':
self.selected_event[0] = ' '.join(self.dur_var.get().split(' ')[1:]).replace('{', '').replace('}', '')
elif self.freq_var.get() != 'None':
self.selected_event[0] = ' '.join(self.freq_var.get().split(' ')[1:]).replace('{', '').replace('}', '')
self.caller.popup_return(self.selection, self.selected_event)
self.popup_root.destroy()
class ConfigPopup:
def __init__(self, root, config):
self.config = config
self.popup_root = popup_root = tkinter.Toplevel(root)
popup_root.config(bg="white", bd=-2)
popup_root.geometry("300x450")
popup_root.title("Configuration Settings")
fps_tag = tkinter.Label(popup_root, text="FPS", bg='white', font=('Purisa', 12))
fps_tag.place(x=10, y=10)
self.fps_entry = tkinter.Entry(popup_root, bd=2, width=5, font=('Purisa', 12))
self.fps_entry.insert(0, int(self.config.get_fps()))
self.fps_entry.place(x=60, y=10)
self.e4_var = tkinter.BooleanVar(popup_root, value=self.config.get_e4())
e4_checkbutton = tkinter.Checkbutton(popup_root, text="Show E4 Tab", bg='white', variable=self.e4_var,
font=('Purisa', 12))
e4_checkbutton.place(x=10, y=50)
self.woodway_var = tkinter.BooleanVar(popup_root, value=self.config.get_woodway())
woodway_checkbutton = tkinter.Checkbutton(popup_root, text="Show Woodway Tab", bg='white',
variable=self.woodway_var, font=('Purisa', 12))
woodway_checkbutton.place(x=10, y=80)
self.ble_var = tkinter.BooleanVar(popup_root, value=self.config.get_ble())
ble_checkbutton = tkinter.Checkbutton(popup_root, text="Show BLE Tab", bg='white',
variable=self.ble_var, font=('Purisa', 12))
ble_checkbutton.place(x=10, y=110)
self.click_var = tkinter.BooleanVar(popup_root, value=self.config.get_clickmode())
clk_checkbutton = tkinter.Checkbutton(popup_root, text="Enable Single Click", bg='white',
variable=self.click_var, font=('Purisa', 12))
clk_checkbutton.place(x=10, y=140)
a_tag = tkinter.Label(popup_root, text="A", bg='white', font=('Purisa', 12))
a_tag.place(x=10, y=180)
self.a_entry = tkinter.Entry(popup_root, bd=2, width=12, font=('Purisa', 12))
self.a_entry.insert(0, str(self.config.get_woodway_a()))
self.a_entry.place(x=60, y=180)
b_tag = tkinter.Label(popup_root, text="B", bg='white', font=('Purisa', 12))
b_tag.place(x=10, y=220)
self.b_entry = tkinter.Entry(popup_root, bd=2, width=12, font=('Purisa', 12))
self.b_entry.insert(0, str(self.config.get_woodway_b()))
self.b_entry.place(x=60, y=220)
self.review_var = tkinter.BooleanVar(popup_root, value=self.config.get_review())
review_checkbutton = tkinter.Checkbutton(popup_root, text="Enable Review Mode", bg='white',
variable=self.review_var, font=('Purisa', 12))
review_checkbutton.place(x=10, y=250)
self.auto_export_var = tkinter.BooleanVar(popup_root, value=self.config.get_auto_export())
auto_export_checkbutton = tkinter.Checkbutton(popup_root, text="Enable Auto Export", bg='white',
variable=self.auto_export_var, font=('Purisa', 12))
auto_export_checkbutton.place(x=10, y=280)
self.protocol_var = tkinter.BooleanVar(popup_root, value=self.config.get_protocol_beep())
prot_checkbutton = tkinter.Checkbutton(popup_root, text="Enable Protocol Beep", bg='white',
variable=self.protocol_var, font=('Purisa', 12))
prot_checkbutton.place(x=10, y=310)
clear_projects = tkinter.Button(popup_root, text="Clear Recent Projects",
font=('Purisa', 12), command=self.clear_projects)
clear_projects.place(x=10, y=350)
ok_button = tkinter.Button(popup_root, text="OK", command=self.on_closing, font=('Purisa', 12))
ok_button.place(x=150, y=400, anchor=N)
def on_closing(self):
self.update_fps()
self.update_e4()
self.update_woodway()
self.update_ble()
self.update_review()
self.update_clickmode()
self.update_auto_export()
self.update_protocol_beep()
self.popup_root.destroy()
def update_fps(self):
self.config.set_fps(int(self.fps_entry.get()))
def update_e4(self):
self.config.set_e4(self.e4_var.get())
def update_ble(self):
self.config.set_ble(self.ble_var.get())
def update_woodway(self):
self.config.set_woodway(self.woodway_var.get())
def update_clickmode(self):
self.config.set_clickmode(self.click_var.get())
def update_review(self):
self.config.set_review(self.review_var.get())
def update_auto_export(self):
self.config.set_auto_export(self.auto_export_var.get())
def clear_projects(self):
self.config.set_recent_projects([])
def update_protocol_beep(self):
self.config.set_protocol_beep(self.protocol_var.get())
class ProjectPopup:
def __init__(self, top, root, name, popup_call):
assert top.popup_return
self.popup_call = popup_call
self.caller = top
self.entry = None
self.popup_root = None
self.name = name
self.popup_entry(root)
def popup_entry(self, root):
# Create a Toplevel window
self.popup_root = popup_root = tkinter.Toplevel(root)
popup_root.config(bg="white", bd=-2)
popup_root.geometry("400x100")
popup_root.title(self.name)
# Create an Entry Widget in the Toplevel window
project_name_text = tkinter.Label(popup_root, text="Project Name", font=('Purisa', 10), bg='white')
project_name_text.place(x=10, y=10, anchor=NW)
self.project_name_var = tkinter.StringVar(popup_root)
self.entry = tkinter.Entry(popup_root, bd=2, width=35, textvariable=self.project_name_var)
self.entry.place(x=110, y=11, anchor=NW)
project_dir_text = tkinter.Label(popup_root, text="Project Folder", font=('Purisa', 10), bg='white')
project_dir_text.place(x=10, y=40, anchor=NW)
self.dir_entry_var = tkinter.StringVar(popup_root, value=rf'{cometrics_ver_root}\Projects')
self.dir_entry = tkinter.Entry(popup_root, bd=2, width=35, textvariable=self.dir_entry_var)
self.dir_entry.place(x=110, y=41, anchor=NW)
self.folder_img = itk.PhotoImage(file='images/folder.png')
dir_button = tkinter.Button(popup_root, image=self.folder_img, command=self.select_dir)
dir_button.place(x=330, y=40, anchor=NW)
# Create a Button Widget in the Toplevel Window
import_button = tkinter.Button(popup_root, text="Import", command=self.import_project)
import_button.place(x=195, y=95, anchor=tkinter.SE, width=50)
button = tkinter.Button(popup_root, text="OK", command=self.close_win)
button.place(x=205, y=95, anchor=tkinter.SW, width=50)
center(popup_root)
popup_root.focus_force()
self.entry.focus()
def import_project(self):
chosen_project = filedialog.askdirectory(initialdir=rf'{cometrics_ver_root}\Projects',
title='Select project folder')
if chosen_project:
if os.path.isdir(chosen_project):
if not os.path.exists(os.path.join(chosen_project, '.cometrics')):
response = messagebox.askyesno("Invalid Project", "Project is not a valid cometrics project, "
"this could happen if importing a legacy project "
"(legacy projects will be updated in that case), "
"continue?")
if not response:
self.popup_root.focus_force()
return
self.dir_entry_var.set(pathlib.Path(chosen_project).parent)
self.project_name_var.set(pathlib.Path(chosen_project).name)
self.close_win()
else:
messagebox.showerror("Project Error", "Projects must be folders!")
def select_dir(self):
chosen_dir = filedialog.askdirectory(title='Select root directory to save files')
if chosen_dir:
self.dir_entry_var.set(chosen_dir)
self.popup_root.focus_force()
def close_win(self):
self.caller.popup_return((self.entry.get(), self.dir_entry.get()), self.popup_call)
self.popup_root.destroy()
class SurveyPopup:
def __init__(self, root, name):
self.entry = None
self.popup_root = None
self.name = name
self.popup_entry(root)
def popup_entry(self, root):
# Create a Toplevel window
self.popup_root = popup_root = tkinter.Toplevel(root)
popup_root.config(bg="white", bd=-2)
popup_root.geometry("450x125")
popup_root.title(self.name)
# Create an Entry Widget in the Toplevel window
self.text = tkinter.Label(popup_root, bg='white', bd=2, text="Thank you for using cometrics!")
self.text.pack()
self.survey_text = tkinter.Label(popup_root, bg='white', bd=2,
text="To improve this software, please fill out this survey by clicking the link below,")
self.survey_text.pack()
self.survey_link = tkinter.Label(popup_root, text="Cometrics Survey", font=('TkDefaultFont', 9),
bg='white', fg="blue", cursor="hand2")
self.survey_link.pack()
self.survey_link.bind("<Button-1>", lambda e: webbrowser.open_new_tab(survey))
self.survey_link.bind("<Enter>", self.underline_text)
self.survey_link.bind("<Leave>", self.remove_underline)
self.last_text = tkinter.Label(popup_root, bg='white', bd=2,
text="This popup will not show up again, thank you!")
self.last_text.pack()
# Create a Button Widget in the Toplevel Window
button = tkinter.Button(popup_root, text="Close", command=self.close_win)
button.pack(pady=5, side=TOP)
center(popup_root)
popup_root.focus_force()
def underline_text(self, enter):
self.survey_link["font"] = ('TkDefaultFont', 9, 'underline')
def remove_underline(self, enter):
self.survey_link["font"] = ('TkDefaultFont', 9)
def close_win(self):
try:
self.popup_root.destroy()
except Exception as e:
messagebox.showerror("Error", f"Exception encountered:\n{str(e)}\n{traceback.print_exc()}")
class EntryPopup:
def __init__(self, top, root, name, popup_call):
assert top.popup_return
self.popup_call = popup_call
self.caller = top
self.entry = None
self.popup_root = None
self.name = name
self.popup_entry(root)
def popup_entry(self, root):
# Create a Toplevel window
self.popup_root = popup_root = tkinter.Toplevel(root)
popup_root.config(bg="white", bd=-2)
popup_root.geometry("300x50")
popup_root.title(self.name)
# Create an Entry Widget in the Toplevel window
self.entry = tkinter.Entry(popup_root, bd=2, width=25)
self.entry.pack()
# Create a Button Widget in the Toplevel Window
button = tkinter.Button(popup_root, text="OK", command=self.close_win)
button.pack(pady=5, side=TOP)
center(popup_root)
popup_root.focus_force()
self.entry.focus()
def close_win(self):
try:
self.caller.popup_return(self.entry.get(), self.popup_call)
self.popup_root.destroy()
except Exception as e:
messagebox.showerror("Error", f"Exception encountered:\n{str(e)}\n{traceback.print_exc()}")
class NewKeyPopup:
def __init__(self, top, root, action, index=None, key=None, tag=None):
assert top.key_popup_return
self.caller = top
self.index = index
self.action = action
self.key = key
self.tag = tag
self.tag_entry = None
self.key_entry = None
self.popup_root = None
self.new_key_name_entry(root)
def new_key_name_entry(self, root):
# Create a Toplevel window
popup_root = self.popup_root = tkinter.Toplevel(root)
popup_root.config(bg="white", bd=-2)
popup_root.geometry("300x100")
popup_root.title("Enter New Binding")
# Create an Entry Widget in the Toplevel window
self.tag_label = tkinter.Label(popup_root, text="Definition", bg='white')
self.tag_label.place(x=30, y=20, anchor=W)
self.tag_entry = tkinter.Entry(popup_root, bd=2, width=25, bg='white')
self.tag_entry.place(x=90, y=20, anchor=W)
if self.tag:
set_entry_text(self.tag_entry, self.tag)
self.key_label = tkinter.Label(popup_root, text="Key", bg='white')
self.key_label.place(x=30, y=50, anchor=W)
self.key_entry = tkinter.Entry(popup_root, bd=2, width=25, bg='white')
self.key_entry.place(x=90, y=50, anchor=W)
if self.key:
set_entry_text(self.key_entry, self.key)
# Create a Button Widget in the Toplevel Window
button = tkinter.Button(popup_root, text="Assign", fg='green', command=self.assign_key)
button.place(x=140, y=70, anchor=NE)
button = tkinter.Button(popup_root, text="Delete", fg='red', command=self.delete_key)
button.place(x=160, y=70, anchor=NW)
center(popup_root)
popup_root.focus_force()
self.tag_entry.focus()
def delete_key(self):
self.caller.key_popup_return(self.tag_entry.get(), self.key_entry.get(), self.action, self.index, True)
self.close_win()
def assign_key(self):
if len(self.key_entry.get()) == 1:
self.caller.key_popup_return(self.tag_entry.get(), self.key_entry.get(), self.action, self.index)
self.close_win()
else:
messagebox.showwarning("Warning", 'New key binding can only be one character!')
print("WARNING: New key binding can only be one character!")
self.popup_root.focus_force()
self.tag_entry.focus()
def close_win(self):
self.popup_root.destroy()
def set_entry_text(widget: tkinter.Entry, text):
widget.delete(0, END)
widget.insert(0, text)
class AddWoodwayProtocolStep:
def __init__(self, top, root, edit=False, dur=None, rs=None, ls=None, incl=None, freq_key=None, dur_key=None):
assert top.popup_return
self.caller = top
self.entry = None
self.popup_root = None
self.name = "Add Woodway Step"
self.dur, self.rs, self.ls, self.incl = dur, rs, ls, incl
self.freq_key, self.dur_key = freq_key, dur_key
self.edit = edit
self.popup_entry(root)
def popup_entry(self, root):
# Create a Toplevel window
self.popup_root = popup_root = tkinter.Toplevel(root)
popup_root.config(bg="white", bd=-2)
popup_root.geometry("300x350")
popup_root.title(self.name)
# Create an Entry Widget in the Toplevel window
label = tkinter.Label(popup_root, text="Step Duration", font=('Purisa', 12), bg='white')
label.pack()
self.duration_entry = tkinter.Entry(popup_root, bd=2, width=25)
self.duration_entry.pack()
if self.dur is not None:
set_entry_text(self.duration_entry, self.dur)
label = tkinter.Label(popup_root, text="Left Side Speed", font=('Purisa', 12), bg='white')
label.pack()
self.ls_entry = tkinter.Entry(popup_root, bd=2, width=25)
self.ls_entry.pack()
if self.ls is not None:
set_entry_text(self.ls_entry, self.ls)
label = tkinter.Label(popup_root, text="Right Side Speed", font=('Purisa', 12), bg='white')
label.pack()
self.rs_entry = tkinter.Entry(popup_root, bd=2, width=25)
self.rs_entry.pack()
if self.rs is not None:
set_entry_text(self.rs_entry, self.rs)
label = tkinter.Label(popup_root, text="Incline", font=('Purisa', 12), bg='white')
label.pack()
self.incline_entry = tkinter.Entry(popup_root, bd=2, width=25)
self.incline_entry.pack()
if self.incl is not None:
set_entry_text(self.incline_entry, self.incl)
field_font = ('Purisa', 12)
freq_label = tkinter.Label(popup_root, font=field_font, text="Frequency Key Association", bg='white')
freq_label.pack()
self.freq_var = tkinter.StringVar(popup_root, value=self.freq_key)
freq_box = Combobox(popup_root, textvariable=self.freq_var, font=field_font)
freq_box['values'] = ['None'] + self.caller.caller.ovu.key_view.bindings
freq_box['state'] = 'readonly'
freq_box.config(font=field_font)
freq_box.pack()
freq_box.option_add('*TCombobox*Listbox.font', field_font)
dur_label = tkinter.Label(popup_root, font=field_font, text="Duration Key Association", bg='white')
dur_label.pack()
self.dur_var = tkinter.StringVar(popup_root, self.dur_key)
dur_box = Combobox(popup_root, textvariable=self.dur_var, font=field_font)
dur_box['values'] = ['None'] + self.caller.caller.ovu.key_view.dur_bindings
dur_box['state'] = 'readonly'
dur_box.config(font=field_font)
dur_box.pack()
dur_box.option_add('*TCombobox*Listbox.font', field_font)
# Create a Button Widget in the Toplevel Window
button = tkinter.Button(popup_root, text="OK", command=self.close_win)
button.pack(pady=5, side=TOP)
center(popup_root)
popup_root.focus_force()
self.duration_entry.focus()
def close_win(self):
try:
freq_key = ''
if self.freq_var.get() not in ['None', '']:
freq_key = str(self.freq_var.get()[0]).lower()
dur_key = ''
if self.dur_var.get() not in ['None', '']:
dur_key = str(self.dur_var.get()[0]).lower()
new_step = [float(self.duration_entry.get()), float(self.ls_entry.get()),
float(self.rs_entry.get()), float(self.incline_entry.get()),
freq_key, dur_key]
self.caller.popup_return(new_step, self.edit)
self.popup_root.destroy()
except ValueError:
messagebox.showerror("Error", "All values input must be numbers! Check Woodway documentation or\n"
"User Guide for valid values!")
class AddBleProtocolStep:
def __init__(self, top, root, edit=False, dur=None, motor_1=None, motor_2=None, freq_key=None, dur_key=None):
assert top.popup_return
self.caller = top
self.entry = None
self.popup_root = None
self.name = "Add BLE Step"
self.edit = edit
self.dur, self.motor_1, self.motor_2 = dur, motor_1, motor_2
self.freq_key, self.dur_key = freq_key, dur_key
self.popup_entry(root)
def popup_entry(self, root):
# Create a Toplevel window
self.popup_root = popup_root = tkinter.Toplevel(root)
popup_root.config(bg="white", bd=-2)
popup_root.geometry("300x300")
popup_root.title(self.name)
# Create an Entry Widget in the Toplevel window
label = tkinter.Label(popup_root, text="Step Duration", font=('Purisa', 12), bg='white')
label.pack()
self.duration_entry = tkinter.Entry(popup_root, bd=2, width=25)
self.duration_entry.pack()
if self.dur is not None:
set_entry_text(self.duration_entry, self.dur)
label = tkinter.Label(popup_root, text="Left Motor Level", font=('Purisa', 12), bg='white')
label.pack()
self.motor_1_entry = tkinter.Entry(popup_root, bd=2, width=25)
self.motor_1_entry.pack()
if self.motor_1 is not None:
set_entry_text(self.motor_1_entry, self.motor_1)
label = tkinter.Label(popup_root, text="Right Motor Level", font=('Purisa', 12), bg='white')
label.pack()
self.motor_2_entry = tkinter.Entry(popup_root, bd=2, width=25)
self.motor_2_entry.pack()
if self.motor_2 is not None:
set_entry_text(self.motor_2_entry, self.motor_2)
field_font = ('Purisa', 12)
freq_label = tkinter.Label(popup_root, font=field_font, text="Frequency Key Association", bg='white')
freq_label.pack()
self.freq_var = tkinter.StringVar(popup_root, value=self.freq_key)
freq_box = Combobox(popup_root, textvariable=self.freq_var, font=field_font)
freq_box['values'] = ['None'] + self.caller.caller.ovu.key_view.bindings
freq_box['state'] = 'readonly'
freq_box.config(font=field_font)
freq_box.pack()
freq_box.option_add('*TCombobox*Listbox.font', field_font)
dur_label = tkinter.Label(popup_root, font=field_font, text="Duration Key Association", bg='white')
dur_label.pack()
self.dur_var = tkinter.StringVar(popup_root, value=self.dur_key)
dur_box = Combobox(popup_root, textvariable=self.dur_var, font=field_font)
dur_box['values'] = ['None'] + self.caller.caller.ovu.key_view.dur_bindings
dur_box['state'] = 'readonly'
dur_box.config(font=field_font)
dur_box.pack()
dur_box.option_add('*TCombobox*Listbox.font', field_font)
# Create a Button Widget in the Toplevel Window
button = tkinter.Button(popup_root, text="OK", command=self.close_win)
button.pack(pady=5, side=TOP)
center(popup_root)
popup_root.focus_force()
self.duration_entry.focus()
def close_win(self):
try:
freq_key = ''
if self.freq_var.get() not in ['None', '']:
freq_key = str(self.freq_var.get()[0]).lower()
dur_key = ''
if self.dur_var.get() not in ['None', '']:
dur_key = str(self.dur_var.get()[0]).lower()
new_step = [float(self.duration_entry.get()), float(self.motor_1_entry.get()),
float(self.motor_2_entry.get()),
freq_key, dur_key]
self.caller.popup_return(new_step, edit=self.edit)
self.popup_root.destroy()
except ValueError:
messagebox.showerror("Error", "All values input must be numbers! Check Woodway documentation or\n"
"User Guide for valid values!")
class CalibrateWoodway:
def __init__(self, top, root, woodway):
assert top.calibrate_return
self.caller = top
self.entry = None
self.popup_root = None
self.name = "Calibrate Woodway"
self.woodway = woodway
self.calibrating = False
self.calibrated_speed_increasing = None
self.calibrated_speed_decreasing = None
self.calibrated_speed = None
self.calibration_step = 0
self.popup_entry(root)
def popup_entry(self, root):
# Create a Toplevel window
self.popup_root = popup_root = tkinter.Toplevel(root)
popup_root.config(bg="white", bd=-2)
popup_root.geometry("800x300")
popup_root.title(self.name)
# Create an Entry Widget in the Toplevel window
label = tkinter.Label(popup_root, text="1. Press the 'Calibrate' button,\n"
"2. The Woodway will increase speed by 0.1 MPH from zero every five seconds,\n"
"3. Prompt the subject to alert operator when walking feels comfortable,\n"
"4. When subject alerts operator, press the 'Stop' button,\n"
"5. The speed when stopped will be recorded, \n"
"6. Press 'Calibrate' button again to decrease speed from 150% of previous recorded speed,\n"
"7. Prompt the subject to alert operator when walking feel comfortable,\n"
"8. When subject alerts operator, press the 'Stop' button,\n"
"9. The speed when stopped will be recorded,\n"
"10. The two recorded speeds will be averaged and saved as the Preferred Walking Speed,\n"
"11. Double check the final speed and press 'Save' to save the speed.",
font=('Purisa', 12), bg='white', justify=tkinter.LEFT)
label.pack()
# Create a Button Widget in the Toplevel Window
label.place(x=10, y=10)
# Create a Button Widget in the Toplevel Window
self.cal_button = tkinter.Button(popup_root, text="Calibrate", command=self.start_calibration_step,
font=('Purisa', 12))
self.cal_button.place(x=400, y=280, anchor=tkinter.SE, width=150, height=30)
self.stop_button = tkinter.Button(popup_root, text="Stop", command=self.stop_calibration_step,
font=('Purisa', 12))
self.stop_button.place(x=400, y=280, anchor=tkinter.SW, width=150, height=30)
self.stop_button["state"] = 'disabled'
self.calibration_text_var = tkinter.StringVar(popup_root, value=f"Calibration Value: 0.0 MPH")
text = tkinter.Label(popup_root, textvariable=self.calibration_text_var, font=('Purisa', 12), bg='white')
text.place(x=400, y=240, anchor=tkinter.S)
center(popup_root)
popup_root.focus_force()
def start_calibration_step(self):
if self.calibration_step == 0:
self.calibrating = True
cal_thread = threading.Thread(target=self.calibration_thread, args=(0.1, True))
cal_thread.daemon = True
cal_thread.start()
self.calibration_step += 1
elif self.calibration_step == 1:
self.calibrating = True
cal_thread = threading.Thread(target=self.calibration_thread,
args=(self.calibrated_speed_increasing * 1.5, False))
cal_thread.daemon = True
cal_thread.start()
self.calibration_step += 1
self.stop_button["state"] = 'active'
self.cal_button["state"] = 'disabled'
def calibration_thread(self, start_value, increasing):
while self.calibrating:
if increasing:
for speed in np.arange(start_value, 20.0, 0.1):
self.calibrated_speed_increasing = speed
self.woodway.set_speed(float(speed), float(speed))
self.calibration_text_var.set(f"Calibration Value: {self.calibrated_speed_increasing:.1f} MPH")
for i in range(0, 4):
time.sleep(0.25)
if not self.calibrating:
self.calibrated_speed_increasing = np.average(self.woodway.get_speeds())
self.woodway.set_speed(float(0.0), float(0.0))
return
else:
for speed in np.arange(start_value, 0.0, -0.1):
self.calibrated_speed_decreasing = speed
self.woodway.set_speed(float(speed), float(speed))
self.calibration_text_var.set(f"Calibration Value: {self.calibrated_speed_decreasing:.1f} MPH")
for i in range(0, 4):
time.sleep(0.25)
if not self.calibrating:
self.calibrated_speed_decreasing = np.average(self.woodway.get_speeds())
self.woodway.set_speed(float(0.0), float(0.0))
if self.calibrated_speed_increasing and self.calibrated_speed_decreasing:
self.calibrated_speed = np.average([self.calibrated_speed_increasing,
self.calibrated_speed_decreasing])
self.calibration_text_var.set(f"Calibration Value: {self.calibrated_speed:.1f} MPH")
self.stop_button.config(text="Save", command=self.close_win)
return
def stop_calibration_step(self):
self.calibrating = False
if self.calibration_step == 2:
self.stop_button["state"] = 'active'
self.cal_button["state"] = 'disabled'
else:
self.stop_button["state"] = 'disabled'
self.cal_button["state"] = 'active'
def close_win(self):
if self.calibrated_speed:
self.caller.calibrate_return(self.calibrated_speed)
self.calibrating = False
self.popup_root.destroy()
class CalibrateVibrotactors:
def __init__(self, top, root, left_vta, right_vta):
assert top.calibrate_return
self.caller = top
self.entry = None
self.popup_root = None
self.name = "Calibrate Vibrotactors"
self.left_vta, self.right_vta = left_vta, right_vta
self.calibrating = False
self.calibrated_left_increasing = None
self.calibrated_right_increasing = None
self.calibrated_left_decreasing = None
self.calibrated_right_decreasing = None
self.calibrated_left = None
self.calibrated_right = None
self.calibration_step = 0
self.popup_entry(root)
def popup_entry(self, root):
# Create a Toplevel window
self.popup_root = popup_root = tkinter.Toplevel(root)
popup_root.config(bg="white", bd=-2)
popup_root.geometry("800x350")
popup_root.title(self.name)
# Create an Entry Widget in the Toplevel window
label = tkinter.Label(popup_root, text="1. Press the 'Calibrate Left' button,\n"
"2. The vibrotactors will increase intensity by one from zero each second,\n"
"3. Prompt the subject to alert operator when vibrotactors are perceptible,\n"
"4. When subject alerts operator, press the 'Stop' button,\n"
"5. The intensity level when stopped will be recorded, \n"
"6. Press 'Calibrate Left' button again to decrease intensity from 150% of previous recorded intensity level,\n"
"7. Prompt the subject to alert operator when vibrotactors are perceptible,\n"
"8. When subject alerts operator, press the 'Stop' button,\n"
"9. The intensity level when stopped will be recorded,\n"
"10. The two recorded levels will be averaged and saved as the Left Sensory Perception Threshold,\n"
"11. Notice 'Calibrate Right' button, repeat procedure for left vibrotactor,\n"
"12. Double check the final values and press 'Save' to save the thresholds.",
font=('Purisa', 12), bg='white', justify=tkinter.LEFT)
label.place(x=10, y=10)
# Create a Button Widget in the Toplevel Window
self.cal_button = tkinter.Button(popup_root, text="Calibrate Left", command=self.start_calibration_step,
font=('Purisa', 12))
self.cal_button.place(x=400, y=330, anchor=tkinter.SE, width=150, height=30)
self.stop_button = tkinter.Button(popup_root, text="Stop", command=self.stop_calibration_step,
font=('Purisa', 12))
self.stop_button.place(x=400, y=330, anchor=tkinter.SW, width=150, height=30)
self.stop_button["state"] = 'disabled'
self.calibration_text_var = tkinter.StringVar(popup_root, value=f"Calibration Left: 0\tCalibration Right: 0")
text = tkinter.Label(popup_root, textvariable=self.calibration_text_var, font=('Purisa', 12), bg='white')
text.place(x=400, y=290, anchor=tkinter.S)
center(popup_root)
popup_root.focus_force()
def start_calibration_step(self):
if self.calibration_step == 0:
self.calibrating = True