Send Desktop notification from pygame

For notification handling we will use plyer library

pip install plyer

We will add this functionality in our pygame code

  • When the user add ther notification button it will show the notification

The complete code


import pygame
import sys
from pygame.locals import *
from plyer import notification

pygame.init()

clock = pygame.time.Clock()
user_display = pygame.display.Info()
width, height = 800, 600
screen = pygame.display.set_mode((width, height))

background = (255, 255, 255)
red = (255, 0, 0)
cornflower_blue = (128, 149, 255)
button_color = cornflower_blue
button_width, button_height = 250, 40
button_x = (width - button_width) // 2
button_y = (height - button_height) // 2

button_rect = pygame.Rect(button_x, button_y, button_width, button_height)


def button():
    pygame.draw.rect(screen, button_color, button_rect, border_radius=10)
    button_font = pygame.font.Font(pygame.font.get_default_font(), 20)
    button_text = button_font.render('Notification Button', True, (0, 0, 0))
    text_rect = button_text.get_rect(center=button_rect.center)
    screen.blit(button_text, text_rect)


game_running = True
while game_running:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
            if button_rect.collidepoint(event.pos):
                notification.notify(title='Notification', message='Notification From Pygame')
                print("notification sent")

        if event.type == pygame.MOUSEMOTION:
            if button_rect.collidepoint(event.pos):
                button_color = red
            else:
                button_color = cornflower_blue

    screen.fill(background)
    button()
    pygame.display.update()


Get More pygame examples including this example from github

https://github.com/01one/pygameExamples

Related Posts

Pygame Project: Create the simplest digital clock
April 28, 2024

We will use python datetime module for our clock; We get the current time and separate the time into hour minutes and second. First get the current time Get the time in hour, minute and second Now got our clock time components. It will be like this import datetime current_time = datetime.datetime.now() hour = current_time.hour […]

Simulate Bird Flocking Behavior with pygame: Algorithm and Code Explained
March 23, 2024

Bird flocking is a wonderful example of how simple behaviors can lead to complex, beautiful patterns in nature. We’ll learn the key principles behind this flocking behavior and write a python program to simulate this wonderful phenomenon. The principles Bird Flocking Simulation Algorithm Setting Up the Simulation Each Frame of the Simulation The pygame example […]

Display Matplotlib Plots in Pygame: A Step-by-Step Guide
March 16, 2024

Our tasks for this Import necessary module import sys import pygame import matplotlib from pygame.locals import * import matplotlib.pyplot as plt from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas This code imports a tool from Matplotlib that lets you save plots as image files (like PNG or JPG). Use this for […]