-
Notifications
You must be signed in to change notification settings - Fork 0
/
controls.py
71 lines (55 loc) · 2.32 KB
/
controls.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
from tkinter import *
from idlelib.tooltip import Hovertip
import numpy as np
def create_number_control(parent, default, text, tip="", increment=1, type=int, min=-np.Infinity, max=np.Infinity):
def decrease():
new_value = type(round((type(entry.get())-type(increment))*100)/100)
if new_value >= min:
value.set(new_value)
def increase():
new_value = type(round((type(entry.get())+type(increment))*100)/100)
if new_value <= max:
value.set(new_value)
def filter_decimals_only(value):
if value == "":
return True
try:
value = float(value)
if value >= min and value <= max:
return True
except:
return False
return False
def filter_ints_only(value):
if value == "":
return True
if str.isdigit(value):
value = int(value)
if value >= min and value <= max:
return True
return False
def verify_valid_value(event):
if event.widget.get() == '':
value.set(default)
# Create frame for the tooltip and other controls
frame = Frame(parent, bg='grey')
Hovertip(frame, tip)
# Add the descriptive label
Label(frame, text=text, anchor=W).pack(side=LEFT, fill=Y, expand=False)
# Create a button to decrease the value
Button(frame, text="-", command=decrease).pack(side=LEFT, fill=BOTH, expand=True)
# Create the variable and associate it with an Entry control
value = StringVar(frame, default)
(width, event) = (4, filter_ints_only) if type==int else (6, filter_decimals_only)
entry = Entry(frame, width=width, validate='all', validatecommand=(parent.register(event), '%P'), textvariable=value)
entry.pack(side=LEFT, fill=BOTH, expand=True)
entry.bind("<FocusOut>", verify_valid_value)
# Create a button to increase the value
Button(frame, text="+", command=increase).pack(side=LEFT, fill=BOTH, expand=True)
frame.pack(side=LEFT, fill=BOTH, expand=False)
return entry
def create_toolbar_button(parent, text, event_handler, tooltip, side=LEFT):
button = Button(parent, text=text, command=event_handler)
Hovertip(button, tooltip)
button.pack(side=side, fill=X, expand=False)
return button