Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/288.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 PyQt定时器/睡眠的正确使用_Python_Qt_Timer_Pyqt_Sleep - Fatal编程技术网

Python PyQt定时器/睡眠的正确使用

Python PyQt定时器/睡眠的正确使用,python,qt,timer,pyqt,sleep,Python,Qt,Timer,Pyqt,Sleep,如何正确使用QtTimer 我有一个按钮连接到一个方法,该方法基本上做了一些数学运算,但我想对它进行迭代,每次查看一个结果,它们之间的延迟为1秒 在正常的划线中,我会使用 for i in xrange(10): domath(i) sleep(1) 但在GUI中它不起作用,因为它冻结了GUI,我看不到结果。仅在10秒后,我看到了最后一次迭代 我试过QT定时器,但似乎不起作用 for i in xrange(10): QtCore.QTimer.singleShot(100

如何正确使用QtTimer

我有一个按钮连接到一个方法,该方法基本上做了一些数学运算,但我想对它进行迭代,每次查看一个结果,它们之间的延迟为1秒

在正常的划线中,我会使用

for i in xrange(10):
   domath(i)
   sleep(1)
但在GUI中它不起作用,因为它冻结了GUI,我看不到结果。仅在10秒后,我看到了最后一次迭代

我试过QT定时器,但似乎不起作用

for i in xrange(10):
    QtCore.QTimer.singleShot(1000,lambd: domath(i))

它直接进入最后一次迭代。我遗漏了什么吗?

我个人会这样写:

class A(QObject):
    def __init__(self):
        self.counter = 0
        Timer.singleShot(100, self.domath())

    Slot()
    def domath(self):
        # do the computation
        self.counter += 1
        if self.counter != 10:
            Timer.singleShot(100, self.domath())
        else:
            self.counter = 0;
免责声明:尽管看起来像PyQt代码,但这只是伪代码。我从未测试过等,但我认为这个概念是有效的。

self.domath()应该是self.domath。。。没有括号。我将在收到反馈后更新。计时器值也应该是100而不是1000。。。这也将得到更新。然而,这个概念仍然是一样的。