PyQt5:使用QObject和QThread时得到了AttributeError

时间:2013-12-18 13:23:33

标签: python multithreading qthread qobject pyqt5

我在开发一个可以同时从不同网站检索包含的程序时遇到了问题(现在基本上是从两个或多个在线词典中检索单词的定义)。

我使用QObject和QThread来开发我的程序。一开始一切都很顺利,但当我试图再次搜索和检索包含另一个单词的时候,我得到了2个AttributeError。

import sys
from PyQt5.QtWidgets import QApplication, QMainWindow
from gui import Ui_MainWindow
from PyQt5.QtCore import QObject, QThread, pyqtSignal

class Searcher(QObject):
    signalUpdate=pyqtSignal(str, str)

    def __init__(self, dictionary, word):
        super(Searcher, self).__init__()
        self.dictionary=dictionary
        self.word=word
    def run(self):
        self.signalUpdate.emit(self.dictionary, self.word)      

class MyMainWindow(QMainWindow, Ui_MainWindow, QObject):
    signalSearch=pyqtSignal()

    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__()
        self.setupUi(self)
        self.dictList=['Yahoo dictionary', 'Webster dictionary']
        self.searchThreads=[QThread(), QThread()]


    def click(self):
        word=self.lineEdit.text()
        self.searchers=[]

        for dictionary in self.dictList:
            self.searchers.append(Searcher(dictionary, word))

        for index , searcher in enumerate(self.searchers):
            searcher.signalUpdate.connect(self.updateGUI)
            searcher.moveToThread(self.searchThreads[index])
            self.searchThreads[index].start()
            self.signalSearch.connect(searcher.run)

        self.signalSearch.emit()

    def updateGUI(self, dictionary, word):
        print(dictionary+'['+word+']')

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

    sys.exit(app.exec_())

我不知道如何处理这个问题。有谁知道怎么摆脱它?

Traceback (most recent call last):
Traceback (most recent call last):
File "C:\workspace\online-dictionary\main.py", line 22, in run
File "C:\workspace\online-dictionary\main.py", line 22, in run
self.signalUpdate.emit(self.dictionary, self.word)      
self.signalUpdate.emit(self.dictionary, self.word)      
AttributeError: 'NoneType' object has no attribute 'signalUpdate'
AttributeError: 'NoneType' object has no attribute 'signalUpdate'

1 个答案:

答案 0 :(得分:0)

问题是每次搜索时删除搜索器对象列表引起的,这是不必要的。

您只需要创建一次搜索器对象,然后通过发送带有所需单词的signalSearch来向他们发出请求。

所以搜索者类看起来像这样:

class Searcher(QObject):
    signalUpdate = pyqtSignal(str, str)

    def __init__(self, dictionary):
        super(Searcher, self).__init__()
        self.dictionary = dictionary

    # word is sent by signalSearch
    def run(self, word):
        self.signalUpdate.emit(self.dictionary, word)

并且主窗口类看起来像这样:

class MyMainWindow(QMainWindow, Ui_MainWindow):
    signalSearch = pyqtSignal(str)

    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__()
        self.setupUi(self)
        self.dictList = ['Yahoo dictionary', 'Webster dictionary']
        self.searchThreads = []
        self.searchers = []
        # create all the threads and searchers once
        for dictionary in self.dictList:
            thread = QThread(self)
            self.searchThreads.append(thread)
            searcher = Searcher(dictionary)
            self.searchers.append(searcher)
            searcher.signalUpdate.connect(self.updateGUI)
            self.signalSearch.connect(searcher.run)
            searcher.moveToThread(thread)
            thread.start()

    def click(self):
        # send a new word to the searchers
        self.signalSearch.emit(self.lineEdit.text())

    def updateGUI(self, dictionary, word):
        print(dictionary+'['+word+']')