简体   繁体   中英

Gtk.CssProvider not working in Gtk3 Python3?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM