Updating Labels in Tkinter with for loop

2024/10/14 8:27:39

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 = 1
mover = 227
for items in range(10):item_values = str(list1[list1_placer] + " " +list2[list2_placer])Label(canvas1, width="100", height="2",textvariable=labe11).place(x=200,y=mover)labe11.set(item_values)list1_placer = list1_placer +1list2_placer = list2_placer +1mover = mover +50

Where list1 and list2 are lists containing strings or integers from a separate function and have more than 10 items and only the first 10 are wanted.

Currently this just prints the last item in the list on 10 separate labels. Thanks in advance!

Answer

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, StringVarroot = 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()
https://en.xdnf.cn/q/117979.html

Related Q&A

Paginate results, offset and limit

If I am developing a web service for retrieving some album names of certain artist using an API, and I am asked:The service should give the possibility to paginate results. It should support ofset= and…

Improve code to find prime numbers

I wrote this python code about 3 days ago, and I am stuck here, I think it could be better, but I dont know how to improve it. Can you guys please help me?# Function def is_prime(n):if n == 2 or n == …

How to read the line that contains a string then extract this line without this string

I have a file .txt that contains a specific line, like thisfile.txt. . T - Python and Matplotlib Essentials for Scientists and Engineers . A - Wood, M.A. . . .I would like to extract lines that contain…

Python: How to access and iterate over a list of div class element using (BeautifulSoup)

Im parsing data about car production with BeautifulSoup (see also my first question):from bs4 import BeautifulSoup import stringhtml = """ <h4>Production Capacity (year)</h4>…

What should I worry about Python template engines and web frameworks? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, argum…

Value Search from Dictionary via User Input

I have written the following code for getting an output of the various districts located in the given city and their respective postal codes. I want my code to be able to receive input from the user (D…

Read and aggregate data from CSV file

I have a data file with the following format:name,cost1,cost1,cost1,cost2,cost3,cost3, X,2,4,6,5,6,8, Y,0,3,6,5,4,6, . . ....Now, what I would like to do is to convert this to a dictionary of dictionar…

nltk cant using ImportError: cannot import name compat

This is my codeimport nltk freq_dist = nltk.FreqDist(words) print freq_dist.keys()[:50] # 50 most frequent tokens print freq_dist.keys()[-50:] # 50 least frequent tokensAnd I am getting this error mess…

Fitting and Plotting Lognormal

Im having trouble doing something as relatively simple as:Draw N samples from a gaussian with some mean and variance Take logs to those N samples Fit a lognormal (using stats.lognorm.fit) Spit out a n…

Is there any way to install nose in Maya?

Im using Autodesk Maya 2008 on Linux at home, and Maya 2012 on Windows 7 at work. Most of my efforts so far have been focused on the former. I found this thread, and managed to get the setup there work…