Python 如何让函数在下面的代码中配置tkinter中的正确标签?

Python 如何让函数在下面的代码中配置tkinter中的正确标签?,python,tkinter,label,Python,Tkinter,Label,我可以在单独创建标签时执行此操作,这样每个函数都可以在调用它的按钮旁边配置标签。当我尝试使用zip创建标签时,所有函数都会配置最后一个标签。我觉得我在某个地方错过了一步。我如何做到这一点,使每个函数配置正确的标签,而不必单独创建每个标签 from tkinter import * root = Tk() root.configure(bg='light blue', height=1200, width=1200) left_frame = Frame(root, width=250, he

我可以在单独创建标签时执行此操作,这样每个函数都可以在调用它的按钮旁边配置标签。当我尝试使用zip创建标签时,所有函数都会配置最后一个标签。我觉得我在某个地方错过了一步。我如何做到这一点,使每个函数配置正确的标签,而不必单独创建每个标签

from tkinter import *

root = Tk()
root.configure(bg='light blue', height=1200, width=1200)

left_frame = Frame(root, width=250, height=500, bg='grey')
left_frame.place(x=10, y=10, relx=0.01, rely=0.01,)

def Create_Test():
    l.configure(text='Test result number 1')
def Create_test2():
    l.configure(text="Test result number 2")
def Create_test3():
    l.configure(text="Test result number 3")

text=['butt1', 'butt2', 'butt3','butt4']
Comm=[Create_Test, Create_test2, Create_test3]

for t,z in zip( text, Comm):
    butt=Button(left_frame, text=t, width=12, bg='red', fg='black', command=z, relief='ridge')
    butt.pack(side=TOP)

labels = ['Label1', 'Label2', 'Label3']
ys = [20, 47, 74]

for l,z in zip(labels,ys):
    l=Label(root, text="", width=25, bg='blue', fg='black',relief='ridge')
    l.place(x=125, y=z)

root.mainloop()
试试lambda魔术

改变

butt=Button(left_frame, text=t, width=12, bg='red', fg='black', command=z, relief='ridge')

请注意,您还错误地试图更改标签的文本。您可以重复使用
l
来表示代码中的许多不同内容。下面我提供的示例为您提供了一种将每个标签存储在字典中的方法,以便您以后可以更改它们的内容

from tkinter import *

root = Tk()
root.configure(bg='light blue', height=1200, width=1200)

left_frame = Frame(root, width=250, height=500, bg='grey')
left_frame.grid()

def Create_Test():
    print(1)
    label_dict['Label1'].configure(text='Test result number 1')
def Create_test2():
    print(2)
    label_dict['Label2'].configure(text="Test result number 2")
def Create_test3():
    print(3)
    label_dict['Label3'].configure(text="Test result number 3")

text=['butt1', 'butt2', 'butt3']
labels = ['Label1', 'Label2', 'Label3']
Comm=[Create_Test, Create_test2, Create_test3]

label_dict = {}

for idx,(button_text,label_text,fcn) in enumerate(zip(text,labels, Comm)):
    butt=Button(left_frame, text=button_text, width=12, bg='red', fg='black', command=lambda z=fcn:z(), relief='ridge')
    butt.grid(row=idx,column=0)
    currentLabel=Label(left_frame, text="", width=25, bg='blue', fg='black',relief='ridge')
    currentLabel.grid(row=idx,column=1)
    label_dict[label_text] = currentLabel


root.mainloop()
我使用了.grid,而不是您当前的位置和包装组合。我还有一个单圈来画按钮和标签,因为你可以把两个以上的易拉罐拉在一起。

试试lambda魔术

改变

butt=Button(left_frame, text=t, width=12, bg='red', fg='black', command=z, relief='ridge')

请注意,您还错误地试图更改标签的文本。您可以重复使用
l
来表示代码中的许多不同内容。下面我提供的示例为您提供了一种将每个标签存储在字典中的方法,以便您以后可以更改它们的内容

from tkinter import *

root = Tk()
root.configure(bg='light blue', height=1200, width=1200)

left_frame = Frame(root, width=250, height=500, bg='grey')
left_frame.grid()

def Create_Test():
    print(1)
    label_dict['Label1'].configure(text='Test result number 1')
def Create_test2():
    print(2)
    label_dict['Label2'].configure(text="Test result number 2")
def Create_test3():
    print(3)
    label_dict['Label3'].configure(text="Test result number 3")

text=['butt1', 'butt2', 'butt3']
labels = ['Label1', 'Label2', 'Label3']
Comm=[Create_Test, Create_test2, Create_test3]

label_dict = {}

for idx,(button_text,label_text,fcn) in enumerate(zip(text,labels, Comm)):
    butt=Button(left_frame, text=button_text, width=12, bg='red', fg='black', command=lambda z=fcn:z(), relief='ridge')
    butt.grid(row=idx,column=0)
    currentLabel=Label(left_frame, text="", width=25, bg='blue', fg='black',relief='ridge')
    currentLabel.grid(row=idx,column=1)
    label_dict[label_text] = currentLabel


root.mainloop()

我使用了.grid,而不是您当前的位置和包装组合。此外,我还有一个用于绘制按钮和标签的单循环,因为您可以将两个以上的iterables压缩在一起。

您对三个标签使用了相同的变量
l
,因此在for循环之后,
l
将引用最后一个标签。您可以使用列表存储这三个标签,并访问这三个函数中相应的标签。谢谢。读了你的评论后,我设法使事情进展顺利。感谢您的帮助。您对三个标签使用了相同的变量
l
,因此在for循环之后,
l
将引用最后一个标签。您可以使用列表存储这三个标签,并访问这三个函数中相应的标签。谢谢。读了你的评论后,我设法使事情进展顺利。谢谢你的帮助,谢谢斯科蒂。我对这个很陌生,你的回答中有一些有趣的东西,我以前没有真正探讨过。期待着试一试。
lamba
比它看起来更可怕。如果它被称为
createFunction
,它会更加明显,不像black magicThanks Scotty。我对这个很陌生,你的回答中有一些有趣的东西,我以前没有真正探讨过。期待着试一试。
lamba
比它看起来更可怕。如果它被称为
createFunction
,它将更加明显,不像黑魔法