forked from furas/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain-2.py
More file actions
89 lines (65 loc) · 2.03 KB
/
Copy pathmain-2.py
File metadata and controls
89 lines (65 loc) · 2.03 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
#
# https://stackoverflow.com/questions/4140437/interactively-validating-entry-widget-content-in-tkinter/4140988#4140988
#
def convert(text):
parts = []
while text:
parts.insert(0, text[-3:])
text = text[:-3]
return '.'.join(parts)
def check(d, i, P, s, S, v, V, W):
print("d='%s'" % d)
print("i='%s'" % i)
print("P='%s'" % P)
print("s='%s'" % s)
print("S='%s'" % S)
print("v='%s'" % v)
print("V='%s'" % V)
print("W='%s'" % W)
print('-----')
if V == 'focusin':
print('if focus in')
text = P
# remove points of hundreds
text = text.replace('.', '')
e.delete('0', 'end')
e.insert('end', text)
return True
if V == 'focusout':
print('if focus out')
text = P
# add points of hundreds
parts = text.split(',')
if len(parts) > 0:
parts[0] = convert(parts[0])
text = ','.join(parts)
#e.delete('0', 'end')
#e.insert('end', text)
return True
if V == 'key':
print('if key')
text = P
# validate
parts = text.split(',')
parts_number = len(parts)
if parts_number > 2:
print('too much dots')
return False
if parts_number > 1 and parts[1]: # don't check empty string
if not parts[1].isdecimal() or len(parts[1]) > 2:
print('wrong second part')
return False
if parts_number > 0 and parts[0]: # don't check empty string
if not parts[0].isdecimal() or len(parts[0]) > 8:
print('wrong first part')
return False
return True
# --- main ---
root = tk.Tk()
vcmd = (root.register(check), '%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
e = tk.Entry(root, validate='all', validatecommand=vcmd)
e.pack()
b = tk.Button(root, text='Other widget to set focus')
b.pack()
root.mainloop()