Create a custom system tray indicator for your tasks on Linux

Create a custom system tray indicator for your tasks on Linux

System tray icons are still a magical feature today. By simply right-clicking an icon and selecting the desired action, you can dramatically simplify your life and reduce a lot of unnecessary clicks in your daily activities.

When it comes to useful system tray icons, Skype, Dropbox, and VLC come to mind:

However, system tray icons are actually much more useful; you can create your own system tray icons based on your needs. This guide will teach you how to do this in a few simple steps.

Prerequisites

We are going to implement a custom system tray indicator using Python. Python is probably already installed by default on all major Linux distributions, so you just need to make sure it is installed (version 2.7 is used here). In addition, we also need to install the gir1.2-appindicator3 package. This library allows us to easily create system icon indicators.

Installation on Ubuntu/Mint/Debian:

sudo apt-get install gir1.2-appindicator3

Installing on Fedora:

sudo dnf install libappindicator-gtk3

For other distributions, just search for the package containing "appindicator".

Starting with GNOME Shell 3.26, the system tray icon has been removed. You need to install this extension (or other extensions) to enable this feature for your desktop. Otherwise you won't be able to see the indicator we created.

Basic code

Here is the basic code for the indicator:

#!/usr/bin/python
import os
from gi.repository import Gtk as gtk, AppIndicator3 as appindicator
def main():
 indicator = appindicator.Indicator.new("customtray", "semi-starred-symbolic", appindicator.IndicatorCategory.APPLICATION_STATUS)
 indicator.set_status(appindicator.IndicatorStatus.ACTIVE)
 indicator.set_menu(menu())
 gtk.main()
def menu():
 menu = gtk.Menu()
 command_one = gtk.MenuItem('My Notes')
 command_one.connect('activate', note)
 menu.append(command_one)
 exittray = gtk.MenuItem('Exit Tray')
 exittray.connect('activate', quit)
 menu.append(exittray)
 menu.show_all()
 return menu
def note(_):
 os.system("gedit $HOME/Documents/notes.txt")
def quit(_):
 gtk.main_quit()
if __name__ == "__main__":
 main()

We'll explain how this code works shortly. But for now, let's save this text as tray.py and run it using Python:

python tray.py

We will see the indicator running, as shown below:

Create a Custom System Tray Indicator For Your Tasks on Linux

Now, let's explain how this magic works:

The first three lines of code simply specify the Python path and import the required libraries.

def main(): This is the main function of the indicator. The code in this function is used to initialize and create the indicator.

indicator = appindicator.Indicator.new("customtray","semi-starred-symbolic",

appindicator.IndicatorCategory.APPLICATION_STATUS) : Here we specify that a new indicator named customtray is created. This is a unique name for the indicator so that the system does not confuse it with other running indicators. At the same time we use the icon named semi-starred-symbolic as the default icon for the indicator. You can change this to any other value; such as firefox (if you want the indicator to use the FireFox icon), or any other icon name you want. The last part associated with APPLICATION_STATUS is a general code indicating the category/range of the indicator.
indicator.set_status(appindicator.IndicatorStatus.ACTIVE) : This line activates the indicator.

indicator.set_menu(menu()) : Here we say that we want to use the menu() function (which we will define later) to create menu items for our indicator. This is important so that you can right-click the indicator and see a list of possible actions.

gtk.main() : Runs the GTK main loop.

In menu() we define the behavior or items we want the indicator to provide. command_one = gtk.MenuItem('My Notes') simply initializes the first menu item with the text "My notes", then command_one.connect('activate', note) connects the menu's activate signal to the note() function defined later; in other words, we tell our system: "when this menu item is clicked, run the note() function". Finally, menu.append(command_one) adds the menu item to the list.
The exittray related lines are there to create an exit menu item so you can close the indicator if you want.

menu.show_all() and return menu are just regular code that returns the menu items to the indicator.

Below the note(_) is the code that will be executed when the "My Notes" menu item is clicked. Here we just write os.system("gedit $HOME/Documents/notes.txt") ; the os.system function allows you to run shell commands from within Python, so here we write a line of command to use gedit to open a file called notes.txt in the Documents directory in our home directory. For example, this could become your daily note-taking program from now on!

Add the tasks you need

You only need to modify two places in the code:

menu()

So, let's say you wanted to create a new menu item that, when clicked, would play a specific video/audio file from your hard drive using VLC? To do this, just add the following three lines at line 17:

command_two = gtk.MenuItem('Play video/audio')
command_two.connect('activate', play)
menu.append(command_two)

Then add the following to line 30:

def play(_):
os.system("vlc /home/<username>/Videos/somevideo.mp4")

Replace / home//Videos/somevideo.mp4` with the path to the video/audio file you want to play. Now save the file and run the indicator again:

python tray.py

You will see:

Create a Custom System Tray Indicator For Your Tasks on Linux

And when you click on the newly created menu item, VLC will start playing!

To create other projects/tasks, just repeat the above steps. But be careful, you need to replace command_two with something else, such as command_three, so that there is no conflict between the variables. Then define the new function, just like the play(_) function.

The possibilities are endless; for example I use this method to fetch data from the web (using the urllib2 library) and display it. I also use it to play mp3 files in the background using the mpg123 command, and I have defined another menu item to killall mpg123 to stop the audio at any time. For example, exiting CS:GO on Steam takes a long time (the window doesn't close automatically), so as a workaround, I just minimize the window and click a self-built menu item that executes the killall -9 csgo_linux64 command.

You can use this indicator to do anything: upgrade system packages, run other scripts - literally anything.

Automatic Start

We hope that the system tray indicator can be automatically started after the system starts, without having to run it manually every time. To do this, just add the following command to your startup application (but you need to replace the path to tray.py with your own):

nohup python/home/<username>/tray.py&

Next time you restart the system, the indicator will start working automatically after the system starts!

in conclusion

You now know how to create your own system tray indicators for the tasks you want. Depending on the nature and number of tasks that need to be run each day, this approach can save a significant amount of time. Some people prefer to create aliases from the command line, but this requires you to open a terminal window every time or have a drop-down terminal emulator available, while here, this system tray indicator is always working and available.

Have you used this method to run your tasks before? Would love to hear your thoughts.

Summarize

The above is what I introduced to you about creating a custom system tray indicator for your tasks on Linux. I hope it will be helpful to you. If you have any questions, please leave me a message and I will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

You may also be interested in:
  • How to solve the problem of Linux tray flashing continuously

<<:  Examples of common operations on MySQL foreign key constraints [view, add, modify, delete]

>>:  NodeJs high memory usage troubleshooting actual combat record

Recommend

Detailed explanation of HTML's <input> tag and how to disable it

Definition and Usage The <input> tag is use...

Analyze how uniapp dynamically obtains the interface domain name

background The interface domain name is not hard-...

View MySQL installation information under Linux server

View the installation information of mysql: #ps -...

Summary of 16 XHTML1.0 and HTML Compatibility Guidelines

1. Avoid declaring the page as XML type . The pag...

Analysis and solution of Chinese garbled characters in HTML hyperlinks

A hyperlink URL in Vm needs to be concatenated wit...

Solution to Ubuntu cannot connect to the network

Effective solution for Ubuntu in virtual machine ...

How to deploy MySQL and Redis services using Docker

Table of contents How to deploy MySQL service usi...

Teach you how to implement a react from html

What is React React is a simple javascript UI lib...

Manually install mysql5.7.10 on Ubuntu

This tutorial shares the process of manually inst...

Mini Program Custom TabBar Component Encapsulation

This article example shares the specific code for...

CSS3 realizes the graphic falling animation effect

See the effect first Implementation Code <div ...

A collection of information about forms and form submission operations in HTML

Here we introduce the knowledge about form elemen...

5 ways to quickly remove the blank space of Inline-Block in HTML

The inline-block property value becomes very usef...

Basic installation process of mysql5.7.19 under winx64 (details)

1. Download https://dev.mysql.com/downloads/mysql...