Python 3和GTK3:跨平台任务栏图标

Ale*_*eev 5 python gtk python-3.x gtk3

这是可能的,以创建使用AppIndicator3菜单托盘图标。但是这种解决方案不是便携式的。例如,它在FreeBSD上不起作用,因为该系统上没有libappindicator3。我怀疑这样的代码在Windows和MacOS上也不起作用。

如果没有AppIndicator3,该如何做才能使代码在所有(或几乎所有)系统上都能工作?

Ale*_*eev 5

好吧,我想我明白了。的想法是,如果AppIndicator3不可用,则退回到Gtk.StatusIcon:

class TrayIcon:

    def __init__(self, appid, icon, menu):
        self.menu = menu

        APPIND_SUPPORT = 1
        try:
            from gi.repository import AppIndicator3
        except:
            APPIND_SUPPORT = 0

        if APPIND_SUPPORT == 1:
            self.ind = AppIndicator3.Indicator.new(
                appid, icon, AppIndicator3.IndicatorCategory.APPLICATION_STATUS)
            self.ind.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
            self.ind.set_menu(self.menu)
        else:
            self.ind = Gtk.StatusIcon()
            self.ind.set_from_file(icon)
            self.ind.connect('popup-menu', self.onPopupMenu)

    def onPopupMenu(self, icon, button, time):
        self.menu.popup(None, None, Gtk.StatusIcon.position_menu, icon, button, time)
Run Code Online (Sandbox Code Playgroud)

适用于Linux + Unity,Linux + Xfce,FreeBSD + i3。

也可以看看: