Alex V.
Alex V.

Reputation: 456

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()

Upvotes: 1

Views: 708

Answers (2)

Alex V.
Alex V.

Reputation: 456

I finally found the answer. The validate function HAS TO RETURN True or False, or else it is turned off after one use.

See this thread: Interactively validating Entry widget content in tkinter

Upvotes: 1

Bryan Oakley
Bryan Oakley

Reputation: 386285

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

Upvotes: 2

Related Questions