1

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

我使用 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'
4

1 回答 1

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+']')
于 2013-12-18T18:44:46.943 回答