Skip to content

Commit 90ba386

Browse files
authored
Refactor MillisecondTimerWidget to use QWidget
1 parent 04d567f commit 90ba386

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed
Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,34 @@
1+
from PyQt6.QtCore import Qt, QTimer, pyqtSlot
12
from PyQt6.QtGui import QFont
2-
from PyQt6.QtWidgets import QLabel, QVBoxLayout
3+
from PyQt6.QtWidgets import QLabel, QVBoxLayout, QWidget
34

4-
5-
class MillisecondTimerWidget(QLabel):
6-
def __init__(self) -> None:
5+
class MillisecondTimerWidget(QWidget):
6+
def __init__(self):
77
super().__init__()
88

9-
timer_layout = QVBoxLayout()
10-
9+
layout = QVBoxLayout()
1110
font = QFont('Arial', 14)
1211

1312
self.timer_label = QLabel('00:00:000')
1413
self.timer_label.setFont(font)
15-
timer_layout.addWidget(self.timer_label)
14+
self.timer_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
15+
16+
layout.addWidget(self.timer_label)
17+
layout.setContentsMargins(0, 0, 0, 0)
18+
self.setLayout(layout)
19+
20+
self._elapsed_ms: int = 0
21+
self._timer = QTimer(self)
22+
self._timer.setInterval(10)
23+
self._timer.timeout.connect(self._on_timeout)
24+
self._timer.start()
1625

17-
self.setLayout(timer_layout)
26+
def _on_timeout(self) -> None:
27+
self._elapsed_ms += self._timer.interval()
28+
self.update_time(self._elapsed_ms)
1829

1930
def update_time(self, milliseconds: int) -> None:
2031
minutes = milliseconds // 60000
2132
seconds = (milliseconds % 60000) // 1000
2233
millis = milliseconds % 1000
23-
self.setText(f'{minutes:02}:{seconds:02}:{millis:03}')
34+
self.timer_label.setText(f'{minutes:02}:{seconds:02}:{millis:03}')

0 commit comments

Comments
 (0)