3

I'm looking for a way, on a PyGObject application, to be notified when there is a theme change.

I need this notification because I'm plotting a graph using matplotlib and I'm setting as text color for the Graph the standard label color of the current GTK3 theme:

temp_label = Gtk.Label()
scrolled_window.add(temp_label)
text_color = rgba_to_hex(temp_label.get_style_context().get_color(Gtk.StateType.NORMAL))

But, when the user switches theme, I need to fetch the new label color and redraw the graph.

Currently, going from a dark to a light theme, makes the labels unreadable: readable labels unreadable labels

Roberto Leinardi
  • 10,641
  • 6
  • 65
  • 69

1 Answers1

5

The closest thing I found is to connect to gtk-theme-name:

Gtk.Settings.get_default().connect("notify::gtk-theme-name", self._on_theme_name_changed)

@staticmethod
def _on_theme_name_changed(settings, gparam):
    print("Theme name:", settings.get_property("gtk-theme-name"))
Roberto Leinardi
  • 10,641
  • 6
  • 65
  • 69
  • You might also want to watch for `gtk-application-prefer-dark-theme` changes, if your code is part of a library. But in general, you're doing it wrong. You should connect to the `style-changed` signal on the widget. And since it can produce too many re-renders, only check for _changes_ in your colours. – Přemysl J. Nov 20 '21 at 08:54