Widget Validation In Tkinter
Solution 1:
I found a great explanation here (in the last paragraph of the chapter Validation):
http://stupidpythonideas.blogspot.fr/2013/12/tkinter-validation.html
If your validatecommand (or invalidcommand) modifies the Entry directly or indirectly (e.g., by calling set on its StringVar), the validation will get disabled as soon as your function returns. (This is how Tk prevents an infinite loop of validate triggering another validate.) You have to turn it back on (by calling config). But you can't do that from inside the function, because it gets disabled after your function returns.
But you need to apply some changes to be able to use this trick.
You need to make the Spinbox
an instance attribute, with self
:
self.items_count = Spinbox(self.root, textvariable=self.items_var,
from_=5, to=10, width=4, validate='focusout',
validatecommand=validate_cmd,
invalidcommand=invalid_cmd)
self.items_count.grid(row=0, column=1)
And then you can call self.items_count.after_idle(...)
inside the validate
method :
defvalidate(self, entry):
try:
value = int(entry)
valid = value inrange(5, 11)
except ValueError:
valid = Falseifnot valid:
self.root.bell()
self.items_count.after_idle(lambda: self.items_count.config(validate='focusout'))
return valid
Post a Comment for "Widget Validation In Tkinter"