Taiko2k / GTK4PythonTutorial

GTK4 + Python tutorial with code examples
446 stars 33 forks source link

Describe how to add a stylesheet #4

Closed mijorus closed 1 year ago

mijorus commented 2 years ago

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.

Taiko2k commented 2 years ago

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?

mijorus commented 2 years ago

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 commented 2 years ago

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

Taiko2k commented 2 years ago

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?

mijorus commented 2 years ago

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

rubyFeedback commented 1 year ago

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.