Python如何在循环中证明布尔表达式并在单击按钮时取消循环
您好,我有一个带有函数“startAutomation”的Python程序,在这个函数中我有一个循环:Python如何在循环中证明布尔表达式并在单击按钮时取消循环,python,loops,boolean,qpushbutton,Python,Loops,Boolean,Qpushbutton,您好,我有一个带有函数“startAutomation”的Python程序,在这个函数中我有一个循环: for i,j in zip(int_datadelta, int_timedelta): Att1.SetAttenuation(i) # Set attenuation print('Set Attenuation: ',i) lcd.display(i) time.sleep(j) 我有4个按钮“开始”、“停止”、“暂停”和“继续”。我可以用“开始”按钮启动“start
for i,j in zip(int_datadelta, int_timedelta):
Att1.SetAttenuation(i) # Set attenuation
print('Set Attenuation: ',i)
lcd.display(i)
time.sleep(j)
我有4个按钮“开始”、“停止”、“暂停”和“继续”。我可以用“开始”按钮启动“startAutomation”功能,但其他按钮无法工作。我需要一些东西来检查循环(布尔值),如果单击“停止”,循环应该停止;单击“暂停”时,只要单击“继续”按钮,循环就应暂停;单击“停止”时,循环应中止。
这是我目前的代码:
# start button
def startAutomation(self, lcd, browse_le, rssi_le, add_le, *btns):
int_timedelta =[]
int_datadelta =[]
for i,j in zip(int_datadelta, int_timedelta):
Att1.SetAttenuation(i) # Set attenuation
print('Set Attenuation: ',i)
lcd.display(i)
time.sleep(j)
def parsed():
btn3 = QtWidgets.QPushButton('Start')
btn4 = QtWidgets.QPushButton('Stop')
btn10 = QtWidgets.QPushButton('Pause')
btn11 = QtWidgets.QPushButton('Continue')
btn3.clicked.connect(functools.partial(self.startAutomation, lcd, le, le4, le5, rbtn, rbtn2, rbtn3))
btn4.clicked.connect(functools.partial(self.stopAutomation))
btn10.clicked.connect(functools.partial(self.pauseAutomation))
btn11.clicked.connect(functools.partial(self.continueAutomation))
hbox3.addWidget(btn3)
hbox3.addWidget(btn4)
hbox3.addWidget(btn10)
hbox3.addWidget(btn11)
...
return vbox
# stop button
def stopAutomation(self):
print ("Stop")
# pause button
def pauseAutomation(self):
print ("Pause")
# continue button
def continueAutomation(self):
print ("Continue")
我尝试了几件带有while循环的事情,但都没能成功。如果有人能帮忙,我们会很高兴的。您可以创建一个函数,用于设置并返回一个包含上次按下按钮字符串的变量,并对该变量的值执行操作
def lastPressed(self, what, last_pressed):
if what == 'set':
self.last_pressed = last_pressed
elif what == 'get':
return self.last_pressed
# start button
def startAutomation(self, lcd, browse_le, rssi_le, add_le, *btns):
self.lastPressed('set', "Start")
int_timedelta =[]
int_datadelta =[]
for i,j in zip(int_datadelta, int_timedelta):
last_pressed = self.lastPressed('get', None)
if last_pressed == 'Start':
Att1.SetAttenuation(i) # Set attenuation
print('Set Attenuation: ',i)
lcd.display(i)
time.sleep(j)
elif last_pressed == 'Stop':
...
elif last_pressed == 'Pause':
...
elif last_pressed == 'Continue':
...
def parsed():
btn3 = QtWidgets.QPushButton('Start')
btn4 = QtWidgets.QPushButton('Stop')
btn10 = QtWidgets.QPushButton('Pause')
btn11 = QtWidgets.QPushButton('Continue')
btn3.clicked.connect(functools.partial(self.startAutomation, lcd, le, le4, le5, rbtn, rbtn2, rbtn3))
btn4.clicked.connect(functools.partial(self.stopAutomation))
btn10.clicked.connect(functools.partial(self.pauseAutomation))
btn11.clicked.connect(functools.partial(self.continueAutomation))
hbox3.addWidget(btn3)
hbox3.addWidget(btn4)
hbox3.addWidget(btn10)
hbox3.addWidget(btn11)
...
return vbox
# stop button
def stopAutomation(self):
self.lastPressed ('set', "Stop")
# pause button
def pauseAutomation(self):
self.lastPressed ('set', "Pause")
# continue button
def continueAutomation(self):
self.last_pressed ('set', "Continue")
请注意,您应该始终使用4个空格进行缩进,这样读起来更清晰。startAutomation中的*BTN是您的“停止”、“暂停”、“继续”按钮吗?你的代码使用了我们看不到的东西嘿,利奥,非常感谢!我也试着实现“暂停”和“继续”功能,但它不起作用。我将发布循环和我所做的事情。也许你知道缺少什么或者如何实现它?我会喜欢它。@Rafa如果为True,你只需要在while循环中添加一行:last_pressed=self.lastppressed('get',None)ti.sleep(1)如果last_pressed='Continue':break