8

When I bind the event <Key> to an entry and read the content, the change somehow lags behind. I want to "dynamically update" another entry that shows the result of a calculation of the contents of various entries as soon as entry 1 is changed. But somehow the change is not recognized instantly, only the foregoing one. Don't know if the problem is clear, so let's say it like this: If I make n changes, the changes up to n-1 are recognized. For example, if the number 1000 is in the entry and I press backspace twice, entry_1.get() would yield 100 instead of 10. Hope you understand what i mean now :)

Code snippet (simplified):

self.entry_1.bind('<Key>',lambda d: self.update())

def update(self):
    success=True
    try:
        float(self.entry_1.get())
        float(self.entry_2.get())
    except ValueError: success=False
    if success:
 
        self.entry_3.delete(0,"end")
        x=(float(self.entry_1.get())*float(self.entry_2.get())
        self.entry_3.insert("end", "%g" %x)

What might be the reason for that?

danicotra
  • 1,333
  • 2
  • 15
  • 34
Jakob
  • 1,897
  • 2
  • 16
  • 17
  • possible duplicate of [How to bind self events in Tkinter Text widget after it will binded by Text widget?](http://stackoverflow.com/questions/3501849/how-to-bind-self-events-in-tkinter-text-widget-after-it-will-binded-by-text-widge) – Bryan Oakley Aug 29 '11 at 19:33

1 Answers1

7

The reason is due to the order that events are processed. That order is defined by the "binding tag" (or bindtag) of the widget. By default the order is widget, class, toplevel, "all". For example, if you have a binding on the widget, and on the class, and on the toplevel window that contains the widget, and on the special case "all", the bindings will fire in that order.

I gave a lengthy writeup of this problem in this answer to the question How to bind self events in Tkinter Text widget after it will binded by Text widget?

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685