gpt4 book ai didi

python - 在 PyGObject 中设置样式属性

转载 作者:太空狗 更新时间:2023-10-30 02:29:22 25 4
gpt4 key购买 nike

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

Application

我想让进度条更粗。所以我发现有一个样式属性:

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

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

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方法和 this answer to similar question 中描述的所有方法.

一)

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

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

二)

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

关于python - 在 PyGObject 中设置样式属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32812087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com