在python pyqt中从线程内部调用方法到另一个类

在python pyqt中从线程内部调用方法到另一个类,python,pyqt,qthread,Python,Pyqt,Qthread,在pyqt窗口中,我调用一个线程来执行,该线程将数据发送回该窗口以更新QLCDNAMER,并且运行良好。但是当线程完成运行后,我希望它在接收窗口中调用一个方法。或者在接收到所有数据后,调用接收窗口自己的方法。我不知道如何在窗口内正确使用语句使其工作,也不知道如何从线程调用该方法。我已经尝试了很多方法,但是我已经在代码中写下了我想要实现的目标。。有什么想法吗 class MyThread(QtCore.QThread): countChange = QtCore.pyqtSignal(in

在pyqt窗口中,我调用一个线程来执行,该线程将数据发送回该窗口以更新QLCDNAMER,并且运行良好。但是当线程完成运行后,我希望它在接收窗口中调用一个方法。或者在接收到所有数据后,调用接收窗口自己的方法。我不知道如何在窗口内正确使用语句使其工作,也不知道如何从线程调用该方法。我已经尝试了很多方法,但是我已经在代码中写下了我想要实现的目标。。有什么想法吗

class MyThread(QtCore.QThread):
    countChange = QtCore.pyqtSignal(int)
    countReset = QtCore.pyqtSignal(int)

    def __init__(self, parent=None):
        super(MyThread, self).__init__(parent)
        self.stopped = QtCore.QEvent(QtCore.QEvent.User)

    def start(self):
        self.stopped.setAccepted(False)
        super(MyThread, self).start()

    def run(self):
        while not self.stopped.isAccepted():
            credit = 0
            limit = 13    
            while credit <= limit:
                press = int(input('add a number'))
                if press == 1:
                    if not credit >= limit:
                        credit=(credit + 1)
                        print (credit)
                        self.countChange.emit(credit)
                        if credit >= limit:
                            print ('Enough Credit')
                            self.stop()

    def stop(self):
        self.stopped.setAccepted(True)
##   What I want to have here is:
##      send a command to class winDow.move_on() to execute

class winDow(QtGui.QMainWindow,cashwin.Ui_MainWindow):
    def __init__(self, parent=None):
        super(winDow, self).__init__(parent)
        self.setupUi(self)
        self.thread = MyThread(self)
        self.thread.countChange.connect(self.lcdNumber.display)
        self.pull_credit()
## or if self.thread.countChange.connect == 13:
##        self.move_on()

    @QtCore.pyqtSlot()
    def pull_credit(self):
        self.thread.start()

    def move_on(self):
        self.window = NextWindow()
        self.window.show()
        self.close()
类MyThread(QtCore.QThread):
countChange=QtCore.pyqtSignal(int)
countReset=QtCore.pyqtSignal(int)
def uuu init uuu(self,parent=None):
超级(MyThread,self)。\uuuuu init\uuuuuuuu(父级)
self.stopped=QtCore.QEvent(QtCore.QEvent.User)
def启动(自):
self.stopped.setAccepted(False)
super(MyThread,self).start()
def运行(自):
而不是self.stopped.isAccepted():
信用=0
极限=13
信用=限额:
学分=(学分+1)
印刷(学分)
self.countChange.emit(信用)
如果信用>=限额:
打印(‘足够的信用’)
self.stop()
def停止(自):
self.stopped.setAccepted(True)
##我想要的是:
##向类窗口发送一个命令。move_on()执行
类窗口(QtGui.QMainWindow、cashwin.Ui\u MainWindow):
def uuu init uuu(self,parent=None):
超级(窗口,自我)。\uuuuu初始化\uuuuuuu(父级)
self.setupUi(self)
self.thread=MyThread(self)
self.thread.countChange.connect(self.lcdNumber.display)
self.pull_credit()
##或者如果self.thread.countChange.connect==13:
##self.move_on()
@QtCore.pyqtSlot()
def拉_积分(自):
self.thread.start()
def开启(自我):
self.window=NextWindow()
self.window.show()
self.close()

这正是信号和插槽的用途:在对象之间进行通信,尤其是在不同线程中的对象

幸运的是,
QThread
对象已经有了一个定义的信号,每当它们完成并即将退出时都会发出该信号。您可以简单地将其连接到主窗口的插槽,该插槽在线程完成其工作后要执行

将类的构造函数修改为如下所示:

class winDo(QtGui.QMainWindow, cashwin.Ui_MainWindow):
    def __init__(self, parent=None):
        # What you already have ...
        self.thread = MyThread(self)
        self.thread.countChange.connect(self.lcdNumber.display)
        self.thread.finished.connect(self.move_on)

成功了!我想我必须发送一个变量,然后测试这个变量。