import time import threading import subprocess from PyQt6.QtWidgets import QWidget, QVBoxLayout, QPushButton, QLabel, QHBoxLayout, QLineEdit from PyQt6.QtCore import QTimer, QUrl from PyQt6.QtMultimedia import QSoundEffect class Timer: def __init__(self): self.remaining = 0 self.running = False self.timer_thread = None def set(self, hours, minutes, seconds): self.remaining = hours * 3600 + minutes * 60 + seconds def start(self): if self.remaining > 0: self.running = True self.timer_thread = threading.Thread(target=self._run_timer) self.timer_thread.start() def _run_timer(self): while self.running and self.remaining > 0: time.sleep(1) self.remaining -= 1 if self.running: self._alarm() def stop(self): self.running = False if self.timer_thread: self.timer_thread.join() def get_remaining_time(self): return self.remaining def _alarm(self): print("Time's up!") try: for _ in range(3): subprocess.run(["aplay", "/usr/share/sounds/sound-icons/glass.wav"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, check=True) except subprocess.CalledProcessError: print("Error playing alarm sound") self._fallback_alarm() def _fallback_alarm(self): sound = QSoundEffect() sound.setSource(QUrl.fromLocalFile("/usr/share/sounds/sound-icons/glass.wav")) sound.play() class Timer_Ui(QWidget): def __init__(self): super().__init__() self.setWindowTitle("Timer") self.width = 400 self.height = 200 self.timer = Timer() self.init_ui() def init_ui(self): layout = QVBoxLayout() self.setLayout(layout) self.timer_label = QLabel('Timer: Not Set') layout.addWidget(self.timer_label) timer_input_layout = QHBoxLayout() self.hours_input = QLineEdit() self.minutes_input = QLineEdit() self.seconds_input = QLineEdit() timer_input_layout.addWidget(QLabel('Hours:')) timer_input_layout.addWidget(self.hours_input) timer_input_layout.addWidget(QLabel('Minutes:')) timer_input_layout.addWidget(self.minutes_input) timer_input_layout.addWidget(QLabel('Seconds:')) timer_input_layout.addWidget(self.seconds_input) layout.addLayout(timer_input_layout) self.set_timer_button = QPushButton('Set Timer') self.set_timer_button.clicked.connect(self.set_timer) layout.addWidget(self.set_timer_button) self.start_timer_button = QPushButton('Start Timer') self.start_timer_button.clicked.connect(self.start_timer) layout.addWidget(self.start_timer_button) self.stop_timer_button = QPushButton('Stop Timer') self.stop_timer_button.clicked.connect(self.stop_timer) layout.addWidget(self.stop_timer_button) self.update_timer = QTimer() self.update_timer.timeout.connect(self.update_timer_display) self.update_timer.start(1000) def set_timer(self): try: hours = int(self.hours_input.text() or 0) minutes = int(self.minutes_input.text() or 0) seconds = int(self.seconds_input.text() or 0) self.timer.set(hours, minutes, seconds) self.timer_label.setText(f'Timer: Set to {hours:02d}:{minutes:02d}:{seconds:02d}') except ValueError: self.timer_label.setText('Timer: Invalid input') def start_timer(self): if self.timer.get_remaining_time() > 0: self.timer.start() else: self.timer_label.setText('Timer: Not set') def stop_timer(self): self.timer.stop() self.timer_label.setText('Timer: Stopped') def update_timer_display(self): if self.timer.running: remaining = self.timer.get_remaining_time() hours, remainder = divmod(remaining, 3600) minutes, seconds = divmod(remainder, 60) self.timer_label.setText(f'Timer: {hours:02d}:{minutes:02d}:{seconds:02d}') elif self.timer.get_remaining_time() == 0 and self.timer_label.text() != 'Timer: Not Set': self.timer_label.setText('Timer: Time\'s up!') def showUI(self): self.setWindowTitle("Timer") self.setGeometry(100, 100, self.width, self.height) super().show() if __name__ == "__main__": import sys from PyQt6.QtWidgets import QApplication app = QApplication(sys.argv) Timerui = Timer_Ui() Timerui.showUI() sys.exit(app.exec())