Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 单次放炮时间在pyqt4 QThread内不工作_Python_Python 2.7_Pyqt_Pyqt4_Qthread - Fatal编程技术网

Python 单次放炮时间在pyqt4 QThread内不工作

Python 单次放炮时间在pyqt4 QThread内不工作,python,python-2.7,pyqt,pyqt4,qthread,Python,Python 2.7,Pyqt,Pyqt4,Qthread,我试图在QThread中使用单次触发计时器,但它不起作用。以下是我正在使用的代码: class thread1((QtCore.QThread): def __init__(self,parent): QtCore.QThread.__init__(self, parent) self.Timer1 = None def __del__(self): self.wait() def timerPINNo(self):

我试图在QThread中使用单次触发计时器,但它不起作用。以下是我正在使用的代码:

class thread1((QtCore.QThread):
    def __init__(self,parent):
        QtCore.QThread.__init__(self, parent)
        self.Timer1 = None

    def __del__(self):
        self.wait()

    def timerPINNo(self):
        print "Timer completed"

    def run(self):
        tempVal0 = getData()
        if tempVal0 == 0:
            self.Timer1 = QtCore.QTimer()
            self.Timer1.timeout.connect(self.timerPINNo)
            self.Timer1.setSingleShot(True)
            self.Timer1.start(5000)
        else: pass

我面临的问题是,超时后,timerPINNo函数永远不会被调用。单次快照在正常使用时有效,但在从QThread调用它时无效。我在哪里出错?

问题的原因是,如果run方法完成执行,线程将完成其执行,因此它将被消除,因此计时器也将被消除。解决方案是保持run方法为其运行,必须使用QEventLoop

import sys
from PyQt4 import QtCore

class thread1(QtCore.QThread):
    def __init__(self,*args, **kwargs):
        QtCore.QThread.__init__(self, *args, **kwargs)
        self.Timer1 = None

    def __del__(self):
        self.wait()

    def timerPINNo(self):
        print("Timer completed")


    def run(self):
        tempVal0 = getData()
        if tempVal0 == 0:
            self.Timer1 = QtCore.QTimer()
            self.Timer1.timeout.connect(self.timerPINNo)
            self.Timer1.setSingleShot(True)
            self.Timer1.start(5000)
            loop = QtCore.QEventLoop()
            loop.exec_()

if __name__ == "__main__":
   app = QtCore.QCoreApplication(sys.argv)
   th = thread1()
   th.start()
   sys.exit(app.exec_())

更好的解决方案是根本不重写run方法,这样就不会阻止QThread的内置事件循环运行。您能更好地解释一下吗