PyQt5:使用 QObject 和 QThread 时出现 AttributeError
Posted
技术标签:
【中文标题】PyQt5:使用 QObject 和 QThread 时出现 AttributeError【英文标题】:PyQt5: Got AttributeError while using QObject and QThread 【发布时间】:2013-12-18 13:23:33 【问题描述】:我在开发一个可以同时从不同站点检索包含内容的程序时遇到了一个问题(现在基本上是从两个或多个在线词典中检索一个单词的定义)。
我使用 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】:问题是因为每次搜索都删除搜索器对象列表,这是不必要的。
您只需要创建一次搜索器对象,然后通过发出带有所需单词的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+']')
【讨论】:
以上是关于PyQt5:使用 QObject 和 QThread 时出现 AttributeError的主要内容,如果未能解决你的问题,请参考以下文章