|
| 1 | +import tkinter as tk |
| 2 | +from tkinter import ttk |
| 3 | +from threading import Thread |
| 4 | +import time |
| 5 | +from datetime import datetime, timedelta |
| 6 | +import winsound |
| 7 | + |
| 8 | +class NotificationApp(tk.Tk): |
| 9 | + def __init__(self): |
| 10 | + super().__init__() |
| 11 | + self.title("Notification App") |
| 12 | + self.geometry("400x250") |
| 13 | + self.configure(bg="black") |
| 14 | + |
| 15 | + style = ttk.Style(self) |
| 16 | + style.configure("TLabel", foreground="white", background="black", font=("Helvetica", 12)) |
| 17 | + style.configure("TButton", foreground="black", background="white", font=("Helvetica", 12)) |
| 18 | + |
| 19 | + self.label_days = ttk.Label(self, text="Days:") |
| 20 | + self.label_days.pack(pady=5) |
| 21 | + |
| 22 | + self.entry_days = ttk.Entry(self) |
| 23 | + self.entry_days.pack() |
| 24 | + |
| 25 | + self.label_hours = ttk.Label(self, text="Hours:") |
| 26 | + self.label_hours.pack(pady=5) |
| 27 | + |
| 28 | + self.entry_hours = ttk.Entry(self) |
| 29 | + self.entry_hours.pack() |
| 30 | + |
| 31 | + self.label_minutes = ttk.Label(self, text="Minutes:") |
| 32 | + self.label_minutes.pack(pady=5) |
| 33 | + |
| 34 | + self.entry_minutes = ttk.Entry(self) |
| 35 | + self.entry_minutes.pack() |
| 36 | + |
| 37 | + self.label_seconds = ttk.Label(self, text="Seconds:") |
| 38 | + self.label_seconds.pack(pady=5) |
| 39 | + |
| 40 | + self.entry_seconds = ttk.Entry(self) |
| 41 | + self.entry_seconds.pack() |
| 42 | + |
| 43 | + self.label_message = ttk.Label(self, text="Enter notification message:") |
| 44 | + self.label_message.pack(pady=5) |
| 45 | + |
| 46 | + self.entry_message = ttk.Entry(self) |
| 47 | + self.entry_message.pack() |
| 48 | + |
| 49 | + self.button_set = ttk.Button(self, text="Set Notification", command=self.schedule_notification) |
| 50 | + self.button_set.pack(pady=10) |
| 51 | + |
| 52 | + self.label_time_left = ttk.Label(self, text="Time left: 0 days, 0:00:00") |
| 53 | + self.label_time_left.pack(pady=5) |
| 54 | + |
| 55 | + def show_notification(self): |
| 56 | + message = self.entry_message.get() or "This is a sample notification." |
| 57 | + now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") |
| 58 | + notification_window = tk.Toplevel(self) |
| 59 | + notification_window.title("Notification") |
| 60 | + notification_window.geometry("300x150") |
| 61 | + notification_window.configure(bg="black") |
| 62 | + |
| 63 | + notification_label = ttk.Label(notification_window, text=f"{message}\n\nCurrent time: {now}", font=("Helvetica", 12), foreground="white", background="black") |
| 64 | + notification_label.pack(pady=10) |
| 65 | + |
| 66 | + # Play notification sound |
| 67 | + try: |
| 68 | + winsound.PlaySound("SystemExclamation", winsound.SND_ALIAS) |
| 69 | + except: |
| 70 | + pass |
| 71 | + |
| 72 | + def get_delay_time(self): |
| 73 | + try: |
| 74 | + days = int(self.entry_days.get()) |
| 75 | + hours = int(self.entry_hours.get()) |
| 76 | + minutes = int(self.entry_minutes.get()) |
| 77 | + seconds = int(self.entry_seconds.get()) |
| 78 | + |
| 79 | + if days < 0 or hours < 0 or minutes < 0 or seconds < 0: |
| 80 | + messagebox.showerror("Error", "All values must be non-negative.") |
| 81 | + return None |
| 82 | + |
| 83 | + total_seconds = days * 86400 + hours * 3600 + minutes * 60 + seconds |
| 84 | + return total_seconds |
| 85 | + except ValueError: |
| 86 | + messagebox.showerror("Error", "Invalid input. Please enter numeric values.") |
| 87 | + return None |
| 88 | + |
| 89 | + def schedule_notification(self): |
| 90 | + delay = self.get_delay_time() |
| 91 | + if delay is None: |
| 92 | + return |
| 93 | + |
| 94 | + self.button_set.config(state=tk.DISABLED) |
| 95 | + notification_thread = Thread(target=self._wait_and_notify, args=(delay,)) |
| 96 | + notification_thread.start() |
| 97 | + |
| 98 | + def _wait_and_notify(self, delay): |
| 99 | + while delay > 0: |
| 100 | + time.sleep(1) |
| 101 | + delay -= 1 |
| 102 | + |
| 103 | + delta = timedelta(seconds=delay) |
| 104 | + time_left = f"{delta.days} days, {delta.seconds // 3600:02d}:{(delta.seconds % 3600) // 60:02d}:{delta.seconds % 60:02d}" |
| 105 | + self.label_time_left.config(text=f"Time left: {time_left}") |
| 106 | + self.update() |
| 107 | + |
| 108 | + self.show_notification() |
| 109 | + self.button_set.config(state=tk.NORMAL) |
| 110 | + |
| 111 | +if __name__ == "__main__": |
| 112 | + app = NotificationApp() |
| 113 | + app.mainloop() |
0 commit comments