-
Notifications
You must be signed in to change notification settings - Fork 3
/
tkComment.py
69 lines (49 loc) · 1.95 KB
/
tkComment.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
#!/usr/bin/env python3
import tkinter
import matplotlib
matplotlib.use("TkAgg")
if matplotlib.get_backend() != 'TkAgg':
raise Exception(
f'current matplotlib backend <{matplotlib.get_backend()}> might result in a crash when used with '
f'<tkinter>, please use <TkAgg> backend')
class _ConstrainedEntry(tkinter.Entry):
def __init__(self, charlimit=12, *args, **kwargs):
tkinter.Entry.__init__(self, *args, **kwargs)
vcmd = (self.register(self.on_validate),"%P")
self.configure(validate="all", validatecommand=vcmd)
self.charlimit = charlimit
def disallow(self):
self.bell()
def on_validate(self, new_value):
try:
# print(new_value) # for testing
if len(new_value) <= self.charlimit: return True
if len(new_value) > self.charlimit:
self.disallow()
return False
except ValueError:
self.disallow()
return False
class tkComment(object):
def __init__(self):
root = self.root = tkinter.Toplevel()
self.comment = ''
root.geometry("500x50+600+600")
tkinter.Button(root, text="save comment",
command=lambda: self._assign(entryVar)).pack(anchor=tkinter.S, side=tkinter.BOTTOM)
root.title('comment for this source:')
root.bind('<Escape>', lambda e: self._assign(entryVar))
root.bind('<Return>', lambda e: self._assign(entryVar))
entryVar = self.entryVar = tkinter.StringVar()
entry = self.entry = _ConstrainedEntry(charlimit=53, master=root, width=60, textvariable=self.entryVar)
entry.pack(side=tkinter.TOP)
entry.focus()
root.attributes("-topmost", True)
root.attributes("-topmost", False)
def _assign(self,var):
self.comment=var.get()
self.root.destroy()
if __name__ == '__main__':
tkC = tkComment()
tkC.root.mainloop()
print(tkC.comment)