0

If I had a Frame named as the variable table which has multiple Entry widgets within this Frame, would there be a way of getting all the Entry widgets' text content?

newbie
  • 1,551
  • 1
  • 11
  • 21
  • where is the minimal code to debug – AD WAN Mar 22 '18 at 11:51
  • 2
    [iterate over all child widgets](https://stackoverflow.com/questions/15995783/python-tkinter-how-to-delete-all-children-elements), and [get their text content](https://stackoverflow.com/questions/9815063/get-contents-of-a-tkinter-entry-widget) – Aran-Fey Mar 22 '18 at 11:55
  • 1
    Actually, [this](https://stackoverflow.com/questions/34667710/pattern-matching-tkinter-child-widgets-winfo-children-to-determine-type-pytho) is a better dupe for iterating over the child widgets – Aran-Fey Mar 22 '18 at 11:57
  • Thanks @Aran-Fey – newbie Mar 22 '18 at 11:58
  • 1
    You could also create all entry widgets in a list and iterate over that list. – Mike - SMT Mar 22 '18 at 12:56

1 Answers1

2

Yes. In the example below when the button is clicked every child in a parent widget is checked whether are equal or not to be an entry, then their contents are printed:

try:
    import tkinter as tk
except ImportError:
    import Tkinter as tk


def get_all_entry_widgets_text_content(parent_widget):
    children_widgets = parent_widget.winfo_children()
    for child_widget in children_widgets:
        if child_widget.winfo_class() == 'Entry':
            print(child_widget.get())


def main():
    root = tk.Tk()
    table = tk.Frame(root)
    for _ in range(3):
        tk.Entry(table).pack()
    tk.Button(table, text="Get",
        command=lambda w=table: get_all_entry_widgets_text_content(w)).pack()
    table.pack()
    tk.mainloop()


if __name__ == '__main__':
    main()

One could have a much better get_all_entry_widgets_text_content method based on how the entries are instantiated, however, this one should work for all direct children regardless.

Nae
  • 14,209
  • 7
  • 52
  • 79