-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgui_widgets.py
113 lines (89 loc) · 3.87 KB
/
gui_widgets.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
import tkinter as tk
from tkinter import ttk
class TableWidget(ttk.Frame):
def __init__(self, parent, **options):
self.column_names = options.pop("column_names")
super().__init__(parent, **options)
cols = [i for i in range(len(self.column_names))]
self.index = 0
self.table = ttk.Treeview(self, **options, columns=cols, show='headings')
for col in cols:
self.table.heading(col, text=self.column_names[col])
self.table.column(col, minwidth=0, width=4*len(self.column_names[col])+80)
self.table.pack(side="left", fill="both", expand=True)
self.scrollbar = ttk.Scrollbar(self, orient=tk.VERTICAL, command=self.table.yview)
self.table.configure(yscroll=self.scrollbar.set)
self.scrollbar.pack(side="right", fill="y")
def add_row(self, entry):
self.table.insert('', tk.END, id=self.index, values=entry, tags=("all"))
self.table.yview_moveto(1)
self.index += 1
def set_row_at_index(self, index, entry):
if self.table.exists(index):
self.table.item(index, values=entry)
else:
self.add_row(entry)
def set_focus(self, index):
for i in range(self.index):
self.table.item(i, tags='all')
self.table.item(index, tags='focus')
self.table.tag_configure('all', background='white')
self.table.tag_configure('focus', background='#a8ccff')
self.table.yview_moveto(index/self.index)
class ScrollableFrameX(ttk.Frame):
def __init__(self, parent, **options):
super().__init__(parent, **options)
canvas = tk.Canvas(self)
scrollbar = ttk.Scrollbar(self, orient="horizontal", command=canvas.xview)
self.scrollable_frame = ttk.Frame(canvas)
self.scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")
)
)
canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")
canvas.configure(xscrollcommand=scrollbar.set)
canvas.pack(side="top", fill="both", expand=True)
scrollbar.pack(side="bottom", fill="x")
class ScrollableFrameY(ttk.Frame):
def __init__(self, parent, **options):
super().__init__(parent, **options)
canvas = tk.Canvas(self)
scrollbar = ttk.Scrollbar(self, orient="vertical", command=canvas.yview)
self.scrollable_frame = ttk.Frame(canvas)
self.scrollable_frame.bind(
"<Configure>",
lambda e: canvas.configure(
scrollregion=canvas.bbox("all")
)
)
canvas.create_window((0, 0), window=self.scrollable_frame, anchor="nw")
canvas.configure(yscrollcommand=scrollbar.set)
canvas.pack(side="left", fill="both", expand=True)
scrollbar.pack(side="right", fill="y")
class ImageWidget(ttk.Frame):
def __init__(self, parent, **options):
image_path = options.pop("image_path")
super().__init__(parent, **options)
img = tk.PhotoImage(file=image_path)
image_panel = tk.Label(self, image = img)
image_panel.image = img
image_panel.pack(side = "bottom", fill = "both", expand = "yes")
class DiscreteIntSpinbox(ttk.Frame):
def __init__(self, parent, **options):
max = options.pop("max")
min = options.pop("min")
super().__init__(parent, **options)
valid_values = [''] + [str(i) for i in range(min, max + 1)]
self.spinbox = ttk.Spinbox(
self,
from_=min,
to=max,
values=[i for i in range(min, max + 1)],
validate='key',
validatecommand= (self.register(lambda val : val in valid_values), '%P'),
wrap=True)
self.spinbox.pack()
def get(self):
return self.spinbox.get()