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

相关推荐
丁总学Java1 分钟前
如何使用 maxwell 同步到 redis?
数据库·redis·缓存
爱吃南瓜的北瓜9 分钟前
Redis的Key的过期策略是怎样实现的?
数据库·redis·bootstrap
丶213610 分钟前
【Nginx】在 Docker 上安装 Nginx 的详细指南
运维·nginx·docker
奈斯。zs14 分钟前
yjs08——矩阵、数组的运算
人工智能·python·线性代数·矩阵·numpy
Melody205014 分钟前
tensorflow-dataset 内网下载 指定目录
人工智能·python·tensorflow
学步_技术15 分钟前
Python编码系列—Python抽象工厂模式:构建复杂对象家族的蓝图
开发语言·python·抽象工厂模式
一心只为学23 分钟前
Oracle密码过期问题,设置永不过期
数据库·oracle
danplus24 分钟前
node发送邮件:如何实现Node.js发信功能?
服务器·node.js·外贸开发信·邮件群发·蜂邮edm邮件营销·邮件接口·营销邮件
神即道 道法自然 如来26 分钟前
Jenkins怎么设置每日自动执行构建任务?
运维·jenkins
小光学长32 分钟前
基于vue框架的宠物销售管理系统3m9h3(程序+源码+数据库+调试部署+开发环境)系统界面在最后面。
数据库