在PyGObject中设置样式属性

凤凰

我有一个非常简单的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()

应用

我希望进度条变粗。因此,我发现有一个样式属性:

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

但是,我似乎无法以任何方式设置style属性。

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
)

但是我得到一个错误:

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'

知道如何增加进度条吗?

番茄

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

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

它们并不是要在代码中设置的,因此没有与对应的setter方法style_get_property()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Nextjs中的样式组件延迟设置属性

在Web用户控件中设置样式属性

从要设置样式的实例的属性中获取样式的值

根据Vuejs中的属性为列表中的项目设置样式

根据ViewModel中存在的属性值在ItemContainerStyleSelector中设置样式

如何在“占位符”属性中设置单个单词的样式

在 React 中为图像动态设置样式属性

在 React 的 JSX 中的地图循环中设置元素的样式属性

如何在Javascript中设置SVG CSS属性的样式?

在Windows Phone 8.1的样式中设置多属性值的值

从附加属性中以样式设置WPF MenuItem图标

如何在Vue JS中设置动态样式属性

可以在样式中设置约束布局属性吗?

如何设置Kotlin中的Toast的重力(属性样式)(出现错误)?

如何使用 ID 属性为 css 中的元素设置样式?

传单:如何在没有样式属性的GeoJSON对象中设置2000+点的样式?

在Openpyxl中设置样式

在Openpyxl中设置样式

在Dash中执行回调时,如何为children属性中的某些单词设置样式?

如何在Qt中为QWidget设置或更改特定的样式表属性,而不重置整个样式表?

如何检查html元素是否在IE6-IE8的样式标签中设置了某些样式属性?

使用React设置对象属性的样式

仅当设置了属性时才应用样式

使用thymeleaf设置CSS样式属性

选择自定义属性以设置样式

在div元素上设置样式属性

以WPF样式设置嵌套元素的属性

以样式设置网格RowDefinitions / ColumnDefinitions属性

使用样式属性设置输入行为