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: Installing on Fedora: 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: We will see the indicator running, as shown below:
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 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. 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 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 / 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 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): 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! You may also be interested in:
|
<<: Examples of common operations on MySQL foreign key constraints [view, add, modify, delete]
>>: NodeJs high memory usage troubleshooting actual combat record
Definition and Usage The <input> tag is use...
background The interface domain name is not hard-...
View the installation information of mysql: #ps -...
1. Avoid declaring the page as XML type . The pag...
Recently, I need to use a lot of fragmented pictu...
A hyperlink URL in Vm needs to be concatenated wit...
Effective solution for Ubuntu in virtual machine ...
Table of contents How to deploy MySQL service usi...
What is React React is a simple javascript UI lib...
This tutorial shares the process of manually inst...
This article example shares the specific code for...
See the effect first Implementation Code <div ...
Here we introduce the knowledge about form elemen...
The inline-block property value becomes very usef...
1. Download https://dev.mysql.com/downloads/mysql...