Skip to content Skip to sidebar Skip to footer

How Can I Update Entry Without A "submit" Button In Tkinter?

So I have Entries which have some values assigned to them from a CFG File. I want to modify the CFG file when the Entry is updated, live, without a submit button; Using

Solution 1:

You can use either

  • FocusOut
  • tab or enter Key
  • KeyRelease

bindings to achieve that.

Also validation functions can help as they have previous and new values available. Please read the docs for more information on that matter.

It is IMHO the most "pythonic" / "tkinter" way of achieving what is a "check and submit" functionality.

Edit

As stated by OP, binding focusout could lead to problems here an example how it does indeed work:

import Tkinter as tk
import sys

def focus_out_event(event):
    print >> sys.stderr, "Focus-Out   event called with args: %s"%event
    print >> sys.stderr, "Entry Widget Content:               %s"%event.widget.get()
def key_release_event(event):
    print >> sys.stderr, "Key-Release event called with args: %s"%event
    print >> sys.stderr, "Entry Widget Content:               %s"%event.widget.get()

if __name__ == "__main__":
    root = tk.Tk()
    entry1 = tk.Entry(root)
    entry1.bind("", key_release_event)
    entry1.bind("", focus_out_event)
    entry1.grid()

    entry2 = tk.Entry(root)
    entry2.bind("", key_release_event)
    entry2.bind("", focus_out_event)
    entry2.grid()

    root.mainloop()

Test: - enter text ("asd") to entry1 - click into entry2

The last line of output is from changing to screenshot (event that fired a focusout)

Test Result

Solution 2:

You have this key-press in event.char so you can add it to text.

Solution 3:

I decided that <Key> was not the right option in my case and instead used <FocusOut>. This way, if you either change the value using the mouse or keyboard TAB, on focus out it will update it.

Post a Comment for "How Can I Update Entry Without A "submit" Button In Tkinter?"