/
qt-stop-watch.py
executable file
·62 lines (47 loc) · 1.39 KB
/
qt-stop-watch.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
#!/usr/bin/env python
import sys
from PyQt5 import Qt
from PyQt5.uic import loadUi
# [ms]
TICK_TIME = 2**6
class StopWatch(Qt.QMainWindow):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = loadUi("gui.ui", self)
self.reset.clicked.connect(self.do_reset)
self.start.clicked.connect(self.do_start)
self.timer = Qt.QTimer()
self.timer.setInterval(TICK_TIME)
self.timer.timeout.connect(self.tick)
self.do_reset()
def keyPressEvent(self, event):
if event.key() == Qt.Qt.Key_Escape:
self.close()
else:
super().keyPressEvent(event)
def display(self):
self.lcd.display("%d:%05.2f" % (self.time // 60, self.time % 60))
@Qt.pyqtSlot()
def tick(self):
self.time += TICK_TIME/1000
self.display()
@Qt.pyqtSlot()
def do_start(self):
self.timer.start()
self.start.setText("Pause")
self.start.clicked.disconnect()
self.start.clicked.connect(self.do_pause)
@Qt.pyqtSlot()
def do_pause(self):
self.timer.stop()
self.start.setText("Start")
self.start.clicked.disconnect()
self.start.clicked.connect(self.do_start)
@Qt.pyqtSlot()
def do_reset(self):
self.time = 0
self.display()
app = Qt.QApplication(sys.argv)
watch = StopWatch()
watch.show()
app.exec_()