[英]Python tkinter: configure multiple labels with a loop
I have a window with multiple labels.我有一个带有多个标签的 window。 Instead of configuring each label individually, I want to use a for loop to configure them.我不想单独配置每个 label,而是想使用 for 循环来配置它们。
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'.基本上,我从下面的代码中得到的是所有标签都显示文本“问题 #3”,但我希望每个 label label 相应地显示正确的文本 - 所以 label1 需要有文本“问题 #1”,label2 需要显示“问题 #2”,label3 需要显示“问题 #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()
The easiest way to do so by only modifying your code will involve using zip.仅通过修改代码来实现此目的的最简单方法将涉及使用 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()
this works for me:这对我有用:
result of code is given below;代码结果如下;
code explain: I have one recipe that has a 3 foods and one drink for the small buffet.代码解释:我有一个食谱,其中包含 3 种食物和一种用于小型自助餐的饮料。 I want pide (food)+ayran (drink), some body want kebap(food) + ayran etc.我想要pide(食物)+ayran(饮料),有些人想要kebap(食物)+ayran等。
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()
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.