如何使用 PyGObject 为整个应用程序设置图标

How do I set an icon for the whole application using PyGObject

我在 python 中有一个非常简单的应用程序,并且使用 GTK3 作为图形用户界面。我只是想为它设置一个默认图标,所以它在 Gnome 的 Dash 中运行-shell.

我很绝望,我不知道我做错了什么。

我使用了这个代码:

Gtk.Window.__init__(self, title="Window title")
icon=("firefox")
pixbuf24 = Gtk.IconTheme.get_default().load_icon(icon, 24, 0)
pixbuf32 = Gtk.IconTheme.get_default().load_icon(icon, 32, 0)
pixbuf48 = Gtk.IconTheme.get_default().load_icon(icon, 48, 0)
pixbuf64 = Gtk.IconTheme.get_default().load_icon(icon, 64, 0)
pixbuf96 = Gtk.IconTheme.get_default().load_icon(icon, 96, 0)
self.set_icon_list([pixbuf24, pixbuf32, pixbuf48, pixbuf64, pixbuf96]);

我没有收到任何错误消息,但是没有显示任何图标。我选择 firefox 进行测试并确保图标存在。

谁能帮我弄清楚为什么图标不显示?

P.S。 我尝试 运行 来自@morningbird 的代码在我的电脑上,但图标仍然没有出现在 Dash 中。

Icon is shown in the window, but not in Dash

我不得不将屏幕截图保留为 link,因为我不允许在此处包含图片。

我决定测试代码,因为它看起来应该可以正常工作。我将您的逻辑与附加的 IconViewer 合并到 Gtk.Window 中,以查看加载到其中一个 pixbuf 中的图标。

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
from gi.repository.GdkPixbuf import Pixbuf

icon = ("firefox")

class IconViewWindow(Gtk.Window):

  def __init__(self):
    Gtk.Window.__init__(self)
    self.set_default_size(200, 200)

    liststore = Gtk.ListStore(Pixbuf, str)
    iconview = Gtk.IconView.new()
    iconview.set_model(liststore)
    iconview.set_pixbuf_column(0)
    iconview.set_text_column(1)

    pixbuf24 = Gtk.IconTheme.get_default().load_icon(icon, 24, 0)
    pixbuf32 = Gtk.IconTheme.get_default().load_icon(icon, 32, 0)
    pixbuf48 = Gtk.IconTheme.get_default().load_icon(icon, 48, 0)
    pixbuf64 = Gtk.IconTheme.get_default().load_icon(icon, 64, 0)
    pixbuf96 = Gtk.IconTheme.get_default().load_icon(icon, 96, 0)
    self.set_icon_list([pixbuf24, pixbuf32, pixbuf48, pixbuf64, pixbuf96])
    liststore.append([pixbuf64, "firefox"])

    self.add(iconview)

win = IconViewWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

运行 它与 Gnome 3.24 下的 python3 一起导致在破折号和所有 DE 元素中正确显示 Gtk.Window 的图标。

编辑:

也许进行适当的图标发现(因为您目前没有使用自行安装的图标)可能有助于获得一些见解。

icon_name = "firefox"
icon_theme = Gtk.IconTheme.get_default()

found_icons = set()
for res in range(24, 32, 48, 64, 96):
    icon = icon_theme.lookup_icon(icon_name, res, 0)
    #print(icon)
    if icon != None:
        found_icons.add(icon.get_filename())

if len(found_icons) > 0:
    print("\n".join(found_icons))
    sizes = Gtk.IconTheme.get_default().get_icon_sizes(icon_name)
    max_size = max(sizes)
    print("max size = {} ({})".format(max_size, sizes))
    pixbuf = icon_theme.load_icon(icon_name, max_size, 0)
    self.set_default_icon_list([pixbuf])

编辑 2:

一般来说,Gtk.Window.set_icon_name("icon-name") 是使用已安装图标的更好方法,因为系统会为您处理所有图标大小选择。使用设置大小的本地 PNG 图标时,使用 Gtk.Windowset_default_icon_list([pixbufs]) 是一个不错的选择。

如果问题不在于 "firefox" 图标可用性,我会感到惊讶,因为这些方法已得到充分记录和测试。

终于找到问题的原因了

韦兰。该代码在 X11 下运行良好,但在 Wayland 上运行不佳。再次感谢@morningbird 的帮助。我已经快疯了。当我问这个问题时,我不知道这会有什么不同。

要让它在 Wayland 中运行,您必须使用

GLib.set_prgname('app_name')

app_name 必须对应于相应 *.desktop 文件中的 exec= 条目。之后,name= 将用于应用程序名称,icon= 将用于 Dash 中的图标。