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