Python如何证明循环中的 boolean 表达式并在单击按钮时取消循环

标签 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 个按钮“开始”、“停止”、“暂停”和“继续”。我可以使用“开始”按钮启动“startAutomation”功能,但我无法使用其他按钮。我需要一些东西来检查循环( boolean 值),如果单击“停止”,循环应该停止;单击“暂停”时,只要单击“继续”按钮,循环就会暂停,或者在单击“停止”时中止。 到目前为止,这是我的代码:

# 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")

关于Python如何证明循环中的 boolean 表达式并在单击按钮时取消循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32247480/

相关文章:

c++ - 懒惰、重载的 C++ && 运算符?

c++ - if 的感觉——这怎么可能起作用

python - 构建一个大型 python 存储库,不导入所有内容

Grails 从 View 调用 Controller 方法

python - Python安装libs文件夹中的python3.lib和python3x.lib有什么区别?

ruby - 是否保证 Ruby 哈希文字的顺序?

java - While 与 For 循环中的变量范围

python - 在没有我告诉它的情况下返回 true 或 false ... python 3

python - 检查列内的字符串错误行为 - Pandas

python - 如何从位于名为 '@file_directory' 的文件目录或具有特殊字符的目录中的模块导入 python 类?