Make Desktop Notifier App using Python & Tkinter

how-to-make-desktop-notifier-app-using-python

Today I will show you how to make a desktop notifier app using Python and Tkinter. A desktop notifier application is a program that can display notifications on your computer screen. You can use it to remind yourself of important tasks, events, or messages. For example, you can make a desktop notifier application that shows you the weather forecast, the latest news headlines, or the cricket scores.

Read this post if want to learn the basics of the Tkinter library: How to Create Desktop Application Using Python

What You’ll Need

Before making a desktop notifier app using Python and Tkinter, let’s make sure you have everything you need:

  • Python: Ensure you have Python installed on your computer. If not, download and install it from the official website (https://www.python.org/)
  • Tkinter: Tkinter is a Python library for creating graphical user interfaces. You can install it using the command pip install tkinter in your terminal or command prompt.
  • plyer: This package allows you to show pop-up messages on your computer screen, no matter whether you’re using Windows, macOS, or Linux. You can install it using pip install plyer command.

Let me now break the entire development process or code to create a notification application in Python into some steps.

Note: I will not explain the basics of Python or Tkinter library in this tutorial. If you want to learn basics of Python then I will suggest this Udemy course for you: Learn Python in 100 days. To learn desktop applications with tkinter I will suggest this course: GUI development with Python and Tkinter.

Step 1: Import Necessary Libraries

The first step of coding is to import the modules that we will use in our program. We only need three libraries plyer, tkinter, and time. So let’s import those.

import plyer as p
import tkinter as tk
import time as t

Step 2: Create the GUI

Now we need to create the GUI for our application. We will use the tkinter module to create a simple window with a title, a label, an entry box, and a button.

Also Read:  Make interactive dashboard using Dash with Python

The label will display the text “Enter your message:”, the entry box will allow the user to type their message, and the button will trigger the notification.

Create Application Window

We will use the tk.Tk() function to create a window object called root. Then let’s set the title of the window using the title() method and the size of the window using the geometry() method (I am using 300×100 window dimension but you can try anything). We will also use the resizable() method to prevent the user from resizing the window.

import plyer as p
import tkinter as tk
import time as t

# Setup aplication window
root = tk.Tk()
root.title("Desktop Notifier App")
root.geometry("300x100")
root.resizable(0,0)

# Run the application to display the design
root.mainloop()
design-tkinter-application-window-in-python

Add Label

Next using tk.Label() function, create a label object called label. Setting the text of the label using the text parameter and the font of the label using the font parameter. Using grid() method we are placing the label to the desired position in our application window. The grid() method takes two parameters: row and column, which specify the position of the widget in a grid layout.

import plyer as p
import tkinter as tk
import time as t

# Setup aplication window
root = tk.Tk()
root.title("Desktop Notifier App")
root.geometry("300x100")
root.resizable(0,0)

# Add labels
label = tk.Label(root, text="Enter your message:", font=("Arial", 12))
label.grid(row=0, column=0, padx=10, pady=10)

# Run the application to display the design
root.mainloop()
add-labels-to-tkinter-application-in-python

Add Notification Text Box

Now we need a text box where we write something to notify. Using tk.Entry() function we are creating a textbox entry object called entry. We can set the width of the entry box using the width parameter and the font of the entry box using the font parameter. We also use the grid() method here to place the entry box in the desired place of our application window.

import plyer as p
import tkinter as tk
import time as t

# Setup aplication window
root = tk.Tk()
root.title("Desktop Notifier App")
root.geometry("300x100")
root.resizable(0,0)

# Add labels
label = tk.Label(root, text="Enter your message:", font=("Arial", 12))
label.grid(row=0, column=0, padx=10, pady=10)

# Setup textbox entry
entry = tk.Entry(root, width=20, font=("Arial", 12))
entry.grid(row=0, column=1, padx=10, pady=10)

# Run the application to display the design
root.mainloop()
add-textbox-entry-to-tkinter-application-in-python

Define the Notification Function

Now we need to write the notification function. In this function, we need to define all the activities we want to do. Here in this example tutorial, I only want to print the notification text (tk.Entry()) one second after hitting the enter button.

def notify():
    message = entry.get()
    p.notification.notify(
        title="Desktop Notifier App",
        message=message,
        app_name="Desktop Notifier",
        app_icon=None,
        timeout=10
    )

Add button

Let’s now add that button to our application. Using the tk.Button() function we can create a button object called button. We will set the text of the button using the text parameter and the font of the button using the font parameter.

Also Read:  Upload and display image in Flask Python

We also use the command parameter to specify a function (notify() function – created in the previous step) that will be executed when the button is clicked.

Below is the final code to make desktop notifications in Python using the Tkinter library.

import plyer as p
import tkinter as tk
import time as t

def notify():
    message = entry.get()
    p.notification.notify(
        title="Desktop Notifier App",
        message=message,
        app_name="Desktop Notifier",
        app_icon=None,
        timeout=10
    )

# Setup aplication window
root = tk.Tk()
root.title("Desktop Notifier App")
root.geometry("300x100")
root.resizable(0,0)

# Add labels
label = tk.Label(root, text="Enter your message:", font=("Arial", 12))
label.grid(row=0, column=0, padx=10, pady=10)

# Setup textbox entry
entry = tk.Entry(root, width=20, font=("Arial", 12))
entry.grid(row=0, column=1, padx=10, pady=10)

# Add Button
button = tk.Button(root, text="Notify", font=("Arial", 12), command=notify)
button.grid(row=1, column=0, columnspan=2, padx=10, pady=10)

# Run the application to display the design
root.mainloop()
desktop-notifier-application-using-python-and-tkinter

Step 3: Run the app

That’s it! We have completed our desktop notifier app using Python and Tkinter. To run the app, we need to add the following line at the end of our code (which is already there in previous code):

root.mainloop()

This line will start the main loop of the Tkinter module, which will keep the window open and handle the events. You can also save your code in a file with a .py extension, such as desktop_notifier.py, and run it using the command python desktop_notifier.py in your terminal or command prompt.

Final Thought

In this tutorial, I just gave you the starting point of how to make a desktop notifier app using Python and Tkinter. We used the plyer module to access the notification feature of our operating system (for me Windows but same code will work for other operating systems) and the tkinter module to create a simple GUI. We also used the time module to control the timing of our notifications.

Also Read:  Beginners guide to Flask Python Web App framework

My intention of this tutorial is to just give you a starting point, so that you can take it to the next level. Maybe you can make a weather forecast or a latest news headlines notifier application. You just need to modify the notify() function.

Or you can add more features, such as choosing the frequency, duration, or sound of your notifications, or fetching data from external sources, such as APIs, websites, or databases. You can also change the appearance, style, or layout of your user interface. The possibilities are endless!

This is for this tutorial. If you have any questions, comments, or feedback, please feel free to leave them below.

Similar Read:

Leave a comment