我有pyqt5 QTimer類和OSX的使用問題。PyQt5:最小化窗口(OSX)後QTimer不同步
如果通過單擊窗口左上角的黃色按鈕使以下簡單應用程序最小化,應用程序將按預期方式移動到停靠欄的右下角,並且定時器每秒增加一次。
奇怪的是,大約2:30分鐘後,定時器減速極端減少,讓說每10秒。我懷疑這與OSX下的「凍結」或「睡眠」模式有關?
我想要的是一個可靠的計時器,每秒計數一次。 pyqt下有一些我不知道的技巧嗎?
----編輯----
這也會發生,如果窗口失去焦點。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import time
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt, QTimer, pyqtSlot
class Main(QWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.seconds = 0
self.init_ui()
def init_ui(self):
self.timer = QTimer()
self.timer.setInterval(1000)
self.timer.setTimerType(Qt.PreciseTimer)
self.timer.timeout.connect(self.on_timer)
self.timer.start()
@pyqtSlot()
def on_timer(self):
self.seconds += 1
print(time.strftime("%H:%M:%S", time.gmtime(self.seconds)))
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
screen = Main()
screen.show()
sys.exit(app.exec_())
我的一些系統信息:
Python: 3.4.3
PyQt: 5.4.0
OSX: 10.9.5
uname -a: Darwin mac-pro 13.4.0 Darwin Kernel Version 13.4.0: Wed Mar 18 16:20:14 PDT 2015; root:xnu-2422.115.14~1/RELEASE_X86_64 x86_64
我迄今發現的是一個ThreadsafeTimer類形式https://github.com/robertsj/poropy/blob/master/pyqtgraph/ThreadsafeTimer.py。但是這仍然行不通。我認爲這可能與我作爲GUI程序員的技能有關。 – hetsch