-
Notifications
You must be signed in to change notification settings - Fork 1
/
ChecklistBox.py
95 lines (84 loc) · 3.21 KB
/
ChecklistBox.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
import tkinter as tk
import tkinter.font as font
class ChecklistBox(tk.Frame):
def __init__(self, parent, **kwargs):
tk.Frame.__init__(self, parent, **kwargs)
def setItems(self, choices, callback):
for child in self.winfo_children():
child.destroy()
self.vars = []
bg = self.cget('background')
for choice in choices:
var = tk.StringVar(value=choices[choice]['description'])
self.vars.append(var)
cb = tk.Checkbutton(self,
var=var,
text=choices[choice]['description'],
fg='black',
onvalue=1,
offvalue=0,
font=font.Font(family="TkDefaultFont", size=8),
cursor='hand2',
anchor='w',
width=20,
background=bg,
relief='flat',
highlightthickness=0,
command=callback)
cb.pack(side='top', fill='x', anchor='w')
def getItemCheckStates(self):
values = []
for var in self.vars:
value = var.get()
values.append(value)
return values
class ScrollableChecklist(tk.Frame):
def __init__(self, parent, **kwargs):
super().__init__(parent, **kwargs)
self.vscrollbar = tk.Scrollbar(self, orient=tk.VERTICAL)
self.vscrollbar.pack(side='right', fill='y', expand='false')
self.canvas = tk.Canvas(self,
highlightthickness=0,
yscrollcommand=self.vscrollbar.set,
**kwargs)
self.canvas.configure(bd=0)
self.canvas.pack(side='left', fill='both', expand='true')
self.vscrollbar.config(command=self.canvas.yview)
self.canvas.xview_moveto(0)
self.canvas.yview_moveto(0)
self.interior = tk.Frame(self.canvas, **kwargs)
self.interior.configure(height=2400)
self.canvas.create_window((0, 0), window=self.interior, anchor='nw')
self.canvas.bind('<Configure>', self.set_scrollregion)
def set_scrollregion(self, event=None):
self.canvas.configure(scrollregion=self.canvas.bbox('all'))
def setItems(self, choices, callback):
for child in self.interior.winfo_children():
child.destroy()
self.vars = []
bg = self.cget('background')
for choice in choices:
var = tk.StringVar(value=choices[choice]['description'])
var.set(choices[choice]['hidden'])
self.vars.append(var)
cb = tk.Checkbutton(self.interior,
var=var,
text=choices[choice]['description'],
foreground='#000000',
onvalue=1,
offvalue=0,
font=font.Font(family="TkDefaultFont", size=8),
cursor='hand2',
anchor='w',
width=20,
background=bg,
relief='flat',
highlightthickness=0,
command=callback)
cb.pack(side='top', fill='x', anchor='w')
def getItemCheckStates(self):
values = []
for var in self.vars:
value = var.get()
values.append(value)
return values