Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
394 views
in Technique[技术] by (71.8m points)

python - Access a QLCDNumber object from a external function

My python script need to change one object lcd_p1 every time the function wait_thread_v1 is call every second by a thread t1, but how do this? I don't know how to access this object inside the function? Anyone can help?

vazao1 = 12
global pulses_v1
pulses_v1 = 0

GPIO.setmode(GPIO.BCM)
GPIO.setup(vazao1, GPIO.IN)

class Window(QWidget):

    def __init__(self):
        super().__init__()
        self.setGeometry(50, 50, 640, 480)
        self.setWindowTitle("Programa")
        self.initUI()

    def initUI(self):
        self.lcd_v1 = QLCDNumber(self)
        self.lcd_v1.display(0)
        self.lcd_v1.setDigitCount(4)
        self.lcd_v1.setFixedWidth(180)
        self.lcd_v1.setFixedHeight(80)
        self.lcd_v1.move(60,320)

def count_pulses_v1(channel):
    global pulses_v1
    pulses_v1 += 1

def wait_thread_v1():
    global pulses_v1
    while True:
        time.sleep(1)
        print("Pulsos total de vaz?o 1: "+str(pulses_v1))
        #Window.initUI.self.lcd_p1.display(100)
        pulses_v1 = 0

GPIO.add_event_detect(vazao1, GPIO.FALLING, callback=count_pulses_v1)
t1 = Thread(target=wait_thread_v1, name='thread_01', args=())
t1.start()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    sys.exit(app.exec())
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You cannot and should not modify the GUI from a secondary thread as indicated by the docs since Qt does not guarantee that it works besides that it is unnecessary to create a new thread. In this case it is better to create a QObject that emits the signal when the callback is invoked since it is thread-safe, and then connect that signal to a slot that increases the pulses, then use a QTimer to implement the logic of the thread.

import sys

from PyQt5.QtCore import pyqtSignal, pyqtSlot, QObject, QTimer
from PyQt5.QtWidgets import QApplication, QLCDNumber, QWidget

import RPi.GPIO as GPIO


class Window(QWidget):
    def __init__(self):
        super().__init__()

        self.pulses = 0

        self.setGeometry(50, 50, 640, 480)
        self.setWindowTitle("Programa")
        self.initUI()

        timer = QTimer(self, timeout=self.on_timeout, interval=1000)
        timer.start()

    def initUI(self):
        self.lcd_v1 = QLCDNumber(self)
        self.lcd_v1.display(0)
        self.lcd_v1.setDigitCount(4)
        self.lcd_v1.setFixedWidth(180)
        self.lcd_v1.setFixedHeight(80)
        self.lcd_v1.move(60, 320)

    @pyqtSlot()
    def falling_slot(self):
        self.pulses += 1

    @pyqtSlot()
    def on_timeout(self):
        self.lcd_v1.display(self.pulses)
        self.pulses = 0


class GPIOManager(QObject):
    fallingSignal = pyqtSignal()

    def __init__(self, parent=None):
        super().__init__(parent)

        pin = 12
        GPIO.setmode(GPIO.BCM)
        GPIO.setup(pin, GPIO.IN)
        GPIO.add_event_detect(pin, GPIO.FALLING, callback=self.falling_callback)

    def falling_callback(self, channel):
        # This method is called in the thread where GPIOs are monitored.
        self.fallingSignal.emit()


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

    window = Window()
    window.show()
    manager = GPIOManager()
    manager.fallingSignal.connect(window.falling_slot)

    sys.exit(app.exec())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...