Search code examples
pythonlisttkintertkinter-entry

How to obtain all available Entry values


I have created a pop up that would ask for entry, and the amount of entries would depend on the information given.

self.e = Entry(self.top, bd = 5)
self.e.grid(column = 1, row = 0)
row = 2
for d in extra:
   self.e2 = Entry(self.top, bd = 5)
   self.e2.grid(column = 1, row = row)
   row = row + 1

def ok(self):
   new = self.e.get().strip()

Function ok would be called by a button and then it would return the values. How do I return a list of values from an unknown amount of entries?

Python 2.7


Solution

  • Normally, you would put the entries in a list:

    from Tkinter import *
    
    class App(object):
    
        def __init__(self, top):
            self.top = top
            self.ok_button = Button(self.top, text='OK', command=self.ok)
            self.make_entries()
    
        def make_entries(self): 
            self.entries = []
            for d in extra:
                e2 = Entry(self.top, bd = 5)
                e2.grid(column = 1, row = row)
                self.entries.append(e2)
                row += 1
    
        def ok(self):
            values = [e.get().strip() for e in self.entries]
    
    root = Tk()
    app = App(root)
    root.mainloop()