Guest User

Untitled

a guest
Apr 26th, 2023
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. from PyQt5.QtCore import QTime, QTimer, Qt
  2. from PyQt5.QtGui import QColor, QPalette, QFont
  3. from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton, QGridLayout
  4.  
  5.  
  6. class StopWatch(QWidget):
  7. def __init__(self, name, parent=None):
  8. super().__init__(parent)
  9. self.name = name
  10. self.time = QTime(0, 0, 0)
  11. self.timer = QTimer(self)
  12. self.timer.timeout.connect(self.update)
  13. self.is_running = False
  14. self.counter = 0
  15.  
  16. self.label = QLabel(name)
  17. self.label.setFont(QFont("Arial", 16, QFont.Bold))
  18. self.time_label = QLabel(self.time.toString("hh:mm:ss"))
  19. self.time_label.setFont(QFont("Arial", 16))
  20. self.start_button = QPushButton("Start")
  21. self.stop_button = QPushButton("Stop")
  22. self.reset_button = QPushButton("Reset")
  23.  
  24. self.start_button.clicked.connect(self.start)
  25. self.stop_button.clicked.connect(self.stop)
  26. self.reset_button.clicked.connect(self.reset)
  27. self.reset_button.setEnabled(False)
  28.  
  29. hlayout = QHBoxLayout()
  30. hlayout.addWidget(self.start_button)
  31. hlayout.addWidget(self.stop_button)
  32. hlayout.addWidget(self.reset_button)
  33.  
  34. vlayout = QVBoxLayout()
  35. vlayout.addWidget(self.label)
  36.  
  37. # Adding a yellow square box to the left of the timer
  38. self.yellow_box = QWidget(self)
  39. self.yellow_box.setAutoFillBackground(True)
  40. self.set_box_color(QColor(Qt.yellow))
  41. self.yellow_box.setMinimumWidth(20)
  42. self.yellow_box.setMinimumHeight(20)
  43. self.yellow_box.setMaximumWidth(60)
  44.  
  45. timer_hlayout = QHBoxLayout()
  46. timer_hlayout.addWidget(self.yellow_box)
  47. timer_hlayout.addWidget(self.time_label)
  48.  
  49. vlayout.addLayout(timer_hlayout)
  50. vlayout.addLayout(hlayout)
  51.  
  52. self.setLayout(vlayout)
  53.  
  54. def set_box_color(self, color):
  55. palette = self.yellow_box.palette()
  56. palette.setColor(QPalette.Window, color)
  57. self.yellow_box.setPalette(palette)
  58.  
  59. def start(self):
  60. if not self.is_running:
  61. self.timer.start(1000)
  62. self.is_running = True
  63. self.set_box_color(QColor(Qt.green))
  64. self.reset_button.setEnabled(True)
  65.  
  66. def stop(self):
  67. if self.is_running:
  68. self.timer.stop()
  69. self.is_running = False
  70. if self.time.second() > 15:
  71. self.set_box_color(QColor(Qt.red))
  72. else:
  73. self.set_box_color(QColor(Qt.red))
  74. if self.time == QTime(0, 0, 0):
  75. self.set_box_color(QColor(Qt.red))
  76. else:
  77. self.set_box_color(QColor(Qt.red))
  78.  
  79. def reset(self):
  80. if self.is_running:
  81. self.timer.stop()
  82. self.is_running = False
  83. self.time = QTime(0, 0, 0)
  84. self.time_label.setText(self.time.toString("hh:mm:ss"))
  85. self.set_box_color(QColor(Qt.yellow))
  86. self.counter += 1
  87. self.label.setText(f"{self.name} - Count: {self.counter}")
  88. self.reset_button.setEnabled(False)
  89.  
  90. def update(self):
  91. self.time = self.time.addSecs(1)
  92. self.time_label.setText(self.time.toString("hh:mm:ss"))
  93. if self.time.second() > 15:
  94. self.set_box_color(QColor(255,165,0))
  95.  
  96.  
  97. class MainWindow(QMainWindow):
  98. def __init__(self):
  99. super().__init__()
  100.  
  101. self.timers = []
  102.  
  103. central_widget = QWidget()
  104. self.setCentralWidget(central_widget)
  105.  
  106. grid_layout = QGridLayout(central_widget)
  107. grid_layout.setColumnStretch(0, 1)
  108. grid_layout.setColumnStretch(1, 1)
  109.  
  110. # Create multiple instances of StopWatch and add them to the grid layout
  111. for i in range(6):
  112. timer = StopWatch(f"Station {len(self.timers) + 1}", self)
  113. self.timers.append(timer)
  114. grid_layout.addWidget(timer, i, 0)
  115.  
  116. for i in range(5):
  117. timer = StopWatch(f"Station {len(self.timers) + 1}", self)
  118. self.timers.append(timer)
  119. grid_layout.addWidget(timer, i, 1)
  120.  
  121.  
  122.  
  123. if __name__ == "__main__":
  124. app = QApplication([])
  125. window = MainWindow()
  126. window.show()
  127. app.exec_()
  128.  
Add Comment
Please, Sign In to add comment