Playing around with Python - tkInter - Entry widget - when I use validatecommand (below), the check happens the first time the string > Max, but when I continue to enter tex
Here is a code sample that will limit the input to 5 characters:
import Tkinter as tk
master = tk.Tk()
def callback():
print e.get()
def val(i):
print "validating"
print i
if int(i) > 4:
print "False"
return False
return True
vcmd = (master.register(val), '%i')
e = tk.Entry(master, validate="key", validatecommand=vcmd)
e.pack()
b = tk.Button(master, text="OK", command=lambda: callback())
b.pack()
tk.mainloop()
I threw in a bunch of print statements so you can sort of see what it's doing in the console.
Here are the other substitutions you can pass:
%d Type of action: 1 for insert, 0 for delete, or -1 for focus,
forced or textvariable validation.
%i Index of char string to be inserted/deleted, if any, otherwise -1.
%P The value of the entry if the edit is allowed. If you are config-
uring the entry widget to have a new textvariable, this will be
the value of that textvariable.
%s The current value of entry prior to editing.
%S The text string being inserted/deleted, if any, {} otherwise.
%v The type of validation currently set.
%V The type of validation that triggered the callback (key, focusin,
focusout, forced).
%W The name of the entry widget.