Retrieving Tktinter Entry Values where GUI is Variable
Since you define the entries like so:
idx = 0
for i in list:
globals()['label%s' % idx] = Label(root, text=i).grid(row=(idx+1), column=0, sticky=W, padx=12)
globals()['entry%s' % idx] = Entry(root, text=i).grid(row=(idx+1), column=1, sticky=W, padx=12)
idx += 1
You need to get the values in the same way so:
for i in range(len(list)):
globals()["entry_get" + str(i)] = globals()['entry%s' % i].get()
This will save entry 1 to entry_get1
and so on.
If you want to save them to abc etc. you can do:
idx = 0
for i in 'abcdefghijklmno':
globals()[i] = globals()['entry%s' % idx].get()
idx+=1
Your function could look like this:
def upload_prices():
global list
for i in range(len(list)):
globals()['entry_get' + str(i)] = globals()['entry%s' % i].get()
I would not recommend using the list
keyword as a variable though.
Also, upload_prices
is a button and function, that will prove not good. change one or the other like so:
upload_prices_button = Button(root, text="Upload Prices", command=upload_prices)
upload_prices_button.grid(row=16, column=0, padx=10, pady=10)
You also cannot define and grid the entries at the same time so here is the full working code:
from tkinter import *
...
root = Tk()
root.title('Title')
title_label = Label(root, text="Inputs", font=("Helvetica", 14))
title_label.grid(row=0, column=0, columnspan=2, pady="8")
My_list = [i for i in range(10)]
def upload_prices():
for i in range(len(My_list)):
globals()['entry_get' + str(i)] = globals()['entry ' + str(i)].get()
idx = 0
for i in My_list:
globals()['label ' + str(idx)] = Label(root, text=i)
globals()['label ' + str(idx)].grid(row=(idx+1), column=0, sticky=W, padx=12)
globals()['entry ' + str(idx)] = Entry(root, text=i)
globals()['entry ' + str(idx)].grid(row=(idx+1), column=1, sticky=W, padx=12)
print(globals()['entry ' + str(idx)])
idx += 1
upload_prices_button = Button(root, text="Upload Prices", command=upload_prices)
upload_prices_button.grid(row=16, column=0, padx=10, pady=10)
root.mainloop()
NOTE: I changed list
to My_list
.
Another point: I would recommend you a dictionary instead of globals()
:
My_Dict["entry" + str(i)] = Entry()