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中创建一个系统托盘应用程序。

相关推荐
孟健13 小时前
Karpathy 用 200 行纯 Python 从零实现 GPT:代码逐行解析
python
码路飞15 小时前
写了个 AI 聊天页面,被 5 种流式格式折腾了一整天 😭
javascript·python
倔强的石头_15 小时前
kingbase备份与恢复实战(二)—— sys_dump库级逻辑备份与恢复(Windows详细步骤)
数据库
曲幽17 小时前
FastAPI压力测试实战:Locust模拟真实用户并发及优化建议
python·fastapi·web·locust·asyncio·test·uvicorn·workers
敏编程1 天前
一天一个Python库:jsonschema - JSON 数据验证利器
python
前端付豪1 天前
LangChain记忆:通过Memory记住上次的对话细节
人工智能·python·langchain
碳基沙盒1 天前
OpenClaw 多 Agent 配置实战指南
运维
databook1 天前
ManimCE v0.20.1 发布:LaTeX 渲染修复与动画稳定性提升
python·动效
花酒锄作田1 天前
使用 pkgutil 实现动态插件系统
python
前端付豪2 天前
LangChain链 写一篇完美推文?用SequencialChain链接不同的组件
人工智能·python·langchain