|
| 1 | +import sys |
| 2 | +import time |
| 3 | +from PyQt6.QtCore import QTimer, Qt |
| 4 | +from PyQt6.QtWidgets import QApplication, QWidget, QVBoxLayout, QPushButton, QLabel |
| 5 | +from PyQt6.QtGui import QFont |
| 6 | +from colorama import init, Fore, Style |
| 7 | + |
| 8 | +# Initialize colorama |
| 9 | +init(autoreset=True) |
| 10 | + |
| 11 | +class CountdownApp(QWidget): |
| 12 | + def __init__(self): |
| 13 | + super().__init__() |
| 14 | + |
| 15 | + self.initUI() |
| 16 | + |
| 17 | + def initUI(self): |
| 18 | + self.layout = QVBoxLayout() |
| 19 | + |
| 20 | + self.label = QLabel("00:00") |
| 21 | + self.label.setFont(QFont('Arial', 48)) |
| 22 | + self.label.setAlignment(Qt.AlignmentFlag.AlignCenter) |
| 23 | + self.layout.addWidget(self.label) |
| 24 | + |
| 25 | + self.work_button = QPushButton('Start Work Session') |
| 26 | + self.work_button.clicked.connect(lambda: self.start_countdown(25, "Work Session")) |
| 27 | + self.layout.addWidget(self.work_button) |
| 28 | + |
| 29 | + self.break_button = QPushButton('Start Break') |
| 30 | + self.break_button.clicked.connect(lambda: self.start_countdown(5, "Break")) |
| 31 | + self.layout.addWidget(self.break_button) |
| 32 | + |
| 33 | + self.setLayout(self.layout) |
| 34 | + |
| 35 | + self.setWindowTitle('Countdown Timer') |
| 36 | + self.setGeometry(300, 300, 300, 200) |
| 37 | + self.show() |
| 38 | + |
| 39 | + def start_countdown(self, minutes, session_name): |
| 40 | + self.work_button.setDisabled(True) |
| 41 | + self.break_button.setDisabled(True) |
| 42 | + self.seconds = minutes * 60 |
| 43 | + self.session_name = session_name |
| 44 | + self.update_timer() |
| 45 | + self.timer = QTimer(self) |
| 46 | + self.timer.timeout.connect(self.update_timer) |
| 47 | + self.timer.start(1000) |
| 48 | + |
| 49 | + def update_timer(self): |
| 50 | + mins, secs = divmod(self.seconds, 60) |
| 51 | + timer_text = f'{mins:02d}:{secs:02d}' |
| 52 | + self.label.setText(timer_text) |
| 53 | + |
| 54 | + if self.seconds == 0: |
| 55 | + self.timer.stop() |
| 56 | + self.label.setText(f'{self.session_name} is over!') |
| 57 | + |
| 58 | + self.work_button.setDisabled(False) |
| 59 | + self.break_button.setDisabled(False) |
| 60 | + else: |
| 61 | + self.seconds -= 1 |
| 62 | + |
| 63 | +if __name__ == '__main__': |
| 64 | + app = QApplication(sys.argv) |
| 65 | + ex = CountdownApp() |
| 66 | + sys.exit(app.exec()) |
0 commit comments