[Tutor] Tkinter entry box text changed event

Peter Otten __peter__ at web.de
Mon Apr 10 03:31:10 EDT 2017


Phil wrote:

> Again, thank you for reading this.
> 
> I would like a function to be called when I enter text and then tab to the
> next entry box. I've been trying to follow the answers to similar
> questions in Stack Overflow but I've become hopelessly confused because of
> the different answers given to seemingly the same question.
> 
> I have created a row of entry boxes and a matching function like this:
> 
> for i in range(8):
>     self.numbers[i]= Entry(master, width=4, justify=CENTER,
>     foreground="gray") self.numbers[i].grid(row=16, column=i)
>     self.numbers[i].bind('StringVar()', self.my_function)
> 
> def my_function(event):
> print("function called")

To be consistent with the other code snippet this should be a method, not a 
function.
> 
> The function is not called and I know that the binding of the function to
> the entry boxes is not the correct method. What am I missing?
> 

What the ... did you expect from the "StringVar()" argument?

You can find valid events here:

http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/event-types.html

Here's a simple example without a class:

import Tkinter as tk

def welcome(event):
    print("Welcome")

def bye(event):
    print("Bye!")

root = tk.Tk()
for i in range(3):
    entry = tk.Entry(root)
    entry.bind("<FocusIn>", welcome)
    entry.bind("<FocusOut>", bye)
    entry.pack()

root.mainloop()

If you want to pass the index of the entry, say, you can use the 
extra-arguments trick,

http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/extra-args.html

also shown in one of my previous answers.



More information about the Tutor mailing list