Search code examples
python-3.xtkintertkinter-entry

Ever expanding amount of Entries with TKinter


What I'm trying to do is to make a GUI where when you start typing in an entry-box another shows up just beneath the one you are typing in. Then when you start typing in the one that popped up, another pops up. Is this possible with TKinter and Python?

Edit:

So what I currently have is this:

entry1 = StringVar()
numberLabel3 = Label(window, text = "3. External meeting attendees")
r+=1
numberLabel3.grid(column = 0, row = r, sticky = W)
externalAtendeesLabel = Label(window, text = "input name of external meeting atendee: ")
r+=1
externalAtendeesLabel.grid(column = 1, row = r, sticky = E)
externalAtendeesEntry = Entry(window, textvariable = entry1)
externalAtendeesEntry.grid(column = 2, row = r)
#Note to self: Find a smart way of dynamically expanding this "list" of entries

(There is more code above and below this, but this is the relevant code for my question)

where r is a variable I made to make it easier to insert stuff into the middle of my rather long code.

The imports I'm using are:

from tkinter import *
from PIL import ImageTk
from PIL import Image
import os

I use the image modules and OS to insert an image further up in my GUI.

What I was thinking was to make a function that I could somehow setup to check the newest Entry-box, but I've run into the problem that for this to be potentially infinite I would have to dynamically create new variables, so that I can access the information that the user inputs. These variables would save the info just like my entry1 variable does it for the externalAtendeesEntry. I would also have to dynamically make variables for more entries.

How do I dynamically create a potentially infinite amount of variables?

I know that this is kind of a re-post, but the other ones I've found all say that you should use dictionaries, but in that case it can't be infinite. It can only be finite to the point where my dictionary is no longer.


Solution

  • For one, you don't need to use StringVar. It only complicates your code without providing any real value. The other part of the answer is to store the entries in a list.

    For example, create a function called addEntry that creates an entry and adds it to a list:

    entries = []
    ...
    def addEntry():
        entry = tk.Entry(...)
        entry.pack(...)
        entries.append(entry)
    

    To get the values at a later date, just iterate over the list:

    for entry in entries:
        print(entry.get())
    

    With that, you can add entries whenever you want. You could, for example, bind to <Any-KeyRelease> to create a new entry as the user types (being sure to only do it if there isn't already a blank entry). Or, bind to <Return> or <FocusOut>, or on the click of a "new person" button, or however else you decide.