简体   繁体   中英

Calling a method from inside a thread to another class in python pyqt

From a pyqt window, I call a thread to execute which emits data back to the window to update a QLCDNumber and it works fine. But when the thread has finished running, I would like it to call a method in the receiving window. OR the receiving window, after receiving all of the data, to call it's own method. I'm not sure how to use statements properly inside the window to make it work nor call the method from the thread. I've tried SO many ways but I have written in the code what I am trying to accomplish.. Any ideas?

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()

This is exactly what signals and slots are for: communicating between objects, especially those in different threads.

Luckily, QThread objects already have a defined signal which is emitted whenever they are done, and about to exit. You can simply connect this to the main window's slot that you want to be executed after the thread has completed its work.

Amend the class's constructor to look like this:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM