我想在这个python动作中添加我的计数器。
这是我的代码:
with open('cocktails.csv') as csvfile:
readCSV = csv.reader(csvfile, delimiter=',')
x = 1
for row in readCSV:
print(row[1])
widget.btn_ckt1.setText(row[1])
print(settext)
x=x+1
然后我想在btn_ckt'COUNTER'
插入计数器这应该是结果:
widget.btn_ckt1.setText(row[1])
widget.btn_ckt2.setText(row[1])
...
答案 0 :(得分:2)
您要做的是使用计数器生成名称并在with open('cocktails.csv') as csvfile:
reader = csv.reader(csvfile, delimiter=',')
# Using enumerate is "pythonic"
for i, row in enumerate(reader):
# Gives us btn_ckt1, btn_ckt2 and so on...
name = "btn_ckt" + str(i + 1)
# Get attribute
obj = getattr(widget, name)
obj.setText(row[1])
:
{{1}}