From 6562d0908c3ecc6e674cfa1ba5fe1c7d68f63fd6 Mon Sep 17 00:00:00 2001 From: Lorenzo Paderi <39067225+mijorus@users.noreply.github.com> Date: Sun, 24 Apr 2022 21:20:24 +0200 Subject: [PATCH 1/2] first draft --- README.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/README.md b/README.md index 1e2ca5d..06bb68c 100644 --- a/README.md +++ b/README.md @@ -272,6 +272,33 @@ It should look like this now: The file `part1.py` is an example of the code so far. +## Adding your custom CSS stylesheet + +Did you know you can use **some** CSS rules in GTK? +Lets create a new `style.css` file that we can use to apply properties to our new label: + +```css +/* Let's create a title class */ +.title { + font-size: 25px; + font-weight: bold; +} +``` + +Then, we need to load the CSS file in our application; to achieve this, we need a [CssProvider](https://docs.gtk.org/gtk4/class.CssProvider.html). + +```python +css_provider = Gtk.CssProvider() +css_provider.load_from_file('style.css') +``` + +Finally, we can add the `title` class to our `label` + +```python +self.label.set_css_classes(['title']) +``` + + ## Adding a slider (Aka scale) Here's an example of adding a [Scale](https://docs.gtk.org/gtk4/ctor.Scale.new.html) with a range from 0 to 10 From 2e0b9e31db9d88dada4629b83193c26c14790e40 Mon Sep 17 00:00:00 2001 From: Lorenzo Paderi <39067225+mijorus@users.noreply.github.com> Date: Sun, 24 Apr 2022 21:30:42 +0200 Subject: [PATCH 2/2] add_provider_for_display --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 06bb68c..7bc1058 100644 --- a/README.md +++ b/README.md @@ -288,8 +288,12 @@ Lets create a new `style.css` file that we can use to apply properties to our ne Then, we need to load the CSS file in our application; to achieve this, we need a [CssProvider](https://docs.gtk.org/gtk4/class.CssProvider.html). ```python +# first, we need to add Gdk to our imports +from gi.repository import Gtk, Gdk + css_provider = Gtk.CssProvider() css_provider.load_from_file('style.css') +Gtk.StyleContext.add_provider_for_display(Gdk.Display.get_default(), css_provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION) ``` Finally, we can add the `title` class to our `label`