-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilsgui.py
76 lines (63 loc) · 2.45 KB
/
utilsgui.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
import tkinter as tk
import re
def enable_children(parent, enabled=True):
for child in parent.winfo_children():
wtype = child.winfo_class()
# print(wtype)
if wtype not in ('Frame', 'Labelframe', 'TFrame', 'TLabelframe'):
child.configure(state=tk.NORMAL if enabled else tk.DISABLED)
else:
enable_children(child, enabled)
def validate_numeric_entry_input(new_value):
# Allow only if the input matches the regex for a decimal number
if re.match(r"^\d*\.?\d*$", new_value):
return True
return False
class ToolTip:
def __init__(self, widget, text):
self.widget = widget
self.text = text
self.tooltip = None
self.label = None # To reference the Label for updating its text
self.widget.bind("<Enter>", lambda _: self.show_tooltip())
self.widget.bind("<Leave>", lambda _: self.hide_tooltip())
def show_tooltip(self):
if self.tooltip: # Tooltip is already being shown
self.label.config(text=self.text) # Just update the text
else:
x = self.widget.winfo_rootx() + 20
y = self.widget.winfo_rooty() + 20
self.tooltip = tk.Toplevel(self.widget)
self.tooltip.wm_overrideredirect(True)
self.tooltip.wm_geometry(f"+{x}+{y}")
self.label = tk.Label(
self.tooltip,
text=self.text,
background="light goldenrod",
relief="solid",
borderwidth=1,
font=("Arial", 10),
)
self.label.pack()
def hide_tooltip(self):
if self.tooltip:
self.tooltip.destroy()
self.tooltip = None
self.label = None # Reset the reference to the label
def change_text(self, text):
self.text = text
if self.tooltip: # If the tooltip is visible, update its text
self.label.config(text=self.text)
class PrintLogger(object):
def __init__(self, textbox): # pass reference to text widget
self.textbox = textbox
def write(self, text):
# Use of after() is needed to prevent segmentation faults
self.textbox.after(0, self._append_text, text)
def _append_text(self, text):
self.textbox.configure(state="normal")
self.textbox.insert("end", text)
self.textbox.see("end")
self.textbox.configure(state="disabled")
def flush(self): # needed
pass