Amazing-Python-Scripts

Форк
0
124 строки · 4.0 Кб
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

9
class NotificationApp(tk.Tk):
10
    def __init__(self):
11
        super().__init__()
12
        self.title("Notification App")
13
        self.geometry("400x250")
14
        self.configure(bg="black")
15

16
        style = ttk.Style(self)
17
        style.configure("TLabel", foreground="white",
18
                        background="black", font=("Helvetica", 12))
19
        style.configure("TButton", foreground="black",
20
                        background="white", font=("Helvetica", 12))
21

22
        self.label_days = ttk.Label(self, text="Days:")
23
        self.label_days.pack(pady=5)
24

25
        self.entry_days = ttk.Entry(self)
26
        self.entry_days.pack()
27

28
        self.label_hours = ttk.Label(self, text="Hours:")
29
        self.label_hours.pack(pady=5)
30

31
        self.entry_hours = ttk.Entry(self)
32
        self.entry_hours.pack()
33

34
        self.label_minutes = ttk.Label(self, text="Minutes:")
35
        self.label_minutes.pack(pady=5)
36

37
        self.entry_minutes = ttk.Entry(self)
38
        self.entry_minutes.pack()
39

40
        self.label_seconds = ttk.Label(self, text="Seconds:")
41
        self.label_seconds.pack(pady=5)
42

43
        self.entry_seconds = ttk.Entry(self)
44
        self.entry_seconds.pack()
45

46
        self.label_message = ttk.Label(
47
            self, text="Enter notification message:")
48
        self.label_message.pack(pady=5)
49

50
        self.entry_message = ttk.Entry(self)
51
        self.entry_message.pack()
52

53
        self.button_set = ttk.Button(
54
            self, text="Set Notification", command=self.schedule_notification)
55
        self.button_set.pack(pady=10)
56

57
        self.label_time_left = ttk.Label(
58
            self, text="Time left: 0 days, 0:00:00")
59
        self.label_time_left.pack(pady=5)
60

61
    def show_notification(self):
62
        message = self.entry_message.get() or "This is a sample notification."
63
        now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
64
        notification_window = tk.Toplevel(self)
65
        notification_window.title("Notification")
66
        notification_window.geometry("300x150")
67
        notification_window.configure(bg="black")
68

69
        notification_label = ttk.Label(notification_window, text=f"{message}\n\nCurrent time: {now}", font=(
70
            "Helvetica", 12), foreground="white", background="black")
71
        notification_label.pack(pady=10)
72

73
        # Play notification sound
74
        try:
75
            winsound.PlaySound("SystemExclamation", winsound.SND_ALIAS)
76
        except:
77
            pass
78

79
    def get_delay_time(self):
80
        try:
81
            days = int(self.entry_days.get())
82
            hours = int(self.entry_hours.get())
83
            minutes = int(self.entry_minutes.get())
84
            seconds = int(self.entry_seconds.get())
85

86
            if days < 0 or hours < 0 or minutes < 0 or seconds < 0:
87
                messagebox.showerror(
88
                    "Error", "All values must be non-negative.")
89
                return None
90

91
            total_seconds = days * 86400 + hours * 3600 + minutes * 60 + seconds
92
            return total_seconds
93
        except ValueError:
94
            messagebox.showerror(
95
                "Error", "Invalid input. Please enter numeric values.")
96
            return None
97

98
    def schedule_notification(self):
99
        delay = self.get_delay_time()
100
        if delay is None:
101
            return
102

103
        self.button_set.config(state=tk.DISABLED)
104
        notification_thread = Thread(
105
            target=self._wait_and_notify, args=(delay,))
106
        notification_thread.start()
107

108
    def _wait_and_notify(self, delay):
109
        while delay > 0:
110
            time.sleep(1)
111
            delay -= 1
112

113
            delta = timedelta(seconds=delay)
114
            time_left = f"{delta.days} days, {delta.seconds // 3600:02d}:{(delta.seconds % 3600) // 60:02d}:{delta.seconds % 60:02d}"
115
            self.label_time_left.config(text=f"Time left: {time_left}")
116
            self.update()
117

118
        self.show_notification()
119
        self.button_set.config(state=tk.NORMAL)
120

121

122
if __name__ == "__main__":
123
    app = NotificationApp()
124
    app.mainloop()
125

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.