-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGUI_Additional.py
47 lines (39 loc) · 1.26 KB
/
GUI_Additional.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
from Tkinter import *
class ValidatingEntry(Entry):
# base class for validating entry widgets
def __init__(self, master, value="", **kw):
apply(Entry.__init__, (self, master), kw)
self.__value = value
self.__variable = StringVar()
self.__variable.set(value)
self.__variable.trace("w", self.__callback)
self.config(textvariable=self.__variable)
def __callback(self, *dummy):
value = self.__variable.get()
newvalue = self.validate(value)
if newvalue is None:
self.__variable.set(self.__value)
elif newvalue != value:
self.__value = newvalue
self.__variable.set(self.newvalue)
else:
self.__value = value
def validate(self, value):
# override: return value, new value, or None if invalid
return value
class IntegerEntry(ValidatingEntry):
def validate(self, value):
try:
if value:
v = int(value)
return value
except ValueError:
return None
class FloatEntry(ValidatingEntry):
def validate(self, value):
try:
if value:
v = float(value)
return value
except ValueError:
return None