Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set style property in PyGObject

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?

like image 831
Fenikso Avatar asked Sep 27 '15 19:09

Fenikso


1 Answers

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

like image 177
ptomato Avatar answered Nov 07 '22 17:11

ptomato