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