【问题标题】:Access a QLCDNumber object from a external function从外部函数访问 QLCDNumber 对象
【发布时间】:2020-05-01 08:09:23
【问题描述】:

每当线程t1每秒调用函数wait_thread_v1时,我的python脚本需要更改一个对象lcd_p1,但是如何做到这一点?我不知道如何在函数内部访问这个对象?有人可以帮忙吗?

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

【问题讨论】:

  • 在创建window 后创建Thread 并将窗口作为参数传递给线程。
  • 你能给我举个例子吗?
  • @TimKörner 如the docs所示,您不能也不应该从另一个线程修改GUI

标签: python multithreading pyqt pyqt5 qlcdnumber


【解决方案1】:

您不能也不应该像the docs 所指出的那样从辅助线程修改 GUI,因为 Qt 不保证它可以正常工作,而且不需要创建新线程。在这种情况下,最好创建一个在调用回调时发出信号的 QObject,因为它是线程安全的,然后将该信号连接到增加脉冲的槽,然后使用 QTimer 来实现线程的逻辑.

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-18
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多