Home > Software engineering >  Creating and configuring multiple labels
Creating and configuring multiple labels

Time:10-01

I have a list with some data, that changes:

list=[]

if len(list) < 5:
    list.append(data)
else:
    list.pop(0)
    list.append(data)

removing first item and adding new.

I want tk labels to behave like that list when I click a button:

for i, pk in enumerate(list):
    lab = tk.Label(text=pk)
    lab.grid(row=0, column=i)

It kinda works, but there is a problem. It does not change text in labels it just puts new labels above, so it starts lagging after a while. I tried config but it just overwrites last label.

Then I decided to create multiple labels:

labnames = []

for i in range(5):
    labnames.append('lab'   str(i))

for i, name in enumerate(labnames):
    name = tk.Label()

but it doesn't let me do anything with them later. I could manually create every label, but I'm gonna need a lot of them, there must be a smart way to do that. So how should I solve this problem?

CodePudding user response:

self.labnames = []
for n in range(5):
     lab = tk.Label(self.downframe)
     self.labnames.append(lab)

Perfectly works for my needs

More examples here

CodePudding user response:

Much better like this.

self.labnames = [lab for n in range(5)]
lab = tk.Label(self.downframe)
  
  • Related