0

I am trying to set style in a python 3 with Gtk3 from gi.repository in Fedora 32, but it just does nothing.

I used to do this in Gtk2 and python2 with

settings = gtk.settings_get_for_screen(gdk.screen_get_default())
gtk.rc_parse_string(...) 
gtk.rc_reset_styles(settings)

This worked fine.

Now, in Python 3 with Gtk3 from gi.repository I tried this, just to make the background red.

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk as gtk
from gi.repository import Gdk as gdk
....

class Ave:
    ...
    def __init__(self):
        css_provider = gtk.CssProvider()
        css_provider.load_from_data(b'GtkWindow { background-color: #ff0000; }')
        screen = gdk.Screen.get_default()
        gtk.StyleContext.add_provider_for_screen(screen, css_provider, gtk.STYLE_PROVIDER_PRIORITY_USER)
        ...

Ave()
gtk.main()

I also tried with css_provider.load_from_path('./ave_style.css') without success (the program even complains if ave_style.css has errors, so it is loaded. I even tried with gtk.STYLE_PROVIDER_PRIORITY_APPLICATION.

Yet, nothing happens. The program completely ignores the CSS styles.

1 Answer 1

1

Unless you're using GTK < 3.20, that snippet of CSS is not valid.

If you want to match a GtkWindow, you must use the window selector, as the documentation describes.

You also want to use Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION for style introduced by your application, either programmatically or by custom resources; the USER priority is for user-provided CSS files.

Sign up to request clarification or add additional context in comments.

2 Comments

Its was that, thanks. How do I know the element name "translations"?
The selectors are always documented in the API reference of each class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.