在 PyGObject 中设置样式 属性
Set style property in 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
但是,我似乎无法以任何方式设置样式 属性。
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'
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 中,您必须在样式 属性 的名称和样式 属性 所属的 class 的名称前加前缀:
GtkProgressBar {
-GtkProgressBar-min-horizontal-bar-height: 10px;
}
它们不是要在代码中设置的,因此 style_get_property()
没有相应的 setter 方法。
我有一个非常简单的 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
但是,我似乎无法以任何方式设置样式 属性。
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'
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 中,您必须在样式 属性 的名称和样式 属性 所属的 class 的名称前加前缀:
GtkProgressBar {
-GtkProgressBar-min-horizontal-bar-height: 10px;
}
它们不是要在代码中设置的,因此 style_get_property()
没有相应的 setter 方法。