2

I am working on a Raspberry Pi Project. The Raspi detects when a Laser light barrier has been interrupted.

When this happens, i want to change the text in a PyQt label.

I now have two Python files, which i don't know how to connect together.

The GPIO detection:

import RPi.GPIO as GPIO
import os, time

RECEIVER_PIN = 23

def callback_func(channel):
    if GPIO.input(channel):
        # CHANGE THE PYQT LABEL TEXT

if __name__ == '__main__':

    GPIO.setmode(GPIO.BCM)
    GPIO.setwarnings(False)

    GPIO.setup(RECEIVER_PIN, GPIO.IN)
    GPIO.add_event_detect(RECEIVER_PIN, GPIO.RISING, callback=callback_func, bouncetime=200)

    try:
        while True:
            time.sleep(0.5)
    except:
        GPIO.remove_event_detect(RECEIVER_PIN)

And the PyQt Window:

import sys from PyQt5.QtWidgets import QApplication, QMainWindow from PyQt5.QtCore import QFile from ui_mainwindow import Ui_MainWindow

class MainWindow(QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)


        self.label.setGeometry(QtCore.QRect(0, 19, 831, 461))

if __name__ == "__main__":
    app = QApplication(sys.argv)

    window = MainWindow()
    window.show()



    sys.exit(app.exec_())

How can I connect these together?

NoDataDumpNoContribution
  • 10,591
  • 9
  • 64
  • 104
Tristan K.
  • 41
  • 6
  • 1
    I guess you have to use signal/slots in PyQt to ensure that the GUI is only changed from the PyQt GUI thread, but I don't remember anymore how to do it. If user eyllanesc comes by this question, he can probably help you. :) – NoDataDumpNoContribution Feb 20 '20 at 12:17
  • Do you mean you want to incorporate the light barrier code into a single program with the Qt stuff so they always can only run as one? Or do you mean you want to leave them as separately runnable programs that can communicate with each other? – Mark Setchell Feb 20 '20 at 12:37
  • If the latter, you could use the filesystem to communicate in a very simple fashion. The light barrier code could create a file in `/tmp` if the barrier is unbroken and delete the file if broken. The Qt code could then set itself a timer for once every 500ms and check the existence of the file and change the GUI accordingly. This is a hack, but very simple to get started. – Mark Setchell Feb 20 '20 at 12:40
  • @Trilarion I think it would work well with signals / slots, I have read a lot about it on the Internet, but I can't get any further – Tristan K. Feb 20 '20 at 13:27

0 Answers0