我开发了一个多线程gui,它在一个单独的线程中读取串行数据。在python中,我对python线程非常陌生。我用这个网站作为参考,以达到这一点,并使其正常工作,但研究如何添加第二个线程,我发现了许多文章和帖子,你不应该子类线程。我该如何将其转换为“正确”的方法?在
class AThread(QtCore.QThread):
updated = QtCore.pyqtSignal(str)
query = QtCore.pyqtSignal(str)
def __init__(self):
QtCore.QThread.__init__(self)
def run(self):
try:
while True:
if ser.in_waiting:
line=ser.readline()[:-2]#remove end of line \r\n
self.updated.emit(line.decode('utf-8'))
if main_window.keywordCheckBox.isChecked():
if main_window.keywordInput.text() in line.decode('utf-8'):
self.query.emit("Match!")
self.query.emit(line.decode('utf-8'))
except serial.serialutil.SerialException:
pass
class MyMainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
self.thread= AThread()
self.thread.updated.connect(self.updateText)
self.thread.query.connect(self.matchFound)
下面是Qt文档中的一篇文章,您可能会发现这篇文章很有帮助 http://doc.qt.io/qt-4.8/thread-basics.html
有一个段落叫做“应该使用哪种Qt线程技术?”创建一个表格,根据您想要实现的目标,建议使用哪种方法
在您的情况下,您可能需要遵循表的最后两行中描述的方法之一。在
如果是这样,那么您的代码应该如下所示
相关问题 更多 >
编程相关推荐