Home > OS >  GTK - Python Window Symbolic Icon Color Problem
GTK - Python Window Symbolic Icon Color Problem

Time:09-22

I have a GTK3 GUI called by a simple Python 3 code. Icon is located in the /usr/share/icons/hicolor/scalable/actions/ directory. My current theme color is dark and icons look white. When I switch to white system theme GUI icons turn into black. But in my code icon looks as black instead of white when dark theme is activated.

It works when I choose the icon name (icon-symbolic) from Glade program and save the UI file. Icon file is a simple black square .svg file (drawn in Inkscape).

What is the solution for that?

OS: Debian-like Linux, Python 3, GTK 3.24

Simple Python code:

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

builder = Gtk.Builder()
builder.add_from_file('test.ui')
window1 = builder.get_object('window1')
button1 = builder.get_object('button1')

class Signals:
    def on_window1_destroy(self, widget):
        Gtk.main_quit()

builder.connect_signals(Signals())

window1.set_icon_name("icon-symbolic")
window1.show_all()
Gtk.main()

Simple UI file:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.38.2 -->
<interface>
  <requires lib="gtk " version="3.20"/>
  <object class="GtkWindow" id="window1">
    <property name="can-focus">False</property>
    <property name="default-width">300</property>
    <property name="default-height">300</property>
    <child>
      <!-- n-columns=1 n-rows=1 -->
      <object class="GtkGrid">
        <property name="visible">True</property>
        <property name="can-focus">False</property>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label" translatable="yes">Button 1</property>
            <property name="visible">True</property>
            <property name="can-focus">True</property>
            <property name="receives-default">True</property>
          </object>
          <packing>
            <property name="left-attach">0</property>
            <property name="top-attach">0</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

CodePudding user response:

I have found a solution for automatically changing icon color on the window title bar. I have used Gtk.HeaderBar instead of default window title bar and added an Gtk.Image (its name is image_headerbar) to the left of the headerbar. Finally I have set image icon by using the following code and it worked:

image_headerbar.set_from_icon_name("icon-symbolic", -1)

Icon color changes to dark/white automatically when system theme changes to white/dark.

I have tried several methods for dynamically changing icon color on the window title bar. But none of them worked if Gtk.HeaderBar is not used.

But window title bar height is a bit bigger than default window title bars when Gtk.HeaderBar is used (tested on XFCE desktop environment).

  • Related