6

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>
  • You said "it works" when you define the image in the UI file: do you mean that the icon colors are correct or incorrect? – Sylvester Kruin Sep 15 '21 at 22:43
  • Icon colors are correct. In other words, icon colors change to opposite color of the GUI (dark icons on white system GUI, white icons on dark system GUI). – pythonlearner9001 Sep 16 '21 at 03:44
  • Why not just define the icons in the UI file? – Sylvester Kruin Sep 16 '21 at 20:28
  • This is a simple example. In my code icons are defined in the UI file but sometimes icons are defined as they are defined in this simple example. Because these icons change in some situations. They are not same always. – pythonlearner9001 Sep 17 '21 at 05:23
  • Thanks for clarifying, I should have thought of that :-). – Sylvester Kruin Sep 17 '21 at 12:09
  • Try `#bebebe` instead of `#000000`? https://askubuntu.com/questions/148955/why-do-the-gnome-symbolic-icons-appear-darker-in-a-running-application – nelfin Sep 21 '21 at 11:44
  • @nelfin, I have already tried it but it did not fix the problem. – pythonlearner9001 Sep 21 '21 at 13:29
  • Do you have problem with system toolbar (where launched apps are) or with icons inside your widgets? If latter, how do you set them? – Alexander Dmitriev Sep 21 '21 at 16:08
  • @AlexanderDmitriev, I have problem with application windows and treeview rows (cellrendererpixbuf). It works on other widgets with svg icons when icons are set from Glade. But sometimes I have to set them during runtime and there is the color problem. It looks like color changeable feature of the svg images is lost when pixbuf is used for icons and color changeable feature is not supported on window titlebars even if pixbuf is not used. I have tried several methods but none of them worked when pixbuf is used. – pythonlearner9001 Sep 21 '21 at 17:09
  • I can reproduce your problem only partially: I've added a `GtkImage` to the grid, it renders ok and dynamically changes when I change theme. However, system toolbar icon is always black. I'd suggest taking a look at `GtkIconTheme`, especially `gtk_icon_theme_load_icon()` method. – Alexander Dmitriev Sep 21 '21 at 20:09

1 Answers1

2

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).