首页 > 解决方案 > 使用 PyQt 显示 Raspberry Pi GPIO 读数

问题描述

我正在做一个树莓派项目。Raspi 检测到激光光栅何时被中断。

发生这种情况时,我想更改 PyQt 标签中的文本。

我现在有两个 Python 文件,我不知道如何将它们连接在一起。

GPIO检测:

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)

和 PyQt 窗口:

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_())

我怎样才能将这些连接在一起?

标签: python-3.xpyqtraspberry-piraspberry-pi3gpio

解决方案


推荐阅读