薛定谔的错误:PyQt QTimer 事件仅在 startTimer() 上有断点时触发


这是一个

神奇的错误:此代码的行为、打印输出和分支根据放置的断点而不是基于实际代码而变化。

timerEvent() 中的一个断点开始。

不会触发 timerEvent() 中的此断点。

但是,当我在startTimer()上设置第二个断点时 - 但不更改任何其他内容,那么timerEvent()中的断点现在跟踪。

#!/bin/python2
import sys
from PyQt4.QtGui import QMainWindow, QApplication
class SchrodingersWindow(QMainWindow):
    def __init__(self, parent=None, flags=0):
        super(QMainWindow, self).__init__(parent)
        self.startTimer(0) # Try setting a breakpoint here
    def timerEvent(self, event):
        print "Something Timer Happened" # This never gets called unless the breakpoint is set on startTimer()
if __name__ == "__main__":
    app = QApplication(sys.argv)
    SchrodingersWindow()
    app.exec_()

薛定谔的错误。 在带机翼调试器的 pyqt 中进行测试

在多个版本的Wing调试器,两台计算机,Windows和Ubuntu上进行了测试。

要测试这一点,你需要一个python调试器(我推荐Wing)python 2.7和PyQt4

发生这种情况是因为一旦SchrodingersWindow()完成,保存窗口的对象就会被删除,因为您从不存储引用。如果在 __init__ 中调试,则对象正在创建过程中,SchrodingersWindow()尚未返回 - 删除尚未发生。儗

win = SchrodingersWindow()

每次都执行timerEvent

最新更新