Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from PyQt5.QtCore import QTime, QTimer, Qt
- from PyQt5.QtGui import QColor, QPalette, QFont
- from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QGridLayout
- class StopWatch(QWidget):
- def __init__(self, name, parent=None):
- super().__init__(parent)
- self.name = name
- self.time = QTime(0, 0, 0)
- self.timer = QTimer(self)
- self.timer.timeout.connect(self.update)
- self.is_running = False
- self.counter = 0
- self.label = QLabel(name)
- self.label.setFont(QFont("Arial", 16, QFont.Bold))
- self.time_label = QLabel(self.time.toString("hh:mm:ss"))
- self.time_label.setFont(QFont("Arial", 16))
- self.start_button = QPushButton("Start")
- self.stop_button = QPushButton("Stop")
- self.reset_button = QPushButton("Reset")
- self.start_button.clicked.connect(self.start)
- self.stop_button.clicked.connect(self.stop)
- self.reset_button.clicked.connect(self.reset)
- self.reset_button.setEnabled(False)
- hlayout = QHBoxLayout()
- hlayout.addWidget(self.start_button)
- hlayout.addWidget(self.stop_button)
- hlayout.addWidget(self.reset_button)
- vlayout = QVBoxLayout()
- vlayout.addWidget(self.label)
- # Adding a yellow square box to the left of the timer
- self.yellow_box = QWidget(self)
- self.yellow_box.setAutoFillBackground(True)
- self.set_box_color(QColor(Qt.yellow))
- self.yellow_box.setMinimumWidth(20)
- self.yellow_box.setMinimumHeight(20)
- self.yellow_box.setMaximumWidth(60)
- timer_hlayout = QHBoxLayout()
- timer_hlayout.addWidget(self.yellow_box)
- timer_hlayout.addWidget(self.time_label)
- vlayout.addLayout(timer_hlayout)
- vlayout.addLayout(hlayout)
- self.setLayout(vlayout)
- def set_box_color(self, color):
- palette = self.yellow_box.palette()
- palette.setColor(QPalette.Window, color)
- self.yellow_box.setPalette(palette)
- def start(self):
- if not self.is_running:
- self.timer.start(1000)
- self.is_running = True
- self.set_box_color(QColor(Qt.green))
- self.reset_button.setEnabled(True)
- def stop(self):
- if self.is_running:
- self.timer.stop()
- self.is_running = False
- if self.time.second() > 15:
- self.set_box_color(QColor(Qt.red))
- else:
- self.set_box_color(QColor(Qt.red))
- if self.time == QTime(0, 0, 0):
- self.set_box_color(QColor(Qt.red))
- else:
- self.set_box_color(QColor(Qt.red))
- def reset(self):
- if self.is_running:
- self.timer.stop()
- self.is_running = False
- self.time = QTime(0, 0, 0)
- self.time_label.setText(self.time.toString("hh:mm:ss"))
- self.set_box_color(QColor(Qt.yellow))
- self.counter += 1
- self.label.setText(f"{self.name} - Count: {self.counter}")
- self.reset_button.setEnabled(False)
- def update(self):
- self.time = self.time.addSecs(1)
- self.time_label.setText(self.time.toString("hh:mm:ss"))
- if self.time.second() > 15:
- self.set_box_color(QColor(255,165,0))
- class MainWindow(QMainWindow):
- def __init__(self):
- super().__init__()
- self.timers = []
- central_widget = QWidget()
- self.setCentralWidget(central_widget)
- grid_layout = QGridLayout(central_widget)
- grid_layout.setColumnStretch(0, 1)
- grid_layout.setColumnStretch(1, 1)
- # Create multiple instances of StopWatch and add them to the grid layout
- for i in range(6):
- timer = StopWatch(f"Station {len(self.timers) + 1}", self)
- self.timers.append(timer)
- grid_layout.addWidget(timer, i, 0)
- for i in range(5):
- timer = StopWatch(f"Station {len(self.timers) + 1}", self)
- self.timers.append(timer)
- grid_layout.addWidget(timer, i, 1)
- if __name__ == "__main__":
- app = QApplication([])
- window = MainWindow()
- window.show()
- app.exec_()
Add Comment
Please, Sign In to add comment