Notifications

File containing implementation for in app notifications.

 1"""File containing implementation for in app notifications.
 2"""
 3
 4import customtkinter as ctk
 5from typing import Any
 6from properties import COLOR, SYSTEM
 7if SYSTEM == 'Windows':
 8    import pywinstyles
 9
10from tools import get_from_config
11
12class Notification(ctk.CTkFrame):
13    """Class handling showing notifications in the app.
14
15    Args:
16        ctk.CTkFrame : Inheritance from customtkinter CTkFrame widget. 
17    """
18    def __init__(self, master: Any, message: str, duration_sec: float, position: str='center'):
19        """Constructor:
20             - loads font name from config
21             - converts amount of seconds to milliseconds
22             - shows itself at the end
23
24        Args:
25            master (Any): Parent widget.
26            message (str): Desired message to show on screen.
27            duration_sec (float): Amount of seconds before hiding the notification.
28            position (str, optional): Position of the notification 'center' or 'top'. Defaults to 'center'.
29        """
30        super().__init__(
31            master        = master,
32            fg_color      = COLOR.NOTIFICATION_BACKGROUND,
33            corner_radius = 0,
34            border_color  = COLOR.NOTIFICATION_OUTLINE,
35            border_width  = 3,
36            width         = 1,
37            height        = 1
38        )
39        self.font_name: str = str(get_from_config('font_name'))
40        self.message: str = message
41        self.duration: int = int(duration_sec * 1000)
42        self.position: str = position
43        self.show_notification()
44
45    def show_notification(self) -> None:
46        """Places the notification on top of all widgets relatively to the window size.
47        """
48        self.text_label = ctk.CTkLabel(
49            master     = self,
50            text       = self.message,
51            text_color = COLOR.TEXT, 
52            font       = ctk.CTkFont(self.font_name, size=32),
53            anchor     = ctk.N
54        )
55        self.text_label.pack(padx=10, pady=10)
56        if self.position == 'center':
57            self.place(relx=0.504, rely=0.47, anchor=ctk.CENTER)
58        elif self.position == 'top':
59            self.place(relx=0.5, y=20, anchor=ctk.N)
60        else:
61            self.place(relx=0.504, rely=0.47, anchor=ctk.CENTER)
62        self.text_label.bind('<Button-1>', lambda e: self.hide_notification(0))
63        self.show_animation(0)
64
65    def show_animation(self, i: int) -> None:
66        """Animates the notification appearing on the screen. At the end calls hide_notification delayed by (duration_sec * 1000).
67
68        Args:
69            i (int): Iteration value passed by recursive formula.
70        """
71        if not self.winfo_exists:
72            return
73        if i < 100:
74            i += 1
75            if SYSTEM == 'Windows':
76                pywinstyles.set_opacity(self, value=(0.01*i), color='#000001')
77            self.master.after(1, lambda: self.show_animation(i))
78        else:
79            self.master.after(self.duration, lambda: self.hide_notification(0))
80
81    def hide_notification(self, i: int) -> None:
82        """Animates the notification before removing it from the screen.
83
84        Args:
85            i (int): Iteration value passed by recursive formula.
86        """
87        if i < 100:
88            i += 1
89            if self.winfo_exists():
90                if SYSTEM == 'Windows':
91                    pywinstyles.set_opacity(self, value=(1 - (0.01*i)), color='#000001')
92                self.master.after(1, lambda: self.hide_notification(i))
93        else:
94            self.destroy()
class Notification(customtkinter.windows.widgets.ctk_frame.CTkFrame):
13class Notification(ctk.CTkFrame):
14    """Class handling showing notifications in the app.
15
16    Args:
17        ctk.CTkFrame : Inheritance from customtkinter CTkFrame widget. 
18    """
19    def __init__(self, master: Any, message: str, duration_sec: float, position: str='center'):
20        """Constructor:
21             - loads font name from config
22             - converts amount of seconds to milliseconds
23             - shows itself at the end
24
25        Args:
26            master (Any): Parent widget.
27            message (str): Desired message to show on screen.
28            duration_sec (float): Amount of seconds before hiding the notification.
29            position (str, optional): Position of the notification 'center' or 'top'. Defaults to 'center'.
30        """
31        super().__init__(
32            master        = master,
33            fg_color      = COLOR.NOTIFICATION_BACKGROUND,
34            corner_radius = 0,
35            border_color  = COLOR.NOTIFICATION_OUTLINE,
36            border_width  = 3,
37            width         = 1,
38            height        = 1
39        )
40        self.font_name: str = str(get_from_config('font_name'))
41        self.message: str = message
42        self.duration: int = int(duration_sec * 1000)
43        self.position: str = position
44        self.show_notification()
45
46    def show_notification(self) -> None:
47        """Places the notification on top of all widgets relatively to the window size.
48        """
49        self.text_label = ctk.CTkLabel(
50            master     = self,
51            text       = self.message,
52            text_color = COLOR.TEXT, 
53            font       = ctk.CTkFont(self.font_name, size=32),
54            anchor     = ctk.N
55        )
56        self.text_label.pack(padx=10, pady=10)
57        if self.position == 'center':
58            self.place(relx=0.504, rely=0.47, anchor=ctk.CENTER)
59        elif self.position == 'top':
60            self.place(relx=0.5, y=20, anchor=ctk.N)
61        else:
62            self.place(relx=0.504, rely=0.47, anchor=ctk.CENTER)
63        self.text_label.bind('<Button-1>', lambda e: self.hide_notification(0))
64        self.show_animation(0)
65
66    def show_animation(self, i: int) -> None:
67        """Animates the notification appearing on the screen. At the end calls hide_notification delayed by (duration_sec * 1000).
68
69        Args:
70            i (int): Iteration value passed by recursive formula.
71        """
72        if not self.winfo_exists:
73            return
74        if i < 100:
75            i += 1
76            if SYSTEM == 'Windows':
77                pywinstyles.set_opacity(self, value=(0.01*i), color='#000001')
78            self.master.after(1, lambda: self.show_animation(i))
79        else:
80            self.master.after(self.duration, lambda: self.hide_notification(0))
81
82    def hide_notification(self, i: int) -> None:
83        """Animates the notification before removing it from the screen.
84
85        Args:
86            i (int): Iteration value passed by recursive formula.
87        """
88        if i < 100:
89            i += 1
90            if self.winfo_exists():
91                if SYSTEM == 'Windows':
92                    pywinstyles.set_opacity(self, value=(1 - (0.01*i)), color='#000001')
93                self.master.after(1, lambda: self.hide_notification(i))
94        else:
95            self.destroy()

Class handling showing notifications in the app.

Arguments:
  • ctk.CTkFrame : Inheritance from customtkinter CTkFrame widget.
Notification( master: Any, message: str, duration_sec: float, position: str = 'center')
19    def __init__(self, master: Any, message: str, duration_sec: float, position: str='center'):
20        """Constructor:
21             - loads font name from config
22             - converts amount of seconds to milliseconds
23             - shows itself at the end
24
25        Args:
26            master (Any): Parent widget.
27            message (str): Desired message to show on screen.
28            duration_sec (float): Amount of seconds before hiding the notification.
29            position (str, optional): Position of the notification 'center' or 'top'. Defaults to 'center'.
30        """
31        super().__init__(
32            master        = master,
33            fg_color      = COLOR.NOTIFICATION_BACKGROUND,
34            corner_radius = 0,
35            border_color  = COLOR.NOTIFICATION_OUTLINE,
36            border_width  = 3,
37            width         = 1,
38            height        = 1
39        )
40        self.font_name: str = str(get_from_config('font_name'))
41        self.message: str = message
42        self.duration: int = int(duration_sec * 1000)
43        self.position: str = position
44        self.show_notification()
Constructor:
  • loads font name from config
  • converts amount of seconds to milliseconds
  • shows itself at the end
Arguments:
  • master (Any): Parent widget.
  • message (str): Desired message to show on screen.
  • duration_sec (float): Amount of seconds before hiding the notification.
  • position (str, optional): Position of the notification 'center' or 'top'. Defaults to 'center'.
font_name: str
message: str
duration: int
position: str
def show_notification(self) -> None:
46    def show_notification(self) -> None:
47        """Places the notification on top of all widgets relatively to the window size.
48        """
49        self.text_label = ctk.CTkLabel(
50            master     = self,
51            text       = self.message,
52            text_color = COLOR.TEXT, 
53            font       = ctk.CTkFont(self.font_name, size=32),
54            anchor     = ctk.N
55        )
56        self.text_label.pack(padx=10, pady=10)
57        if self.position == 'center':
58            self.place(relx=0.504, rely=0.47, anchor=ctk.CENTER)
59        elif self.position == 'top':
60            self.place(relx=0.5, y=20, anchor=ctk.N)
61        else:
62            self.place(relx=0.504, rely=0.47, anchor=ctk.CENTER)
63        self.text_label.bind('<Button-1>', lambda e: self.hide_notification(0))
64        self.show_animation(0)

Places the notification on top of all widgets relatively to the window size.

def show_animation(self, i: int) -> None:
66    def show_animation(self, i: int) -> None:
67        """Animates the notification appearing on the screen. At the end calls hide_notification delayed by (duration_sec * 1000).
68
69        Args:
70            i (int): Iteration value passed by recursive formula.
71        """
72        if not self.winfo_exists:
73            return
74        if i < 100:
75            i += 1
76            if SYSTEM == 'Windows':
77                pywinstyles.set_opacity(self, value=(0.01*i), color='#000001')
78            self.master.after(1, lambda: self.show_animation(i))
79        else:
80            self.master.after(self.duration, lambda: self.hide_notification(0))

Animates the notification appearing on the screen. At the end calls hide_notification delayed by (duration_sec * 1000).

Arguments:
  • i (int): Iteration value passed by recursive formula.
def hide_notification(self, i: int) -> None:
82    def hide_notification(self, i: int) -> None:
83        """Animates the notification before removing it from the screen.
84
85        Args:
86            i (int): Iteration value passed by recursive formula.
87        """
88        if i < 100:
89            i += 1
90            if self.winfo_exists():
91                if SYSTEM == 'Windows':
92                    pywinstyles.set_opacity(self, value=(1 - (0.01*i)), color='#000001')
93                self.master.after(1, lambda: self.hide_notification(i))
94        else:
95            self.destroy()

Animates the notification before removing it from the screen.

Arguments:
  • i (int): Iteration value passed by recursive formula.