如何在python方法调用上创建循环

如何在python方法调用上创建循环,python,Python,我正在编写一个脚本,使用测试框架在这些标签上重新创建用户的操作。我正在使用以下命令: listBox.LabelButton.Keys("[Enter]") winword.WaitProcess("winword", 2000) listBox.LabelButton2.Keys("[Enter]") winword.WaitProcess("winword", 2000) listBox.LabelButton3.Keys("[Enter]") winword.WaitProcess("wi

我正在编写一个脚本,使用测试框架在这些标签上重新创建用户的操作。我正在使用以下命令:

listBox.LabelButton.Keys("[Enter]")
winword.WaitProcess("winword", 2000)
listBox.LabelButton2.Keys("[Enter]")
winword.WaitProcess("winword", 2000)
listBox.LabelButton3.Keys("[Enter]")
winword.WaitProcess("winword", 2000)
一直到
listBox.LabelButton5
。我如何通过迭代来最小化Python上的冗余

我试过了

listbox.LabelButton.Keys("[Enter]")
winword.WaitProcess("winword",2000)
for i in range (2,6):
   listBox.LabelButton + str(i).Keys("[Enter]")
   winword.WaitProcess("winword", 2000)

在Python中,这在语法上是不正确的。什么是合适的方法?

列出按钮的列表或元组;反复讨论:

button_list = [
    listBox.LabelButton,
    listBox.LabelButton2,
    listBox.LabelButton3,
    ...
]

for button in button_list:
    button.Keys("[Enter]")
    winword.WaitProcess("winword", 2000)

我怀疑你有一些重复的按钮创建过程;这是一个很好的时间把它们都塞进一个列表。

把你的按钮列成一个列表或元组;反复讨论:

button_list = [
    listBox.LabelButton,
    listBox.LabelButton2,
    listBox.LabelButton3,
    ...
]

for button in button_list:
    button.Keys("[Enter]")
    winword.WaitProcess("winword", 2000)

我怀疑你有一些重复的按钮创建过程;这是一个很好的时间把它们都塞进一个列表。

也许是重复的?你在用什么?Python3.x或Python2.7—为什么要标记不使用的另一个?可能是重复的?您使用的是什么?Python3.x或Python2.7——为什么要分别标记未使用的另一个?