You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GTK应用未使用系统图标主题问题求助

问题:Python GTK3应用无法加载指定图标主题,默认使用Hicolor

我按照GTK教程编写的Python应用能够加载settings.ini中定义的窗口主题,但图标却默认使用Hicolor主题;而其他GTK应用(如Glade、PCManFM)均能正常使用Qogir-dark图标主题。

相关代码

import gi

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

icons = ["edit-cut", "edit-paste", "edit-copy", "view-refresh"]


class IconViewWindow(Gtk.Window):
    def __init__(self):
        super().__init__()
        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)

        for icon in icons:
            pixbuf = Gtk.IconTheme.get_default().load_icon(icon, 64, 0)
            liststore.append([pixbuf, "Label"])

            self.add(iconview)


win = IconViewWindow()
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

settings.ini配置

[Settings]
gtk-button-images=1
gtk-cursor-theme-name=Qogir-dark
gtk-cursor-theme-size=0
gtk-enable-event-sounds=1
gtk-enable-input-feedback-sounds=1
gtk-font-name=Liberation Sans 14
gtk-icon-theme-name=Qogir-dark
gtk-menu-images=1
gtk-theme-name=Qogir-dark
gtk-toolbar-icon-size=GTK_ICON_SIZE_LARGE_TOOLBAR
gtk-toolbar-style=GTK_TOOLBAR_BOTH_HORIZ
gtk-xft-antialias=1
gtk-xft-hinting=1
gtk-xft-hintstyle=hintmedium
gtk-xft-rgba=rgb

解决方法

  • 强制加载用户配置的图标主题:
    GTK3有时不会自动读取settings.ini的图标主题设置,可手动获取并应用。在代码开头添加:

    from gi.repository import Gio
    # 读取GNOME桌面设置中的图标主题
    settings = Gio.Settings.new("org.gnome.desktop.interface")
    icon_theme_name = settings.get_string("icon-theme")
    Gtk.IconTheme.get_default().set_custom_theme(icon_theme_name)
    

    或者直接读取本地settings.ini文件:

    import configparser
    import os
    
    config = configparser.ConfigParser()
    config_path = os.path.expanduser("~/.config/gtk-3.0/settings.ini")
    config.read(config_path)
    if config.has_option("Settings", "gtk-icon-theme-name"):
        icon_theme = config.get("Settings", "gtk-icon-theme-name")
        Gtk.IconTheme.get_default().set_custom_theme(icon_theme)
    
  • 检查图标主题的完整性:
    确认Qogir-dark主题包含edit-cut、edit-paste等你调用的图标。如果主题缺失这些图标,GTK会自动回退到Hicolor主题。可使用gtk-icon-browser工具查看图标是否存在。

  • 调整图标加载参数:
    修改load_icon的第三个参数,添加查找标志强制优先使用主题图标:

    pixbuf = Gtk.IconTheme.get_default().load_icon(icon, 64, Gtk.IconLookupFlags.FORCE_SIZE | Gtk.IconLookupFlags.USE_BUILTIN)
    

内容的提问来源于stack exchange,提问作者armoken

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 21:47:38