-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwidgets.py
More file actions
89 lines (74 loc) · 3.31 KB
/
widgets.py
File metadata and controls
89 lines (74 loc) · 3.31 KB
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
import tkinter as tk
from typing import Callable, Union
import customtkinter as ctk
class FloatSpinbox(ctk.CTkFrame):
def __init__(self, *args,
width: int = 100,
height: int = 32,
step_size: Union[int, float] = 1,
min_size : Union[int, float] = 0,
max_size : Union[int, float] = 10,
command: Callable = None,
**kwargs):
super().__init__(*args, width=width, height=height, **kwargs)
self.min_size = min_size
self.max_size = max_size
self.step_size = step_size
self.command = command
self.configure(fg_color=("gray78", "gray28")) # set frame color
self.grid_columnconfigure((0, 2), weight=0) # buttons don't expand
self.grid_columnconfigure(1, weight=1) # entry expands
self.subtract_button = ctk.CTkButton(self, text="-", width=height-6, height=height-6,
command=self.subtract_button_callback)
self.subtract_button.grid(row=0, column=0, padx=(3, 0), pady=3)
self.entry = ctk.CTkEntry(self, width=width-(2*height), height=height-6, border_width=0)
self.entry.grid(row=0, column=1, columnspan=1, padx=3, pady=3, sticky="ew")
self.add_button = ctk.CTkButton(self, text="+", width=height-6, height=height-6,
command=self.add_button_callback)
self.add_button.grid(row=0, column=2, padx=(0, 3), pady=3)
# default value
self.entry.insert(0, "0.0")
def add_button_callback(self):
if float(self.entry.get()) < float(self.max_size):
if self.command is not None:
self.command()
try:
value = float(self.entry.get()) + self.step_size
self.entry.delete(0, "end")
self.entry.insert(0, value)
except ValueError:
return
def subtract_button_callback(self):
if float(self.entry.get()) > float(self.min_size):
if self.command is not None:
self.command()
try:
value = float(self.entry.get()) - self.step_size
self.entry.delete(0, "end")
self.entry.insert(0, value)
except ValueError:
return
def get(self) -> Union[float, None]:
try:
return float(self.entry.get())
except ValueError:
return None
def set(self, value: float):
self.entry.delete(0, "end")
self.entry.insert(0, str(float(value)))
class ScrollableCheckboxFrame(ctk.CTkScrollableFrame):
def __init__(self, master, title, values):
super().__init__(master, label_text=title)
self.grid_columnconfigure(0, weight=1)
self.values = values
self.checkboxes = []
for i, value in enumerate(self.values):
checkbox = ctk.CTkCheckBox(self, text=value['title'])
checkbox.grid(row=i, column=0, padx=10, pady=(10, 0), sticky="w")
self.checkboxes.append(checkbox)
def get(self):
checked_checkboxes = []
for checkbox in self.checkboxes:
if checkbox.get() == 1:
checked_checkboxes.append(checkbox.cget("text"))
return checked_checkboxes