|
| 1 | +from PyQt6.QtCore import Qt, QTimer, pyqtSlot |
1 | 2 | from PyQt6.QtGui import QFont |
2 | | -from PyQt6.QtWidgets import QLabel, QVBoxLayout |
| 3 | +from PyQt6.QtWidgets import QLabel, QVBoxLayout, QWidget |
3 | 4 |
|
4 | | - |
5 | | -class MillisecondTimerWidget(QLabel): |
6 | | - def __init__(self) -> None: |
| 5 | +class MillisecondTimerWidget(QWidget): |
| 6 | + def __init__(self): |
7 | 7 | super().__init__() |
8 | 8 |
|
9 | | - timer_layout = QVBoxLayout() |
10 | | - |
| 9 | + layout = QVBoxLayout() |
11 | 10 | font = QFont('Arial', 14) |
12 | 11 |
|
13 | 12 | self.timer_label = QLabel('00:00:000') |
14 | 13 | 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() |
16 | 25 |
|
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) |
18 | 29 |
|
19 | 30 | def update_time(self, milliseconds: int) -> None: |
20 | 31 | minutes = milliseconds // 60000 |
21 | 32 | seconds = (milliseconds % 60000) // 1000 |
22 | 33 | 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