Home > other >  How on Linux tasks for you to create a custom tray indicator
How on Linux tasks for you to create a custom tray indicator

Time:10-21

WeChat picture _20190520174506. The PNG
System tray icon now is still a very magical function, only need to right click on the icon and then select want action, you can greatly simplify your life and to reduce the daily behavior of a large number of useless clicking,

When it comes to useful system tray icon, we can easily think of Skype, Dropbox and VLC:

However, the system tray icon is actually much more useful; You can create your own according to their own needs in the system tray icon, this guide will teach you through a few simple steps to achieve this goal,

Precondition
We are going to use Python to implement a custom indicator system tray, Python might have been silent installation in all major Linux distributions, so you only need to make sure it has been installed (here to use version 2.7), in addition, we also need to install the good gir1.2 - appindicator3 package, the library can let we can easily create system icon indicator,

On Ubuntu/Mint/Debian installation:

Sudo apt - get the install gir1.2 - appindicator3
On Fedora installation:

Sudo DNF install libappindicator - gtk3
For other distributions, you just need to search for the package that contains "appindicator,

In GNOME Shell 3.26, the system tray icon to be deleted, you need to install the extension (or other extensions) to enable the desktop function, you can't see we create indicator,

Basic code
The following is the basis of the indicator code:

#! The/usr/bin/python

The import OS

The from gi. The repository import Gtk as Gtk, AppIndicator3 as appindicator

Def the main () :

Indicator=appindicator. Indicator. New (" customtray ", "semi - starred - --", appindicator. IndicatorCategory. APPLICATION_STATUS)

Indicator. Set_status (appindicator. IndicatorStatus. ACTIVE)

Indicator. Set_menu (menu ())

GTK. The main ()

Def menu () :

The 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 ', the quit)

Menu. Append (exittray)



Menu. Show_all ()

Return the menu



Def note (_) :

OS. The system (" gedit $HOME/Documents/notes. TXT ")

Def the quit (_) :

GTK. Main_quit ()

If __name__=="__main__" :

The main ()

We will explain how the code works, but for now, let's save the text to tray. The py, then use the Python run:

Python tray. Py
We will see the indicator to run, as shown in the figure below:

WeChat picture _20190520174506. The PNG

Create a Custom System Tray Indicator For the Tasks on Linux

Now, let's explain the principle of the magic:

The first three lines of code only used to indicate the Python path and import the required libraries,

Def the main () : this is the main indicator function, the function of the code used to initialize and create indicator,

Indicator=appindicator. Indicator. New (" customtray ", "semi - starred - --", appindicator. IndicatorCategory. APPLICATION_STATUS) : here we indicate the create a new indicator called customtray, this is the only name of indicator, such a system would not be confused with the other running indicator, at the same time, we use a semi - starred - the ICONS -- as an indicator of the default icon, you can change any other value; Such as firefox (if you want the indicator using firefox icon), or any other you want to use the icon name, the last part is associated with APPLICATION_STATUS point indicator category/range of conventional code,

Indicator. Set_status (appindicator. IndicatorStatus. ACTIVE) : this action will activate the indicator,

Indicator. Set_menu (menu ()) : here is what we want to use the menu () function (we'll define) behind the indicator for us to create a menu item, it is very important, can let you right click on the indicator after see a list of can implement behavior,

GTK. The main () : running GTK main loop,

We defined in the menu () want to offer the behavior of the indicator or project, command_one=GTK. MenuItem (' My Notes) only use text "My Notes" to initialize the first menu item, the next command_one. Connect (' activate 'note) defines menu to activate signal and behind the note () function is connected; In other words, we tell our system: "when the menu item clicked, run note () function", in the end, the menu. Append (command_one), add menu item to the list,

Exittray related line is to create an exit menu item, so that when you want to close the indicator,

Menu. Show_all () and return the menu just return to the menu items to the indicator of conventional code,

In note (_) below is click on the "My Notes" menu item needs to execute code, here are just OS. The system (" gedit $HOME/Documents/Notes. TXT ") this sentence; OS. The system function allows you to run a shell command in Python, so here we write a line command to use gedit open home directory Documents directory called notes. TXT file, for example, this can be called your daily notes program in the future!

Add tasks you need
You only need to modify the code in two places:

In the menu () the task definition for what you want in the new menu items,

Create a new function to the menu item is clicked to perform the specific behavior,

So, let's say you want to create a new menu item, after click, will use VLC play hard disk in a particular video/audio files? To do this, you just need to add the following three lines in line 17 content:

Markup
Command_two=GTK. MenuItem (' Play video/audio ') command_two. Connect (' activate 'Play) menu. Append (command_two)
Then on the 30th line add the following content:

Markup
Def play (_) : OS system (" VLC/home/& lt; Username>/Videos/somevideo. Mp4 ")
The/home//Videos/somevideo. Mp4 ` replace you'd like to play video/audio file path, now save the file and then run the indicator again:

Python tray. Py
You will see:

WeChat picture _20190520174506. The PNG

Create a Custom System Tray Indicator For the Tasks on Linux

And when you click new to create a menu item, VLC will begin to play!

To create other projects/assignments, you just need to repeat the above steps, but be careful, need to replace command_two with other commands, such as command_three, such ability won't produce conflict between variables, and then define a new function, just like play (_) functions,

The possibilities are endless; Such as I use this method to get data from the Internet (use urllib2 library) and displayed, I also use it to use mpg123 command in the background to mp3 files, and I also defines another menu item to killall mpg123 at any time to stop the playback of audio, such as Steam on CS: GO out takes time (window does not automatically closed), therefore, as an alternative method, I just minimize window and then click a self-built menu item, it will execute killall 9 csgo_linux64 command,

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

We hope indicator system tray will start automatically after system boot, instead of manual operation, every time to do this, only need to add the following command from the start the application can be (but you will need to tray. The replaced with your own path to the py) :

Nohup python/home/& lt; Username>/tray. Py & amp;
Next time you restart the system, indicator after the system starts automatically start to work! Server lease qq3003921204

Conclusion
Now you know how to for what you want to create your own task system tray indicator, according to the nature of the need to run the task every day and number, this method can save a lot of time, some people prefer from the command line to create an alias, but it need to open a terminal window or every time you need to have one of the available dropdown terminal emulator, and here, this indicator has been working in the system tray, readily available,
  • Related