|
| 1 | +# pyinstaller --windowed --onefile --name "TimerApp" --icon=icon.ico --add-data "timer2.wav:." Timer_UI.py; |
| 2 | + |
| 3 | +import tkinter as tk |
| 4 | +import time |
| 5 | +import os |
| 6 | +from playsound import playsound |
| 7 | + |
| 8 | +import sys |
| 9 | +import os |
| 10 | + |
| 11 | +def resource_path(filename): |
| 12 | + if hasattr(sys, '_MEIPASS'): |
| 13 | + return os.path.join(sys._MEIPASS, filename) |
| 14 | + return os.path.join(os.path.abspath("."), filename) |
| 15 | + |
| 16 | +AUDIO_PATH = resource_path("timer2.wav") |
| 17 | + |
| 18 | + |
| 19 | +class TimerApp: |
| 20 | + def __init__(self, root): |
| 21 | + self.root = root |
| 22 | + root.title("Timer App") |
| 23 | + |
| 24 | + self.always_on_top = True |
| 25 | + self.root.attributes("-topmost", True) |
| 26 | + |
| 27 | + self.top_button = tk.Button(root, text="Top ON/OFF", command=self.toggle_top) |
| 28 | + self.top_button.grid(row=3, column=0, columnspan=4) |
| 29 | + |
| 30 | + self.time_left = 0 |
| 31 | + self.running = False |
| 32 | + |
| 33 | + self.time_label = tk.Label(root, font=('Helvetica', 48)) |
| 34 | + self.time_label.grid(row=0, column=0, columnspan=4) |
| 35 | + |
| 36 | + self.start_stop_button = tk.Button(root, text="Start/Stop", command=self.start_stop, width=10) |
| 37 | + self.start_stop_button.grid(row=1, column=0, columnspan=2) |
| 38 | + |
| 39 | + self.restart_button = tk.Button(root, text="Restart", command=self.restart, width=10) |
| 40 | + self.restart_button.grid(row=1, column=2, columnspan=2) |
| 41 | + |
| 42 | + self.time_entry = tk.Entry(root, width=10) |
| 43 | + self.time_entry.grid(row=2, column=0, columnspan=2) |
| 44 | + self.time_entry.insert(0, "10") |
| 45 | + |
| 46 | + self.set_button = tk.Button(root, text="Set", command=self.set_time, width=10) |
| 47 | + self.set_button.grid(row=2, column=2, columnspan=2) |
| 48 | + |
| 49 | + self.footer = tk.Label(root, text="© 2025 TimerApp by guilhermemartins.net", font=("Helvetica", 11), fg="grey") |
| 50 | + self.footer.grid(row=99, column=0, columnspan=4, sticky="we", pady=5) |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + def toggle_top(self): |
| 55 | + self.always_on_top = not self.always_on_top |
| 56 | + self.root.attributes("-topmost", self.always_on_top) |
| 57 | + |
| 58 | + if self.always_on_top: |
| 59 | + self.top_button.config(text="Top: ON") |
| 60 | + else: |
| 61 | + self.top_button.config(text="Top: OFF") |
| 62 | + |
| 63 | + |
| 64 | + def set_time(self): |
| 65 | + try: |
| 66 | + minutes = float(self.time_entry.get()) |
| 67 | + self.time_left = minutes * 60 # converter para segundos |
| 68 | + self.time_label.config(text=time.strftime("%M:%S", time.gmtime(self.time_left))) |
| 69 | + except: |
| 70 | + self.time_label.config(text="Invalid") |
| 71 | + |
| 72 | + def start_stop(self): |
| 73 | + if self.running: |
| 74 | + self.running = False |
| 75 | + self.time_label.config(text="Stopped", fg="white") |
| 76 | + else: |
| 77 | + self.running = True |
| 78 | + self.time_label.config(fg="white") |
| 79 | + self.timer() |
| 80 | + |
| 81 | + def restart(self): |
| 82 | + self.running = False |
| 83 | + try: |
| 84 | + minutes = float(self.time_entry.get()) |
| 85 | + self.time_left = int(minutes * 60) # minutos -> segundos |
| 86 | + self.time_label.config( |
| 87 | + text=time.strftime("%M:%S", time.gmtime(self.time_left)), |
| 88 | + fg="white" |
| 89 | + ) |
| 90 | + except: |
| 91 | + self.time_left = 0 |
| 92 | + self.time_label.config(text="Invalid", fg="white") |
| 93 | + |
| 94 | + |
| 95 | + def timer(self): |
| 96 | + if self.running: |
| 97 | + time_str = time.strftime("%M:%S", time.gmtime(self.time_left)) |
| 98 | + self.time_label.config(text=time_str) |
| 99 | + |
| 100 | + self.time_left -= 1 |
| 101 | + |
| 102 | + if self.time_left < 0: |
| 103 | + self.running = False |
| 104 | + self.time_label.config(text="Time's Up!", fg="white") |
| 105 | + playsound(AUDIO_PATH) # caminho para o ficheiro |
| 106 | + |
| 107 | + else: |
| 108 | + self.root.after(1000, self.timer) |
| 109 | + |
| 110 | +def main(): |
| 111 | + root = tk.Tk() |
| 112 | + app = TimerApp(root) |
| 113 | + root.mainloop() |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + main() |
0 commit comments