在PyGObject中设置样式属性

Fen*_*kso 4 python pygobject gtk3

我有一个非常简单的PyGObject应用程序:

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()
Run Code Online (Sandbox Code Playgroud)

应用

我希望进度条更厚.所以我发现有一个样式属性:

Name                        Type    Default  Flags  Short Description
min-horizontal-bar-height   int     6        r/w    Minimum horizontal height of the progress bar
Run Code Online (Sandbox Code Playgroud)

但是,我似乎无法以我尝试过的任何方式设置样式属性.

1)我尝试使用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
)
Run Code Online (Sandbox Code Playgroud)

但是我收到了一个错误:

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

2)我尝试了类似问题的答案中set_style_property描述的方法和所有方法.

一个)

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'

知道如何获得更厚的进度条吗?

pto*_*ato 6

在CSS中,您必须在样式属性的名称前面加上破折号和样式属性所属的类的名称:

GtkProgressBar {
    -GtkProgressBar-min-horizontal-bar-height: 10px;
}
Run Code Online (Sandbox Code Playgroud)

它们不应该在代码中设置,因此没有相应的setter方法style_get_property().

  • 这是一种奇怪的侵略性方式.您是否认为我故意将人们指向错误的页面?不,他们在最新版本中重新组织了CSS文档.[这是我最初链接的页面.](https://developer.gnome.org/gtk3/3.18/GtkCssProvider.html#GtkCssProvider.description)它位于"描述"部分的底部. (2认同)