编程的方法添加自己的 Linux 系统托盘菜单

Spoony 54.4m2020-12-11850 次点击2 人收藏2 人感谢
Create a Custom System Tray Indicator For Your Tasks on Linux

![b61ed6a031d92a44b7e2644409dee786.png](https://ppmm.org/images/2020/12/11/b61ed6a031d92a44b7e2644409dee786.png)

### Prerequisites
We are going to build a custom system tray indicator using Python. Python is probably installed by default on all the major Linux distributions, so just check it’s there (version 2.7). Additionally, we’ll need the gir1.2-appindicator3 package installed. It’s the library allowing us to easily create system tray indicators.

To install it on Ubuntu/Mint/Debian:
```
sudo apt-get install gir1.2-appindicator3
```

### Basic Code
Here’s the basic code of 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 the code works later. But for know, just save it in a text file under the name tray.py, and run it using Python:
```
python tray.py
```

https://fosspost.org/custom-system-tray-icon-indicator-linux/
收藏 ♥ 感谢
暂无回复

登录注册 后可回复。