I have a TKinter entry widget where i want to validate the input, and everything works as expected.
But when I set the StringVariable of the Entry widget the validation function is no more called. Can someone explain me this behavior?
Here is an example of my problem where i validate whether the input is a digit or not
import tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
self.var1 = tk.StringVar()
valid_cmd = (self.register(self.on_validate), '%S')
self.entry = tk.Entry(self, textvariable=self.var1, validate="key", validatecommand=valid_cmd)
self.entry.pack(side="top", fill="x")
self.var1.set("25") # comment out this line and validation function works as expected
def on_validate(self, S):
print("in validation function")
if S.isdigit():
return True
else:
return False
if __name__ == "__main__":
root = tk.Tk()
tk.Tk.geometry(root, '200x40')
Example(root).pack(fill="both", expand=True)
root.mainloop()
I want to be able to change the value of the StringVar (and therefore the content of the Entry Widget) without loosing the validation function.