Quantcast
Viewing all articles
Browse latest Browse all 14011

Multiple CSS Providers in Python GTK3

I've read lots of posts about using CSS in GTK3 in Python, but I never really seemed to find anything about changing styles programmatically. Usually there is one big triple-quoted string that has all of the CSS definitions in it. But what if I want to change a single style programmatically in the code? If I modify the provider (load_from_data) with that single style, all of the other styles are lost (i.e. they revert to default).

I found one way that works, but I'd like to get some advice on whether that's the best way or not. I found that I can create and add another provider, and I can give it a higher priority, so any styles in it would override the styles in the big triple-quoted CSS string. The priorities are discussed here: https://lazka.github.io/pgi-docs/Gtk-3.0/classes/StyleContext.html#Gtk.StyleContext.add_provider_for_screen, and the different priorities are explained here: https://lazka.github.io/pgi-docs/Gtk-3.0/constants.html#Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION.

Here's some sample code to demonstrate. Yes, I realize it's pretty strange code... create a window with a couple of entries with weird background colors. If you change the text in the second entry field, then the background of the two entries changes. But the point is that there is some point in the script where we want to change some styles on the fly (e.g. a font chooser has been executed to choose a new text font), and this seems to give us a mechanism for doing that. You will need to use the .glade file that is found in this post to run this code: Issues with some CSS Selectors when using Python GTK3.

#!/usr/bin/env python3from gi.repository import Gtk, GdkCSS = b"""window {    background-color: yellow;}entry {    background-color: green;}"""def text_changed(self, provider):    text = self.get_text()    print(text)    newcss = "entry {background-color: red}"    provider.load_from_data(bytes(newcss.encode()))myBuilder = Gtk.Builder()myBuilder.add_from_file("pygtk3test.glade")window = myBuilder.get_object("mainWindow")#objects = myBuilder.get_objects()label2 = myBuilder.get_object("label2")entry2 = myBuilder.get_object("entry2")# set up the global CSS providerstyle_provider = Gtk.CssProvider()style_provider.load_from_data(CSS)Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(), style_provider,                                     Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)user_provider = Gtk.CssProvider()user_provider.load_from_data(bytes("".encode()))Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(), user_provider,                                     Gtk.STYLE_PROVIDER_PRIORITY_USER)entry2.connect("changed", text_changed, user_provider)window.connect("delete-event", Gtk.main_quit)window.show_all()Gtk.main()

I'm open to any suggestions as to how this could be done better. Thanks.


Viewing all articles
Browse latest Browse all 14011

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>