Skip to content Skip to sidebar Skip to footer

Updating Labels In Tkinter With For Loop

So I'm trying to print items in a list dynamically on 10 tkinter Labels using a for loop. Currently I have the following code: labe11 = StringVar() list2_placer = 0 list1_placer =

Solution 1:

Just use a distinct StringVar for each Label. Currently, you just pass the same one to all the labels, so when you update it they all update together.

Here's an example. You didn't give a fully runnable program, so I had to fill in the gaps.

from tkinter import Tk, Label, StringVar

root = Tk()

list1 = [1,2,3,4,5]
list2 = [6,7,8,9,10]

for v1, v2 in zip(list1, list2):

    item_values = '{} {}'.format(v1, v2)
    sv = StringVar()
    lbl = Label(root, width="100", height="2",textvariable=sv).pack()

    sv.set(item_values)

root.mainloop()

Post a Comment for "Updating Labels In Tkinter With For Loop"