0

I have a frame that contains multiple entry and combobox widgets. The widgets are filled up with default values when the frame is populated.

I was wondering if there is a simple way to detect which entry/combobox widgets have been updated by the user.

If not, it would require me to go through all the entry/combobox widgets one by one and only store a new value if it is different than the default one.

If so I would be able to save time by only copying the values of the items that have changed without the need to compare them first.

gerts
  • 1
  • 3

1 Answers1

0

I am not sure to understand what you are trying to achieve, but the two options I can think are

  1. iterate through each widget and compare to reference values (the one you evoke in your question)
  2. bind to edits on those widgets and either store new values, or handle a list of edited widgets

The usual way to perform the former in GUI is through binding and callbacks. If you use Entry and ttk.Combobox, you can handle both with StringVar and trace bindings.

Here is a snippet (inspired by this answer) illustrating callback aware of the edited widget. It is up to you to handle a damage list (with the widgets that have been edited) or a data structure with only modified value.

from Tkinter import *
import ttk

def callback(name, sv):
    print "{0}: {1}".format(name, sv.get())

root = Tk()

sv = StringVar()
sv.trace("w", lambda name, index, mode, sv=sv: callback("one", sv))
e = Entry(root, textvariable=sv)
e.pack()

sv = StringVar()
sv.trace("w", lambda name, index, mode, sv=sv: callback("two", sv))
e = Entry(root, textvariable=sv)
e.pack()

sv = StringVar()
box = ttk.Combobox(root, textvariable=sv)
box.pack()
box['values'] = ('X', 'Y', 'Z')
box.current(0)
sv.trace("w", lambda name, index, mode, sv=sv: callback("three", sv))

root.mainloop()
Community
  • 1
  • 1
FabienAndre
  • 4,514
  • 25
  • 38