简体   繁体   English

如何使用多核处理 setText 动态更改 pyqt QLabel 的内容

[英]How to dynamically change the content of a pyqt QLabel using multicoreprocessing setText

I'm trying to create a program to get the results from a measurement device and display them on the UI using multicoreprocessing.我正在尝试创建一个程序来从测量设备获取结果并使用多核处理将它们显示在 UI 上。 2 days of hard work, I'm trying to revise the code to see if I can display the results, but I can't figure it out, so I asked this question. 2天的努力,我正在尝试修改代码,看看是否可以显示结果,但我无法弄清楚,所以我问了这个问题。 My question is I want to change the content of PySide's QLabel dynamically, but because of multicoreprocessing, I can't change it dynamically.我的问题是我想动态更改 PySide 的 QLabel 的内容,但是由于多核处理,我无法动态更改它。 I searched the net, but there is no detailed information on how to do this, so I asked this question.我在网上搜索,但没有关于如何做到这一点的详细信息,所以我问了这个问题。

Error Description错误描述

Process Process-2:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 297, in _bootstrap
self.run()
File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\multiprocessing\process.py", line 99, in run
self._target(*self._args, **self._kwargs)
File "D:\Hiroshi\Program\Mitutoyo\test005.py", line 98, in lcd
self.ct_label.setText(q.get())
AttributeError: 'Main' object has no attribute 'ct_label
from multiprocessing import Queue, Process, freeze_support
import sys
from PySide6.QtWidgets import *
from PySide6 import QtWidgets
from PySide6.QtWidgets import QApplication, QLabel
from PySide2.QtCore import QTimer, QObject, Signal, Slot, QThread, QTimer
import time
import datetime
import csv
from playsound import playsound
from mitutoyo import mitutoyo
import signal

from multiprocessing import pool
import dill
import pickle

threshold = 0.030

sound01 = "D:\\DEFAULT.ACER\\Programing\\PycharmProjects\\Mitutoyo\\dial-guage\\sound\\0.010.wav"

class Main(QtWidgets.QLabel):

    def __setstate__(self, state):
        """非 Pickle 化されるとき呼ばれる"""
        # オブジェクトの持つ属性を復元する
        self.__dict__.update(state)

    def __getstate__(self):
        """Pickle 化されるとき呼ばれる"""
        # オブジェクトの持つ属性をコピーする
        self.__dict__.copy()

    def __init__(self, parent=None):
        super().__init__(parent)
        self.initUI()
        #self.setText("sdsdsdsdsdsdsds")

    def initUI(self):
        # プログラムスタートボタン
        self.btn01 = self.auto_button = QPushButton("開始", self)
        self.btn01.clicked.connect(self.start)
        self.btn01.move(0, 0)
        self.btn01.resize(200, 50)

        # プログラムを閉じるボタン
        self.btn01 = self.auto_button =  QPushButton("停止", self)
        self.btn01.clicked.connect(self.exit_main)
        self.btn01.move(0, 50)
        self.btn01.resize(200, 50)

        # CTの表示
        self.ct_label = QLabel("")
        #self.ct_label.move(300, 0)
        #self.ct_label.resize(400, 100)
        #self.style = ("font-size: 128px;")
        #self.ct_label.setStyleSheet(self.style)

        # Status Barの表示
        self.status_bar01 = QLabel("", self)
        self.status_bar01.move(0, 110)
        self.status_bar01.resize(2000,2000)


    def start(self):
        main = Main()
        q = Queue()
        p0 = Process(target = main.loop, args=(q,))
        p1 = Process(target = main.lcd, args=(q,))
        p2 = Process(target = main.alarm, args=(q,))

        p0.start()
        p1.start()
        p2.start()

    # whileループでミツトヨからのデータを取得し、LCDの表示と色の変更
    def loop(self, q):
        while True:
            self.value = mitutoyo()
            q.put(self.value)


    # exitが押された場合プログラム強制終了
    def exit_main(self, q):
        sys.exit()


    # LCD表示
    def lcd(self, q,):
        while True:
            self.ct_label.setText(q.get())

            #print(self.value)

            #self.styleA = "QWidget{background-color:%s}" % ("green" if float(self.value) < threshold else "brown")
            #self.status_bar01.setStyleSheet(self.styleA)

    # 警告音 ok
    def alarm(self, q):
        while True:
            if float(q.get()) > 0.010:
                print("sdsdsdsdwsdwdw")
                #playsound(sound01)

if __name__ == '__main__':

    freeze_support()

    app = QApplication(sys.argv) #不明。実行には必ず必要
    main_window = Main() #class Ppf()を表示
    main_window.setWindowTitle("歪 アラーム Ver.0.0.1") #windowのtitle
    main_window.setFixedSize(600, 600) #ソフトのサイズとサイズの固定
    #main_window.showMaximized() #ソフトのサイズとサイズの固定
    main_window.show()

    app.exec_() #不明。実行には必ず必要

What about using the event-mechanism of (Py)Qt.使用 (Py)Qt 的事件机制怎么样?

As any Widget subclass in Qt, the QLabel has event-handlers that can be implemented .作为 Qt 中的任何Widget子类, QLabel具有可以实现的事件处理程序

When one or both of your concurrent processes is emitting a QEvent , they can be interpreted on the QLabel inside one of these, like event(QEvent e) .当您的一个或两个并发进程发出QEvent时,它们可以在其中之一内的QLabel上进行解释,例如event(QEvent e)

See also:也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM