插入计数器

时间:2016-12-06 15:07:28

标签: python pyqt5

我想在这个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])
...

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}}