Set style property in PyGObject
Asked Answered
E

1

6

I have a very simple PyGObject application:

from gi.repository import Gtk, Gdk


class Window(Gtk.Window):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.set_border_width(5)

        self.progress = Gtk.ProgressBar()
        self.progress.set_fraction(0.5)

        self.box = Gtk.Box()
        self.box.pack_start(self.progress, True, True, 0)

        self.add(self.box)
        self.connect('delete-event', Gtk.main_quit)
        self.show_all()


win = Window()
Gtk.main()

Application

I want to have the progress bar thicker. So I found that there is a style property for that:

Name                        Type    Default  Flags  Short Description
min-horizontal-bar-height   int     6        r/w    Minimum horizontal height of the progress bar

However, I seem not to be able to set the style property in any way I tried.

1) I tried to use CSS:

style_provider = Gtk.CssProvider()

css = b"""
GtkProgressBar {
    border-color: #000;
    min-horizontal-bar-height: 10;
}
"""

style_provider.load_from_data(css)

Gtk.StyleContext.add_provider_for_screen(
    Gdk.Screen.get_default(),
    style_provider,
    Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)

But I got an error:

GLib.Error: gtk-css-provider-error-quark: <data>:4:37'min-horizontal-bar-height' is not a valid property name (3)

2) I tried set_style_property method and all methods described in this answer to similar question.

a)

self.progress.set_property('min-horizontal-bar-height', 10)

TypeError: object of type 'GtkProgressBar' does not have property 'min-horizontal-bar-height'

b)

self.progress.set_style_property('min-horizontal-bar-height', 10)

AttributeError: 'ProgressBar' object has no attribute 'set_style_property'

c)

self.progress.min_horizontal_bar_height = 10

GLib.Error: gtk-css-provider-error-quark: <data>:4:37'min-horizontal-bar-height' is not a valid property name (3)

d)

self.progress.props.min_horizontal_bar_height = 10

AttributeError: 'gi._gobject.GProps' object has no attribute 'min_horizontal_bar_height'

e)

self.progress.set_min_horizontal_bar_height(10)

AttributeError: 'ProgressBar' object has no attribute 'set_min_horizontal_bar_height'

Any idea how to get thicker progress bar?

Eyesore answered 27/9, 2015 at 19:40 Comment(0)
S
9

In CSS, you must prefix the name of the style property with a dash and the name of the class to which the style property belongs:

GtkProgressBar {
    -GtkProgressBar-min-horizontal-bar-height: 10px;
}

They are not meant to be set in code, so there is no corresponding setter method to style_get_property().

Shahaptian answered 28/9, 2015 at 4:44 Comment(4)
Awesome! Is there a good documentation for this and generally how to use CSS for styling in GTK? Like for example for separate widgets in runtime, styling whole app etc.? I had a really hard time finding anything relevant.Eyesore
Here's the documentation for styling with GTK CSS. It could be better but it's a pretty decent overview of what you can do.Shahaptian
I'm sorry, what? Where exactly is this "prefix with class name and dash" really documented? Not in the linked page, anyway.Poesy
That's a weirdly aggressive way to put it. Did you think I'm trying to point people to the wrong page on purpose? No, they've reorganized the CSS documentation in the latest release. Here's the page I originally linked to. It's at the bottom of that "Description" section.Shahaptian

© 2022 - 2024 — McMap. All rights reserved.