Home > Blockchain >  Retrieving Tktinter Entry Values where GUI is Variable
Retrieving Tktinter Entry Values where GUI is Variable

Time:01-20

I have Tkinter GUI with some entry boxes, and a button at the bottom that I want to click to retrieve the values from the boxes.

The tricky part is, that the number of entry boxes in my GUI is not fixed.

Earlier in my code I generate a list and the number of items in this list is the number of entry boxes I am generating, using the following:

from tkinter import *

...

root = Tk()

root.title('Title')

title_label = Label(root, text="Inputs", font=("Helvetica", 14)).grid(row=0, column=0, columnspan=2, pady="8")

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

upload_prices = Button(root, text="Upload Prices").grid(row=16, column=0, padx=10, pady=10)

root.mainloop()

Now I'm a little bit stuck however, because I'm struggling to defind a command function which is also variable in order to use .get() on each of my "entry0, entry1, entry2" boxes - however many there are. I have tried:

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")

def upload_prices():
    global alist
    for i in range(len(alist)):
        globals()['entry_get'   str(i)] = globals()['entry%s' % i].get()

idx = 0
for i in alist:
    globals()['label%s' % idx] = Label(root, text=i)
    globals()['label%s' % idx].grid(row=(idx 1), column=0, sticky=W, padx=12)
    globals()['entry%s' % idx] = Entry(root, text=i)
    globals()['entry%s' % idx].grid(row=(idx 1), column=1, sticky=W, padx=12)
    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()

Any help would be greatly appreciated!

CodePudding user response:

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()
  •  Tags:  
  • Related