子线程不能访问ui线程的解决办法

#coding:utf-8
from PyQt5 import QtWidgets

from PyQt5.QtCore import *
import sys
import threading

class Main(object):   #接收方可以不是Qt的
    def __init__(self):
        super(Main, self).__init__()

        self.thread = MyThread()
        self.thread.sinOut.connect(self.outText)
        self.thread.start()

    def outText(self, text):
        print('threadxxx %s is running...' % threading.current_thread().name)
        print(text)

class MyThread(QThread): #发信号方必须为Qt的,因为用到了pyqtSignal
    sinOut = pyqtSignal(str)    # l = [] 必须在init之前
    def __init__(self):
        super(MyThread, self).__init__()
        self.identity = None

    def run(self):
        print('thread %s is running...' % threading.current_thread().name)
        self.sinOut.emit("90")    # l.append()

app = QtWidgets.QApplication(sys.argv)
main = Main()
app.exec_()

希望有用

猜你喜欢

转载自blog.csdn.net/ewwerpm/article/details/85935574