Skip to content Skip to sidebar Skip to footer

How Can I Create Multiple Tkinter Widgets With Different Names In A Loop?

I should be able to use a loop to do the below instead of writing out more widgets than I'll ever need and shorten my code.This is the way I'm doing it now: db = sqlite3.connect('/

Solution 1:

Instead of having fifteen variables named c1, c2... c15, create a single list which will hold all of your checkbuttons. Do the same for your entries and vars.

checkbuttons = []
entries = []
vars = []
for i in range(numrec):
    results = cursor.fetchone()
    var = IntVar()
    check_button=Checkbutton(frame1,variable=var)
    check_button.grid(row=i,column=0,sticky='nw')
    check_button.config(bg='black')
    entry=Entry(frame1, bg="black", fg="white")
    entry.grid(row=i, column=1, sticky=NW)
    entry.delete(0, END)
    for row in results:
        entry.insert(END, *results)
    checkbuttons.append(check_button)
    entries.append(entry)
    vars.append(var)

Now instead of getting e.g. the sixth entry with e6, you get it with entries[5].

Post a Comment for "How Can I Create Multiple Tkinter Widgets With Different Names In A Loop?"