Reez0
Reez0

Reputation: 2689

Get input from dynamically created input

How would I retrieve input if I created a bunch of input boxes like this

expensefields=[]
expenses = [i for i in range(6)]
count=0
for expense in expenses:

    expense = Entry(master)
    expense.grid(row=count,column=2)
    expenseinputs.append(expense)
    count=count+1

I know about using the .get() method, but not sure how to approach this. If I'm doing this the wrong way I'd love to get some pointers.

Upvotes: 0

Views: 52

Answers (1)

Henry Yik
Henry Yik

Reputation: 22493

I had the same problem earlier, and after lots of searching i kind of figured it out. There might be better answers though so i will love to hear better answers too.

For you to use the get() method, you need to store the widget somewhere, say a list.

import tkinter as tk

root = tk.Tk()
expenses = [i for i in range(6)]
count=0
widget = []
for expense in expenses:
    expense = tk.Entry(root)
    expense.grid(row=count,column=2)
    expense.insert(0,"{}".format(count))
    count=count+1
    widget.append(expense)

print (widget[2].get())

root.mainloop()

Now you can use get() on the widgets by passing an index to the list.

Upvotes: 2

Related Questions