Python Forum
[PyQt] Loop triggered by button help
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Loop triggered by button help
#1
Hi, I need help. I'm trying to make a loop, triggered by a button, that:

First press: Relay goes on and off intermittently
Second press: Turns relay off
I have a functional button that goes on and off without the intermittent function but when I add the loop in the definition it goes on forever and freezes the program.

I tried to stop it with break but it stops everytime the cycle is done.
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication,QDialog
from PyQt5.uic import loadUi
import RPi.GPIO as gpio
import recu
from time import sleep
import time


load1=22
load2=5
load3=26

gpio.setmode(gpio.BCM)
gpio.setwarnings(False)Your post appears to contain code that is not properly formatted as code. Please indent all code by 4 spaces using the code toolbar button or the CTRL+K keyboard shortcut. For more editing help, click the [?] toolbar icon.



gpio.setup(22,gpio.OUT)
gpio.output(22,gpio.LOW)
gpio.setup(5,gpio.OUT)
gpio.output(5,gpio.LOW)
gpio.setup(26,gpio.OUT)
gpio.output(26,gpio.LOW)



class industrial(QDialog):
    def __init__(self):
        super(industrial,self).__init__()
        loadUi('neo.ui',self)
        self.setWindowTitle('Spectrum')
        self.load_1.clicked.connect(self.load1)
        self.load_2.clicked.connect(self.load2)
        self.load_3.clicked.connect(self.load3)




@pyqtSlot()
def load1(self):
    if gpio.input(load1):
        gpio.output(22,gpio.LOW)
        self.load_1.setText('633 nm Off')
    else:
        gpio.output(22,gpio.HIGH)
        self.load_1.setText('633 nm ON')

def load2(self):
    if gpio.input(load2):
        gpio.output(5,gpio.LOW)
        self.load_2.setText('Neo Push Off')
    else:
        gpio.output(5,gpio.HIGH)
        self.load_2.setText('Neo Push ON')

def load3(self):
    if gpio.input(load3):
        gpio.output(26,gpio.LOW)
        self.load_3.setText('Push Off')
    else:
        gpio.output(5,gpio.HIGH)
        self.load_3.setText('Push ON')

def load3(loop):
    while True:
          gpio.output(26,True)
          time.sleep(0.3)
          gpio.output(26,False)
          time.sleep(0.3) 




app=QApplication(sys.argv)
widget=industrial()
widget.show()
sys.exit(app.exec_())
Reply
#2
Look at QTimer. You could run your relay toggle loop in it's own thread, but I think QTimer may be a better fit.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] How to terminate a loop in python with button in GUI Joni_Engr 6 10,961 Sep-09-2021, 06:33 PM
Last Post: deanhystad
  [Tkinter] Binding Entry box to <Button-3> created in for loop iconit 5 4,874 Apr-22-2020, 05:47 AM
Last Post: iconit
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,947 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] tkinter - unexpected output - don't know how func is triggered before calling omm 8 4,408 Dec-11-2018, 08:09 PM
Last Post: Gribouillis
  [Tkinter] loop function when called from tkinter button click WantedStarling 5 8,905 Jul-13-2018, 06:12 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020