GTK4PythonTutorial icon indicating copy to clipboard operation
GTK4PythonTutorial copied to clipboard

Describe how to add a stylesheet

Open mijorus opened this issue 3 years ago • 6 comments

Hi,

I think a paragraph about how to add a CSS file (even just mentioning fact that you can do it) might be useful. Let me know what you think and if you have suggestions on this first draft.

mijorus avatar Apr 24 '22 19:04 mijorus

Certainly it would be good to have a topic on this.

But I wonder, is applying individual CSS classes to individual widgets the correct approach? I assumed it would be more standard to add a CSS file to the whole window all at once?

Taiko2k avatar Apr 24 '22 22:04 Taiko2k

Right, the code I proposed is not actually what I do when I create an app. Usually, I hook into the do_activate event and register a CSS file there (usually just one, using gresource).

However, some topics need to be introduced in order to talk about this approach, like startup hooks and gresources.

mijorus avatar Apr 26 '22 14:04 mijorus

Btw it's funny because I was attempting to create something similar, but have had no time since

mijorus avatar Apr 26 '22 15:04 mijorus

Applying to single widgets is useful, but I think readers will still be left wondering how apply it to the whole window. So I think you should add how to do that.

And this whole tutorial pretty much takes place in the on_activate method anyway right?

Here's essentially what I did in one of my apps:

style_context = self.get_style_context()
style_context.add_provider_for_display(self.get_display(), css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)

Another issue, for me I have to give a gio.file object and cant use a basic string as an argument to load_from_file like your your example. Are you using an older version?

Taiko2k avatar Apr 26 '22 23:04 Taiko2k

I'll review this PR and add more content because I think there is a lot to say; currently this PR is poorly written.

mijorus avatar Apr 29 '22 14:04 mijorus

This, by the way, just as additional info, helped us ruby folks too. kou wrote the ruby-gtk4 bindings (or, rather, helped adjust the gobject specific parts but he still had a lot to write nonetheless, all deprecation warnings in a .rb file for instance).

I was able to find out how to load up CSS in ruby-gtk4 thanks to your examples here. In ruby-gtk3 it already worked, so the API did not change much, but it took me some time until I realised I can use Gdk::Display.default. The old code was Gdk::Screen. That may not be super-relevant for the python folks, but it actually is SUPER helpful when other people search for things. In some ways github issue trackers almost replaced StackOverflow ... :D

I also almost did not notice that the API changed, e. g. from the trailing _screen name to the _display change. A bit annoying how much they changed from gtk3 to gtk4 ... CSS is cool, but the migration guide put down by the GNOME/GTK devs is fairly underwhelming really ... trial and error seems to be the way to go.

rubyFeedback avatar Sep 13 '22 16:09 rubyFeedback