0

I have the following two files:

import sys
import time
from PyQt4 import QtGui, QtCore

import btnModule

class WindowClass(QtGui.QWidget):

    def __init__(self):
        super(WindowClass, self).__init__()
        self.dataLoaded = None

#       Widgets

#       Buttons
        thread = WorkerForLoop(self.runLoop)
#        thread.start()
        self.playBtn    = btnModule.playpauselBtnClass \
                            ('Play', thread.start)            
#       Layout
        layout = QtGui.QHBoxLayout()
        layout.addWidget(self.playBtn)
        self.setLayout(layout)

#       Window Geometry
        self.setGeometry(100, 100, 100, 100)

    def waitToContinue(self):
        print self.playBtn.text()
        while (self.playBtn.text() != 'Pause'):
            pass

    def runLoop(self):
        for ii in range(100):
            self.waitToContinue()
            print 'num so far: ', ii
            time.sleep(0.5)

class WorkerForLoop(QtCore.QThread):
    def __init__(self, function, *args, **kwargs):
        super(WorkerForLoop, self).__init__()
        self.function = function
        self.args = args
        self.kwargs = kwargs

    def __del__(self):
        self.wait()

    def run(self):
        print 'let"s run it'
        self.function(*self.args, **self.kwargs)
        return



if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)

    wmain = WindowClass()
    wmain.show()

    sys.exit(app.exec_())

and the second file btnModule.py:

from PyQt4 import QtGui, QtCore

class playpauselBtnClass(QtGui.QPushButton):

    btnSgn = QtCore.pyqtSignal()

    def __init__(self, btnName, onClickFunction):
        super(playpauselBtnClass, self).__init__(btnName)

        self.clicked.connect(self.btnPressed)
        self.btnSgn.connect(onClickFunction)

    def btnPressed(self):
        if self.text() == 'Play':
            self.setText('Pause')
            self.btnSgn.emit()
            print 'Changed to pause and emited signal'
        elif self.text() == 'Pause':
            self.setText('Continue')
            print 'Changed to Continue'
        elif self.text() == 'Continue':
            self.setText('Pause')
            print 'Changed to Pause'

If in the first file I remove the comment at thread.start() it works as expected, it starts the thread and then hangs until I click Play on the UI. However, I thought it should work even if I didn't start it there as the signal is btnSgn is connected to onClickFunction which in this case takes the value thread.start.

Used this as a reference http://joplaete.wordpress.com/2010/07/21/threading-with-pyqt4/

4

1 に答える 1

2

2番目のファイルで(self.btnSgn.emit()を介して)thread.start()を呼び出そうとしても機能しない理由は、作成したinit関数の外側でスレッドオブジェクトがスコープ外になるためだと思いますそれ。したがって、すでに削除されたスレッドで start() を呼び出しています。

thread -> self.thread を変更する (つまり、thread オブジェクトを WindowClass オブジェクトのメンバーにする) だけで、試してみると問題なく動作します。これは、thread がプログラムの最後まで存続するためです。

于 2013-04-08T04:36:54.593 に答える