Python tkinter: configure multiple labels with a loop

2024/10/15 21:22:02

I have a window with multiple labels. Instead of configuring each label individually, I want to use a for loop to configure them.

Basically, what I get from the below code is all labels are showing the text 'question #3', but I want each label label to show the right text accordingly - so label1 needs to have the text 'question #1', label2 needs to show 'question #2' and label3 needs to show 'question #3'. Can somebody please help.

from tkinter import *root = Tk()string = 'Question #'nums = ['1', '2', '3']#labels
label_1 = Label(root)
label_1.pack()label_2 = Label(root)
label_2.pack()label_3 = Label(root)
label_3.pack()
# end of labelslabels = [label_1, label_2, label_3]for x in nums:jk = string + xfor l in labels:l.config(text=jk)root.mainloop()
Answer

The easiest way to do so by only modifying your code will involve using zip. Your code just have some looping issues.

for x, l in zip(nums,labels): #change your for loops to thisjk = string + xl.config(text=jk)

Writing a concise code involving this: generating the label and the text together could save you many lines of codes. This works the same for your code

from tkinter import *
root = Tk()
string = 'Question #'
nums = ['1', '2', '3']
labels=[] #creates an empty list for your labels
for x in nums: #iterates over your numsjk = string + xlabel = Label(root,text=jk) #set your textlabel.pack()labels.append(label) #appends the label to the list for further useroot.mainloop()
https://en.xdnf.cn/q/117787.html

Related Q&A

How do you create a sitemap index in Django?

The Django documentation is very minimal and I cant seem to get this to work.Currently I have 3 individual sitemaps, and I would like to create a sitemap index for them: (r^sitemap1\.xml$, django.contr…

Numpy Append to Array

I’m building a project for the Raspberry Pi that turns a relay on and off random times in a specific time window. To manage the time slots, I want to use a two-dimensional array that’s generated dail…

Output of numpy.diff is wrong

heres my problem: I am trying to use numpy to compute (numerical) derivatives, but I am finding some problems in the value the function numpy.diff returns (and numpy.gradient as well). What I find is t…

Calculate Fibonacci numbers up to at least n

I am trying to make a function that allows a user to input a number and the result will be a list containing Fibonacci numbers up to the input and one above if the input is not in the series. For examp…

IEC 62056-21, implement the protocol over a gsm connection

The protocol IEC 62056:21 tells us how to deal with enegy meters, its quite easy!The part where I am stuck is the implementation over a GSM data channel. Normally I would set things like:300 baudrate …

Cannot install plyfile in Anaconda

When u=i try to run the commandconda install plyfilein windows command promptFetching package metadata ...........PackageNotFoundError: Package not found: Package missing in current win-64 channels: -…

Expected singleton: stock.move - Odoo v9 community

Im creating a stock.picking from fleet_vehicle_log_services with this method:@api.multi def create_picking(self):self.ensure_one()vals = {move_lines: self.move_lines.id,origin: self.name}picking = self…

Python Kivy screen manager wiget scope

I am trying to control a screen manager from buttons in a separate class, but I cannot figure out what to set on the button on_press: statements.Kivy file:<HeaderSection>:anchor_x: centeranchor_y…

How do the async and await keywords work, exactly? Whats at the end of the await chain?

I have this code:async def foo(x):yield xyield x + 1async def intermediary(y):await foo(y)def bar():c = intermediary(5)What do I put in bar to get the 5 and the 6 out of c?Im asking because the asynci…

Serial port writing style

I am using two libraries to connect with a port, and two of them uses different styles in writing these commands. I want to understand the difference because I want to use the second one, but it result…