3

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 ?

Rafa
  • 31
  • 1
  • 2
    Side note, you should consistently use 4 spaces for indentation, it's clearer to read that way. – SuperBiasedMan Aug 27 '15 at 10:59
  • are *btns in startAutomation meant to be your 'Stop', 'Pause', 'Continue' buttons? your code there is using things defined somewhere we can't see – Caleth Aug 27 '15 at 12:46

1 Answers1

2

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")
leo
  • 21
  • 2
  • Hey leo thanks a lot! I tried to implement also the "pause" and "continue" function but it's not working. I will post the loop and what i have done. maybe you know what's missing or how to implement it ? I would appeciate it. – Rafa Aug 27 '15 at 19:26
  • @Rafa you need to add only one line in he while loop while True: last_pressed = self.lastPressed('get', None) ti.sleep(1) if last_pressed == 'Continue': break – leo Aug 29 '15 at 09:07