问题描述
以下是我的脚本.基本上,它会要求用户在输入框中输入一个数字.用户输入一个数字并单击确定"后,它会根据用户在输入框中输入的数字为您提供标签+按钮的组合.
The following is my script. Basically, it will ask the user to input a number into the Entry box. Once the user enter a number and click OK, it will give you combination of Labels+Buttons depends on the number that user typed in to the Entry box.
from Tkinter import *
root=Tk()
sizex = 600
sizey = 400
posx = 0
posy = 0
root.wm_geometry("%dx%d+%d+%d" % (sizex, sizey, posx, posy))
def myClick():
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):
Mylabel=Label(myframe,text=" mytext "+str(i)).place(x=10,y=10+(30*i))
Button(myframe,text="Accept").place(x=70,y=10+(30*i))
mybutton=Button(root,text="OK",command=myClick)
mybutton.place(x=420,y=10)
myvalue=Entry(root)
myvalue.place(x=450,y=10)
root.mainloop()
通常,当我创建一个标签小部件时,我会做这样的事情
Normally, when i create a label widget, i would do something like this
mylabel=Label(root,text='mylabel')
mylabel.pack()
所以当我想稍后更改标签的文本时,我可以简单地执行此操作
So when i want to change the text of my label later on i can just simply do this
mylabel.config(text='new text')
但是现在,由于我正在使用 for 循环一次创建所有标签,那么在创建标签后是否还有解决各个标签的问题?例如,用户在输入框中输入5",程序会给我 5 个标签 + 5 个按钮.无论如何,我是否可以更改单个标签的属性(即 label.config(..))?
But now, since i am using for loop to create all labels at once, is there anyway to address the individual labels after the labels has been created?For example, the user typed in '5' into the entry box and the program will give me 5 lables + 5 buttons. Is there anyway for me to change the properties (ie, label.config(..)) of the individual labels?
推荐答案
好的!只需列出标签列表,在每个标签上调用 place
,然后您可以稍后引用它们并更改它们的值.像这样:
Sure! Just make a list of labels, call place
on each one, and then you can reference them later and change their values. Like so:
from Tkinter import *
root=Tk()
sizex = 600
sizey = 400
posx = 0
posy = 0
root.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
调用分成自己的一行,就像上面的代码一样.
Also note! In the assignment Mylabel=Label(myframe,text=" mytext "+str(i)).place(x=10,y=10+(30*i))
in the original code, that call sets Mylabel
to None, since the place
method returns None. You want to separate the place
call into its own line, like in the code above.
这篇关于Tkinter:由 for 循环创建的寻址标签小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!