我想使用for循环来创建这样的东西:
my_form = form.Form(
form.Button("btn", id="btn0", value = "0", html="One!", class_="btnZero")
form.Button("btn", id="btn1", value = "1", html="Two!", class_="btnOne")
form.Button("btn", id="btn2", value = "2", html="Three!", class_="btnThree")
)
我怎么能实现这个目标?每次循环时,都必须在my_form和form.Form()中创建一个form.button,每个都有不同的参数。所以就像一个值为0,另一个为1,另一个为2等。而且HTML是相同的。
答案 0 :(得分:0)
您可以按照以下方式执行此操作:
lst = ['Zero','One','Two','Three']
for i in range(3):
form.Button("btn", id="btn"+str(i), value = str(i), html=lst[i+1]+'!', class_="btn"+str(i))
答案 1 :(得分:0)
html = lst = ['Zero','One','Two','Three']
my_form = form.Form(*[form.Button("btn", id="btn" + str(i), value = i, html=html[i], class_="btn" +str(i)) for i in range(3)])
答案 2 :(得分:0)
试试这个:
counter = ['Zero','One','Two','Three']
my_form = form.Form(*[form.Button("btn", id="btn{}".format(i), value=str(i), html="{}!".format(counter[i+1]), class="_btn{}".format(counter[i]) for i in range(3)])