1. 程式人生 > >子執行緒不能訪問ui執行緒的解決辦法

子執行緒不能訪問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_()

希望有用