-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessClock.py
67 lines (47 loc) · 1.73 KB
/
ChessClock.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from PyQt5 import QtWidgets, QtCore
class ChessClock(QtWidgets.QLabel):
timeIsOver = QtCore.pyqtSignal()
timeFormat = "{:02d}:{:02d}"
def __init__(self, parent=None):
super().__init__(parent)
self.time = 0
self.clock = QtCore.QTimer(None)
self.clock.setTimerType(QtCore.Qt.PreciseTimer)
self.clock.setSingleShot(True)
self.clock.setInterval(self.time)
self.clock.timeout.connect(self._timeIsOver)
self.updateTimer = QtCore.QTimer(None)
self.updateTimer.setTimerType(QtCore.Qt.PreciseTimer)
self.updateTimer.setInterval(1000)
self.updateTimer.timeout.connect(self.updateLabel)
self.initUi()
def initUi(self):
self.setAlignment(QtCore.Qt.AlignCenter)
self.setText("00:00")
def setTime(self, timeInSeconds):
self.clock.stop()
self.updateTimer.stop()
self.time = timeInSeconds * 1000
self.clock.setInterval(self.time)
minutes = self.time // 1000 // 60
seconds = self.time // 1000 % 60
self.setText(self.timeFormat.format(minutes, seconds))
def updateLabel(self):
remainingTime = self.clock.remainingTime()
self.time = max(0, remainingTime)
minutes = self.time // 1000 // 60
seconds = self.time // 1000 % 60
self.setText(self.timeFormat.format(minutes, seconds))
self.updateTimer.start()
def start(self):
self.clock.start(self.time)
self.updateTimer.start()
def stop(self):
self.clock.stop()
self.updateTimer.stop()
def getTime(self):
return self.time
def _timeIsOver(self):
self.updateTimer.stop()
self.updateLabel()
self.timeIsOver.emit()