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?

Community
  • 1
  • 1
Fenikso
  • 9,251
  • 5
  • 44
  • 72

1 Answers1

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().

ptomato
  • 56,175
  • 13
  • 112
  • 165
  • 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. – Fenikso Sep 28 '15 at 09:02
  • 1
    [Here's the documentation for styling with GTK CSS](https://developer.gnome.org/gtk3/stable/GtkCssProvider.html#GtkCssProvider.description). It could be better but it's a pretty decent overview of what you can do. – ptomato Sep 29 '15 at 04:05
  • I'm sorry, what? Where exactly is this "prefix with class name and dash" *really* documented? Not in the linked page, anyway. – yPhil Jun 28 '16 at 21:25
  • 3
    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.](https://developer.gnome.org/gtk3/3.18/GtkCssProvider.html#GtkCssProvider.description) It's at the bottom of that "Description" section. – ptomato Jun 29 '16 at 05:57