Search code examples
pythonpython-3.xtkinterpython-3.5tkinter-entry

Getting all of the Entry text content from a frame


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?


Solution

  • 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.