In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to create a custom system tray indicator for performing tasks on Linux". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
The system tray icon is still a magical feature today. Just right-click the icon and select the action you want, and you can greatly simplify your life and reduce the number of useless clicks in your daily behavior.
When it comes to useful system tray icons, it's easy to think of Skype, Dropbox, and VLC:
However, the system tray icon is actually much more useful; you can create your own system tray icon according to your needs. This guide will teach you to do this in a few simple steps.
Precondition
We are going to use Python to implement a custom system tray indicator. Python may already be installed by default in all major Linux distributions, so you just need to make sure it is installed (version 2.7 is used here). In addition, we need to install the gir1.2-appindicator3 package. The library allows us to easily create system icon indicators.
Install on Ubuntu/Mint/Debian:
Sudo apt-get install gir1.2-appindicator3
Install on Fedora:
Sudo dnf install libappindicator-gtk3
For other distributions, you just need to search for packages that contain "appindicator".
Since 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 the desktop. Otherwise you won't be able to see the indicator we created.
Basic code
The following is the basic code for the indicator:
#! / usr/bin/pythonimport osfrom gi.repository import Gtk as gtk, AppIndicator3 as appindicatordef 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 the code works later. But for now, let's save the text as tray.py and run it using Python:
Python tray.py
We will see the indicator running, as shown in the following figure:
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 are simply used to indicate the path to Python and import the required libraries.
Def main (): this is the main function of the indicator. The code for this function is used to initialize and create an indicator.
Indicator = appindicator.Indicator.new ("customtray", "semi-starred-symbolic", appindicator.IndicatorCategory.APPLICATION_STATUS): here we specify the creation of a new indicator called customtray. This is the unique name of the indicator so that the system is not confused with other running indicators. At the same time, we use an icon named semi-starred-symbolic as the default icon for the indicator. You can change it to any other value, such as firefox (if you want the indicator to use the FireFox icon), or any other icon name you want to use. The last part related to APPLICATION_STATUS is the general code that indicates the category / scope of the indicator.
Indicator.set_status (appindicator.IndicatorStatus.ACTIVE): this line activates the indicator.
Indicator.set_menu (menu ()): what I'm saying here is that we want to use the menu () function (which we'll define later) to create a menu item for our indicator. This is important to allow you to right-click the indicator and see a list of actions that can be implemented.
Gtk.main (): run the GTK main loop.
In menu () we define the behavior or project that we want the indicator to provide. Command_one = gtk.MenuItem ('My Notes') only initializes the first menu item with the text "My notes", and then command_one.connect ('activate',note) connects the menu's activate signal with the note () function defined later; in other words, we tell our system: "run the note () function when the menu item is clicked." Finally, menu.append (command_one) adds menu items to the list.
The exittray-related lines are used to create an exit menu item so that you can turn off the indicator when you want it.
Menu.show_all () and return menu are just regular code that returns menu items to the indicator.
Below note (_) is the code that needs to be executed when you click the "My Notes" menu item. This is just the sentence os.system ("gedit $HOME/Documents/notes.txt"); the os.system function allows you to run the shell command in Python, so here we write a command to use gedit to open the file named notes.txt in the Documents directory in the home directory. For example, this can be called your daily note-taking program in the future!
Add the tasks you need
You only need to modify two parts of the code:
Hongmeng official Strategic Cooperation to build HarmonyOS Technology Community
Define a new menu item for the task you want in menu ().
Create a new function to perform a specific behavior when the menu item is clicked.
So, for example, if you want to create a new menu item, after clicking it, you will use VLC to play a specific video / audio file on your hard drive? 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 on line 30:
Def play (_): os.system ("vlc / home//Videos/somevideo.mp4")
Replace / home//Videos/ somevideo.mp4` with the path of 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 to play!
To create additional projects / tasks, simply repeat the above steps. Be careful, however, that you need to replace command_two with other commands, such as command_three, so that there is no conflict between variables. Then define a new function, just like the play (_) function.
The possibilities are endless; for example, I use this method to get data from the Internet (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 playing audio at any time. For example, CS:GO exit on Steam takes a lot of time (the window doesn't close automatically), so as a workaround, I just minimize the window and click on a self-built menu item, which executes the killall-9 csgo_linux64 command.
You can use this indicator to do anything: upgrade the system package, run other scripts-literally anything.
Automatic start
We want the system tray indicator to start automatically after the system starts, instead of running it manually every time. To do this, simply add the following command to the self-launch application (but you need to replace the path of tray.py with your own path):
Nohup python / home//tray.py &
The next time you restart the system, the indicator will start working automatically after the system starts!
This is the end of "how to create a custom system tray indicator for performing tasks on Linux". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.