Cailean Wilkinson
Cailean Wilkinson

Reputation: 1460

Tkinter, update Entry widget before calling <Key> binding

I have a script that updates everytime a key is entered into a tkinter.Entry widget setup like so:

self.entrySearch.bind("<Key>", self.updateSearch)

The problem that I am having is that the method I've bound to "<Key>" is resolved before the key is entered into the Entry widget. This means that when I call self.entrySearch.get(), I only get what was in the Entry box just before the last keystroke.

I've tried simple appending the character onto the end, but I can't think of a way to resolve Backspaces or Deletes, or where the character is entered mid-string.

Basically, what I'm looking for is a method to allow the entry box to update before my binding is resolved.

Thanks.

Upvotes: 1

Views: 688

Answers (1)

falsetru
falsetru

Reputation: 369094

Bind the entry to a variable. Trace the variable change.

try:
    from Tkinter import *
except ImportError:
    from tkinter import *

def print_entry_value(*args):
    print(v.get())

root = Tk()
v = StringVar()
v.trace('w', print_entry_value)
e = Entry(root, textvariable=v)
e.pack()
root.mainloop()

Upvotes: 1

Related Questions