I have a tkinter window that am creating labels and entry widgets based on a list made by a database query.
I need to use the values entered in the entry widgets to create a new list to put into another database.
The list from the database has a variable count.
I use a loop to create the labels and entry widgets but can't work out how to name the entry widgets to be able to put the entries into a usable list. Ideally it will have the e.get
from the original query and the list tied to each of the entries.
Heres what i have so far.
#Create frame for Size inputs and labels
sizes_frame = tk.Frame(p,bg='yellow')
sizes_frame.grid(row=2,column=1)
connection = sqlite3.connect('productdata.db')
pro = connection.cursor()
pro.execute("SELECT Size FROM Products WHERE ColourCode = ?", (e.get(),))
connection.commit()
Sizes = pro.fetchall()
keys = Sizes
otherkeys = Sizes
count = 0
othercount = 0
labels=[]
otherlabels=[]
for j,l in enumerate(labels):
l.config(text=str(keys[j]) str(j))
for key in keys:
labels.append(Label(sizes_frame,text=key,font=('Helvatical bold',10)))
if count <10:
labels[count].grid(row = count, column = 1, padx=5, pady= 5)
count =1
else:
labels[count].grid(row = count-10, column = 3, padx=5, pady= 5)
count = 1
for j,l in enumerate(otherlabels):
l.config(text=str(otherkeys[j]) str(j))
for otherkey in otherkeys:
otherlabels.append(Entry(sizes_frame,width= 6))
if othercount<10:
otherlabels[othercount].grid(row = othercount, column = 2, padx=5, pady= 5)
othercount = 1
else:
otherlabels[othercount].grid(row = othercount-10, column = 4, padx=5, pady= 5)
othercount = 1
CodePudding user response:
I'm not sure if I understand problem but maybe you should use dictionary with nested list instead of list
all_labels = { e.get(): [] } # list inside dictionary
all_entries = { e.get(): [] } # list inside dictionary
and later append widgets
all_labels[e.get()].append(label)
all_entries[e.get()].append(entry)
And later you can get all values using for loop and e.get()
for entry in all_entries[e.get()]:
print(entry.get())
Eventually you can use nested dictionares
all_labels = { e.get(): {} } # dictionary inside dictionary
all_entries = { e.get(): {} } # dictionary inside dictionary
and later append widgets
all_labels[e.get()][size] = label
all_entries[e.get()][size] = entry
And later you can get all values using for loop and e.get()
and .items()
for size, entry in all_entries[e.get()].items():
print(size, entry.get())
Minimal example with other changes to make code more readable.
Because words entry
,entries
and label
,labels
and size
,sizes
are very similar (and it can make problems) so I use prefix `all_
sizes_frame = tk.Frame(p, bg='yellow')
sizes_frame.grid(row=2, column=1)
connection = sqlite3.connect('productdata.db')
pro = connection.cursor()
pro.execute("SELECT Size FROM Products WHERE ColourCode = ?", (e.get(),))
connection.commit()
all_sizes = pro.fetchall()
all_labels = { e.get(): {} }
all_entries = { e.get(): {} }
for number, size in enumerate(all_sizes):
label = tk.Label(sizes_frame, text=size, font=('Helvatical bold',10))
entry = tk.Entry(sizes_frame, width=6)
if number < 10:
label.grid(row=number, column=1, padx=5, pady=5)
entry.grid(row=number, column=2, padx=5, pady=5)
else:
label.grid(row=number-10, column=3, padx=5, pady=5)
entry.grid(row=number-10, column=4, padx=5, pady=5)
all_labels[e.get()][size] = label)
all_entries[e.get()][size] = entry
# --- in some function ---
for size, entry in all_entries[e.get()].items():
print(size, entry.get())
# ... add `size` and `entry.get()` to database