AttributeError:'class'对象没有属性'signal'

时间:2018-02-16 15:51:05

标签: python pyqt pyqt5

这可能是一个愚蠢的问题,但为什么我不能在self.printSecondSignal.emit('TEST2')类中发出信号Worker。我想要做的是:从do_some_calc()类和QPushButton clicked() signal类调用函数TestMain(连接Worker),想要发出一个信号来更新Gui in TestMain。但我得到AttributeError: TestMain object has no attribute printSecondSignal。发出信号self.printFirstSignal.emit('TEST1')可以毫无问题地工作

TestMain.py

from source.Manager.TestManager import *

class TestMain(QMainWindow):
    def __init__(self, parent= None):
        super(TestMain, self).__init__(parent)

        # import ui
        loadUi('../gui/testGui.ui', self)

        self.manager= TestManager()
        self.thread= QThread()
        self.manager.moveToThread(self.thread)

        self.manager.finished.connect(self.thread.quit)
        self.thread.started.connect(self.manager.first_slot)
        self.thread.start()

        self.manager.printFirstSignal.connect(self.print_onTextEdit)
        self.offlinePushButton.clicked.connect(self.offline_slot)
        self.manager.printSecondSignal.connect(self.print_onTextEdit)

    def offline_slot(self):
        manager.do_some_calc(self)

    def print_onTextEdit(self, str):
        self.outputTextEdit.append(str)

Manager.py

class TestManager(QObject): #QtCore.QThread
    finished= pyqtSignal()
    printFirstSignal= pyqtSignal(str)
    printSecondSignal= pyqtSignal(str)

    def __init__(self, parent= None):
        super(TestManager, self).__init__(parent)

    def first_slot(self):
        self.printFirstSignal.emit('TEST1')
        self.finished.emit()

    def do_some_calc(self):
        do_sometingelse()
        try:
            self.printSecondSignal.emit('TEST2')
        except :
            traceback.print_exc()

1 个答案:

答案 0 :(得分:0)

正如你在另一个问题中被告知你所说的相同代码时,线程之间的所有通信都必须通过使用信号来完成。在offline_slot方法中,当管理器实例在另一个线程中运行时,您尝试直接调用manager.do_some_calc()。这种通信必须通过信号完成。它看起来像这样:

class TestMain(QMainWindow):

    do_some_calc_signal = pyqtSignal()

    def __init__(self, parent=None):
        super(TestMain, self).__init__(parent)

        # import ui
        loadUi('../gui/testGui.ui', self)

        self.manager= TestManager()
        self.thread= QThread()
        self.manager.moveToThread(self.thread)

        self.do_some_calc_signal.connect(self.manager.do_some_calc)

        self.manager.finished.connect(self.thread.quit)
        self.thread.started.connect(self.manager.first_slot)
        self.thread.start()

        self.manager.printFirstSignal.connect(self.print_onTextEdit)
        self.offlinePushButton.clicked.connect(self.offline_slot)
        self.manager.printSecondSignal.connect(self.print_onTextEdit)

    def offline_slot(self):
        self.do_some_calc_signal.emit()