I have trouble returning results from Entry in tkinter. I do not use it immediately after creating tne Entry:
import tkinter as tk window = tk.Tk() window.title('Enchantment generator') window.geometry('800x600+400+300') window.resizable(False, False) window.entrylist = [] window.cbuttlist = [] res_lvl_mas = [] res_mas = [] def enchwind(chk_ench, ench, txt_ench, lvl_ench, ench_name, row, column): chk_ench = tk.BooleanVar() chk_ench.set(False) ench = tk.Checkbutton(window, text=ench_name, var=chk_ench) ench.grid(row=row, column=column, sticky='w') txt_ench = tk.Entry(window, width=3) txt_ench.grid(row=row, column=column+1) window.entrylist.append(txt_ench) window.cbuttlist.append(chk_ench) def resstr(lvl, resmas): res = '' list = ['/give @p ', '{', 'Enchantments:['] for i in range(len(lvl)): list.append('{id:"minecraft:') list.append(resmas[i]) list.append('",lvl:') list.append(lvl[i]) if i != len(lvl) - 1: list.append('}, ') else: list.append('}') list.append(']}') for i in range(len(list)): res += list[i] return res ench_mas = [] name_mas = ['Aqua affinity', 'Bane of arthropods', 'Binding curse', 'Blast protection', 'Channeling', 'Depth strider', 'Efficiency', 'Feather falling', 'Fire aspect', 'Fire protection', 'Flame', 'Fortune', 'Frost walker', 'Impaling', 'Infinity', 'Knockback', 'Looting', 'Loyalty', 'Luck of the sea', 'Lure', 'Mending', 'Power', 'Projective protection', 'Protection', 'Punch', 'Respiration', 'Riptide', 'Sharpness', 'Silk touch', 'Smite', 'Sweeping', 'Thorns', 'Unbreaking', 'Vanishing curse'] for i in range(34): ench_mas.append([]) for i in range(34): for j in range(7): ench_mas[i].append(0) for i in range(34): ench_mas[i][4] = name_mas[i] for i in range(17): ench_mas[i][5] = i+1 ench_mas[i][6] = 0 for i in range(17, 34): ench_mas[i][5] = i-16 ench_mas[i][6] = 2 ench_list = tk.Label(window, text='Choose the enchantments:') ench_list.grid(row=0, column=0, columnspan=4) result = tk.Label(window, text='None') result.grid(row=19, column=0, columnspan=4) for i in range(34): enchwind(ench_mas[i][0], ench_mas[i][1], ench_mas[i][2], ench_mas[i][3], ench_mas[i][4], ench_mas[i][5], ench_mas[i][6]) def clicked(): result.configure(text=txt) for i in range(34): if window.cbuttlist[i].get(): res_lvl_mas.append(window.entrylist[i].get()) res_mas.append(ench_mas[i][4]) txt = resstr(res_lvl_mas, res_mas) btn = tk.Button(window, text='Generate!', command=clicked) btn.grid(column=0, row=18, columnspan=4) window.mainloop()
This code creates commands for enchanting minecraft items. As you can see, i have 34 entrys so I decided to store their parameters as a list of lists. In the end of the program, when i wanted to get the numbers in entrys, it always returns 0. The interface looks like that: Interface. I suppose those may be zeros left from first part of code, just to define list parameters. Shoul i move something in the code or is it incorrect at all?
Advertisement
Answer
The problem lies in the fact that you don’t remember the Entry widgets. You just store them to a local variable in function enchwind()
and don’t store them anywhere. They are created but you lose all reference.
You could use a list as an attribute of window
to store them:
In the definition of your window, add the line:
window.entrylist = []
Then, in our enchwind()
function, you can store the Entry to this list:
txt_ench = tk.Entry(window, width=3) txt_ench.grid(row=row, column=column+1) window.entrylist.append(txt_ench)
Now, you have a list of 34 items holding all your text Entries. You can access them by just getting the content of one of the elements of this list (i.e. value = window.entrylist[5].get()
would get you the contents of the 6th Entry widget.