Search code examples
tkinterpython-3.7tkinter-entry

Callback for tkinter Entry only executes once


I want to have a function called, each time when an entry in my tkinter window is changed. Because I don't want the function to be called while the used is still typing, I chose "focusout" for the validate attribute. For clarification, I got the "focusout" idea from another thread, so I am not exactly sure what it does.

The function validate is called once, but never again. Why is that?

Minimum working example below:

 import tkinter as tk


 def validate():
     print("Checked!")


 root = tk.Tk()
 sv1 = tk.StringVar()
 sv2 = tk.StringVar()
 e1 = tk.Entry(root, textvariable=sv1, validate="focusout", validatecommand=validate)
 e2 = tk.Entry(root, textvariable=sv2, validate="focusout", validatecommand=validate)

 e1.pack()
 e2.pack()

 root.mainloop()

Solution

  • The validation function must return True or False, or it will be disabled.