Ubuntu 中的系统托盘(适用于 Python)

在我使用Ubuntu中,尤其是系统托盘我们通常用于显示应用程序的图标和通知。如果我们想在Python中创建一个系统托盘应用程序,则可以使用第三方库pystray来实现。但是大多数人不清楚如何实现Python TKINTER 将程序放在系统托盘中,下面的具体案例可以看看。

1、问题背景

在 Ubuntu 9.04 中,我想使用 Python TKINTER 将程序放在系统托盘中,但我不知道如何实现。

2、解决方案

  • 由于 Tkinter 无法实现此功能,因此需要使用 gtk 库。

  • 以下是一个在 Ubuntu 论坛上找到的示例,它使用了 gtk 库和 Tkinter:

python 复制代码
import gtk
import Tkinter as tk

def main():
    window = tk.Tk()
    window.title("Example")

    # Create a gtk status icon
    status_icon = gtk.StatusIcon()
    status_icon.set_icon_from_file("icon.png")
    status_icon.set_tooltip("Example Tooltip")

    # Connect the status icon to the window
    status_icon.connect("activate", window.deiconify)
    status_icon.connect("popup-menu", window.show_menu)

    # Create a menu for the status icon
    menu = gtk.Menu()
    menu_item = gtk.MenuItem("Show Window")
    menu_item.connect("activate", window.deiconify)
    menu.append(menu_item)
    menu_item = gtk.MenuItem("Quit")
    menu_item.connect("activate", gtk.main_quit)
    menu.append(menu_item)

    status_icon.set_menu(menu)

    # Show the window
    window.mainloop()

if __name__ == "__main__":
    main()
  • 对于 Ubuntu 10.04+,则需要使用 AppIndicator。

  • 以下是一个在 Ubuntu Wiki 上找到的 Python 示例,它使用了 AppIndicator:

python 复制代码
import appindicator

def main():
    # Create an AppIndicator instance
    indicator = appindicator.Indicator("example", "icon.png", appindicator.CATEGORY_APPLICATION_STATUS)

    # Set the tooltip for the AppIndicator
    indicator.set_status(appindicator.STATUS_ACTIVE)
    indicator.set_menu(menu)

    # Show the AppIndicator
    indicator.show()

    # Create a menu for the AppIndicator
    menu = gtk.Menu()
    menu_item = gtk.MenuItem("Show Window")
    menu_item.connect("activate", window.deiconify)
    menu.append(menu_item)
    menu_item = gtk.MenuItem("Quit")
    menu_item.connect("activate", gtk.main_quit)
    menu.append(menu_item)

if __name__ == "__main__":
    main()
  • 此外,还可以使用 X11 方法来实现此功能,但这需要使用 ctypes 库。

  • 以下是一个示例:

python 复制代码
import ctypes

def main():
    # Get the X11 display
    display = ctypes.cdll.LoadLibrary("libX11.so.6")

    # Get the root window
    root_window = display.XDefaultRootWindow(display.XOpenDisplay(None))

    # Create a window for the tray icon
    tray_window = display.XCreateSimpleWindow(display.XOpenDisplay(None), root_window, 0, 0, 1, 1, 0, 0, 0)

    # Set the tray icon
    display.XSetIconName(display.XOpenDisplay(None), tray_window, "icon.png")

    # Show the tray icon
    display.XMapWindow(display.XOpenDisplay(None), tray_window)

    # Create a menu for the tray icon
    menu = gtk.Menu()
    menu_item = gtk.MenuItem("Show Window")
    menu_item.connect("activate", window.deiconify)
    menu.append(menu_item)
    menu_item = gtk.MenuItem("Quit")
    menu_item.connect("activate", gtk.main_quit)
    menu.append(menu_item)

    # Show the menu
    display.XSetTransientForHint(display.XOpenDisplay(None), menu.window.xid, tray_window)
    display.XGrabPointer(display.XOpenDisplay(None), menu.window.xid, True, 0, 0, 0, 0, 0)
    display.XMapRaised(display.XOpenDisplay(None), menu.window.xid)

if __name__ == "__main__":
    main()

从上面的代码我们得知,我们需要将icon.png替换为你自己的图标文件。然后定义了一个点击事件on_clicked,当点击托盘图标时会触发该事件。最后创建了一个托盘应用程序并运行。

我们可以根据自己的需求添加更多的菜单项和事件处理函数来扩展这个示例。希望这可以帮助大家在Ubuntu中创建一个系统托盘应用程序。

相关推荐
聆风吟º几秒前
CANN开源项目实战指南:使用oam-tools构建自动化故障诊断与运维可观测性体系
运维·开源·自动化·cann
神梦流5 分钟前
ops-math 算子库的扩展能力:高精度与复数运算的硬件映射策略
服务器·数据库
NPE~10 分钟前
自动化工具Drissonpage 保姆级教程(含xpath语法)
运维·后端·爬虫·自动化·网络爬虫·xpath·浏览器自动化
极客小云11 分钟前
【ComfyUI API 自动化利器:comfyui_xy Python 库使用详解】
网络·python·自动化·comfyui
让学习成为一种生活方式13 分钟前
trf v4.09.1 安装与使用--生信工具42-version2
数据库
神梦流19 分钟前
GE 引擎的内存优化终局:静态生命周期分析指导下的内存分配与复用策略
linux·运维·服务器
凡人叶枫20 分钟前
C++中输入、输出和文件操作详解(Linux实战版)| 从基础到项目落地,避坑指南
linux·服务器·c语言·开发语言·c++
啦啦啦_999924 分钟前
Redis-5-doFormatAsync()方法
数据库·redis·c#
闲人编程27 分钟前
Elasticsearch搜索引擎集成指南
python·elasticsearch·搜索引擎·jenkins·索引·副本·分片
生产队队长32 分钟前
Redis:Windows环境安装Redis,并将 Redis 进程注册为服务
数据库·redis·缓存