当然!只需列出一个标签列表,调用
place每个标签,然后您便可以稍后引用它们并更改其值。像这样:
from Tkinter import *root=Tk()sizex = 600sizey = 400posx = 0posy = 0root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))labels = []def myClick(): del labels[:] # remove any previous labels from if the callback was called before myframe=frame(root,width=400,height=300,bd=2,relief=GROOVE) myframe.place(x=10,y=10) x=myvalue.get() value=int(x) for i in range(value): labels.append(Label(myframe,text=" mytext "+str(i))) labels[i].place(x=10,y=10+(30*i)) Button(myframe,text="Accept").place(x=70,y=10+(30*i))def myClick2(): if len(labels) > 0: labels[0].config(text="Click2!") if len(labels) > 1: labels[1].config(text="Click2!!")mybutton=Button(root,text="OK",command=myClick)mybutton.place(x=420,y=10)mybutton2=Button(root,text="Change",command=myClick2)mybutton2.place(x=420,y=80)myvalue=Entry(root)myvalue.place(x=450,y=10)root.mainloop()另请注意!在
Mylabel=Label(myframe,text=" mytext"+str(i)).place(x=10,y=10+(30*i))原始代码的分配中,该调用设置
Mylabel为None,因为该
place方法返回None。您希望将
place调用分成单独的一行,就像上面的代码一样。



