Search code examples
pythonpython-2.7tkintertkinter-entry

How do I check if any entry boxes are empty in Tkinter?


I am making a GUI in Tkinter with Python 2.7. I have a frame with about 30 entry boxes among other things. I don't have access to the code right now, but is there a way to check if any or several of the boxes are empty so I can warn the user and ask them if they want to proceed?

Is there a way to go through each of them with a 'for' loop and check the length of each entry. Or is there a command to check if any box is empty?


Solution

  • You can get the content of the entry using Tkinter.Entry.get method.

    Check the length of the entry using len function and get method:

    if len(entry_object.get()) == 0:  # empty!
        # do something
    

    or more preferably:

    if not entry_object.get(): # empty! (empty string is false value)
        # do something
    

    Use for loop to check several entry widgets:

    for entry in entry_list:
        if not entry_object.get():
            # empty!
    

    You should populate entry_list beforehand.

    Manually:

    entry_list = []
    
    entry = Entry(...)
    ...
    entry_list.append(entry)
    
    entry = Entry(...)
    ...
    entry_list.append(entry)
    

    or, using winfo_children method (to get all entries):

    entry_list = [child for child in root_widget.winfo_children()
                  if isinstance(child, Entry)]