Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python how to proof a boolean expression in a loop and cancel loop when button clicked

Hello i have a Python program with a function "startAutomation" and inside this funtion i have the loop:

for i,j in zip(int_datadelta, int_timedelta):
  Att1.SetAttenuation(i) # Set attenuation
  print('Set Attenuation: ',i)
  lcd.display(i)
  time.sleep(j)

I have 4 buttons "Start", "Stop","Pause" and "Continue". I am able to start the function "startAutomation" with the button "Start" but the other buttons i can't get working. I need something to check the loop (boolean) and if "Stop" is clicked the loop should stop; when "Pause" is clicked the loop should pause as long as the button "Continue" is clicked or aborted when "Stop" is clicked. This is my code so far:

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

I tried several things whith a while loop but i can't get it working. It would be glad if someone could help ?

like image 324
Rafa Avatar asked Sep 26 '26 16:09

Rafa


1 Answers

You may create a function that sets and returns a variable contains a string of the last pressed button and make actions upon the value of the variable .

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")
like image 66
leo Avatar answered Sep 29 '26 04:09

leo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!