0

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 labels

labels = [label_1, label_2, label_3]

for x in nums:
    jk = string + x

    for l in labels:
        l.config(text=jk)

root.mainloop()

3 Answers 3

1

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 this
    jk = string + x
    l.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 nums
    jk = string + x
    label = Label(root,text=jk) #set your text
    label.pack()
    labels.append(label) #appends the label to the list for further use

root.mainloop()
Sign up to request clarification or add additional context in comments.

Comments

0

this works for me:

result of code is given below;

enter image description here

code explain: I have one recipe that has a 3 foods and one drink for the small buffet. I want pide (food)+ayran (drink), some body want kebap(food) + ayran etc.

Is it possible the customer to see three different foods plus a drink in a row?, yes of course!. Then I coded something like below. I finally came to solution after tried many times.

class Class1:
    def __init__(self,master,pide=14,ayran=2,kebap=16,sucuk=12): #Class variables
        self.master=master
        master.title("A simple recipe")
        self.ayran=ayran
        self.pide=pide
        self.kebap=kebap
        self.sucuk=sucuk
    

    def hesapla(self):  

        pa=self.pide+self.ayran   #food +drink
        ka=self.kebap+self.ayran
        sa=self.sucuk+self.ayran
        #print (pa)
        fiyatlar= [pa,ka,sa]  #arrays of foods+drinks
    
        for x in range(3):

            L = tk.Label( text=fiyatlar[x])    #labels for price tags in a 3 rows
            L.grid(row=x,column=1)

        yazilar=["pide+ayran=","kebap+ayran=","sucuk+ayran="]  #names of foods and drinks
        for x in range(3):

            L2 = tk.Label( text=yazilar[x])
            L2.grid(row=x,column=0)
        
        for x in range(3):

            L3 = tk.Label( text="$")   # $ sign near the price tags
            L3.grid(row=x,column=2)

    def main(): # let codes work
         uyg = Tk()
         hes1 = Class1(uyg)
         hes1.hesapla()
         uyg.mainloop()

     if __name__ == '__main__': #if you want use another .py file, call it.
     main()

2 Comments

Hi Alphard. Thanks for your answer. Usually answers with an explanation are more welcomed here. Would you like to update your answer to provide short explanation?
Yes of course. When I paste my code I have hurry. I add explain near my code.
0
import tkinter
from tkinter import *


root = Tk()

class Class1:

  def __init__(self,cam):

    self.cam = cam
    cam.title("Abcd egfh")

    self.frame1=Frame(cam, padx=5)
    self.frame1.grid(column=0,row=1)

    self.labels= ["LABELS","label 1","label 2","label 3","label 4"]
    Editlabel=Label(self.frame1,text="EDITS")
    Editlabel.grid(row=0,column=1)
    self.edits= ["ed1","ed2","ed3","ed4"]

    for x in range(5):

        self.L = Label( self.frame1,text=self.labels[x])   
        self.L.grid(row=x,column=0)

class Class2(Class1):

  def __init__(self,ws):

    super().__init__(ws)

    for x in range(1,5):

        self.L = Entry(self.frame1)   
        self.L.grid(row=x,column=1)


 root.geometry("200x150")



 my_gui = Class1(root)

 my_gui2 = Class2(root)

 root.mainloop()

1 Comment

Please consider adding some explanation to the source code explaining how it solves the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.