Open In App

Create a Responsive System Tray Icon using Python PyStray

Last Updated : 21 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how we can make System Tray icons and make them responsive using the PyStray module of Python. Here, we will understand the concept using the proper steps.

What is PyStray?

You are able to make a system tray icon using the PyStray library. It enables the specification of an icon, a title, and a callback function for use when the icon is enabled. After the icon has been generated, the symbol’s visibility can be toggled as well as its icon and title.

Required Modules

We will need two modules here, PyStray and pillow. To install them please write the following command:

pip install pystray
pip install pillow

Stepwise Implementation

Step 1: Import Library

We will first import the required modules.

Python3




import pystray
from PIL import Image


Step 2: Image Icon

After that, we will use an image that will act as the icon in the System Tray which we can click and get some response back. Users can choose any kind of image they want, I will be using a GeeksforGeeks image here.

Python3




# you can download gfg image from here:
image = Image.open("gfg_image.png")


Step 3: Create the Function that Responds to our clicking

The function after_click() takes two parameters, first is the icon (The image we will use as the icon), and the query is the messages which we will use later to describe what clicking on that will return. 

If we don’t provide icon.stop() method then our code will go in an Infinite Loop, if the user wants to make the code exit after each query has been resolved then use the icon. stop() in each if-else statement, if not then make an else Block at the last as I did and make it stop there. The benefit of using the second method is that the code will not stop after every time its menu items are clicked.

Python3




import pystray
from PIL import Image
 
image = Image.open("gfg_image.png")
 
def after_click(icon, query):
    if str(query) == "GeeksforGeeks Website":
        print("The Best Place to learn anything Tech \
        Related -> https://www.geeksforgeeks.org/")
        # icon.stop()
    elif str(query) == "GeeksforGeeks Youtube":
        print("Youtube Channel of GeeksforGeeks \
        is -> https://www.youtube.com/@GeeksforGeeksVideos")
        # icon.stop()
    elif str(query) == "GeeksforGeeks LinkedIn":
        print("LinkedIn of GeeksforGeeks \
        is -> https://www.linkedin.com/company/geeksforgeeks/")
    elif str(query) == "Exit":
        icon.stop()


Step 4: Creating the Menu

Here is a variable named icon we are calling the Icon method of pystray module and passing a name and the image we choose as an icon and then creating a Menu and some menu items. As a parameter of the Icon method we are passing a name, image i.e the Icon, title (this will be shown when we will hover over it), and menu (using this we are creating a menu).

Python3




icon = pystray.Icon("GFG", image, "GeeksforGeeks", menu=pystray.Menu(
    pystray.MenuItem("GeeksforGeeks Website", after_click),
    pystray.MenuItem("GeeksforGeeks Youtube", after_click),
    pystray.MenuItem("GeeksforGeeks LinkedIn", after_click),
    pystray.MenuItem("Exit", after_click)))
 
icon.run()


Complete Code

Python3




import pystray
from PIL import Image
 
image = Image.open("gfg_image.png")
 
 
def after_click(icon, query):
    if str(query) == "GeeksforGeeks Website":
        print("The Best Place to learn anything Tech \
      Related -> https://www.geeksforgeeks.org/")
        # icon.stop()
    elif str(query) == "GeeksforGeeks Youtube":
        print("Youtube Channel of GeeksforGeeks \
      is -> https://www.youtube.com/@GeeksforGeeksVideos")
        # icon.stop()
    elif str(query) == "GeeksforGeeks LinkedIn":
        print("LinkedIn of GeeksforGeeks \
      is -> https://www.linkedin.com/company/geeksforgeeks/")
    elif str(query) == "Exit":
        icon.stop()
 
 
icon = pystray.Icon("GFG", image, "GeeksforGeeks",
                    menu=pystray.Menu(
    pystray.MenuItem("GeeksforGeeks Website",
                     after_click),
    pystray.MenuItem("GeeksforGeeks Youtube",
                     after_click),
    pystray.MenuItem("GeeksforGeeks LinkedIn",
                     after_click),
    pystray.MenuItem("Exit", after_click)))
 
icon.run()


Output:

System Tray before running the code

Create a Responsive System Tray Icon using Python PyStray

 

System Tray after running the code

Create a Responsive System Tray Icon using Python PyStray

 

After Right-clicking on the Icon, It is visible in the Last line that after clicking exit() our program has stopped execution.

Create a Responsive System Tray Icon using Python PyStray

 

Output Video

Create a Responsive System Tray Icon using Python PyStray

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads