多色文字合二为一 Gtk.Label
Multi-color text in one Gtk.Label
我正在使用 css 文件为我的界面设置主题。所以我现在有这个:
style_file = Gio.File.new_for_path('interface.css')
style_provider = Gtk.CssProvider()
style_provider.load_from_file(style_file)
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(),
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
self.label = Gtk.Label()
self.label.set_name('foobar_label')
self.label.set_markup("<span color='blue'>foo </span>"
"<span color='green'>{} </span>"
"<span color='red'>"
"bar</span>".format(baz))
我想摆脱 set_markup 调用并使用 css 来设置样式。我真的必须为颜色创建三个单独的标签吗?
可以使用 CSS 为 Gtk.Label
的子字符串设置样式,尽管它确实涉及一些变通方法。
这是一个用Javascript写的例子:https://github.com/endlessm/eos-knowledge-lib/blob/d4c9666/js/app/utils.js#L170-L225
基本上它涉及临时添加另一种样式 class 到小部件的上下文以从您的 CSS 中获取额外的样式,然后将其转换为 Pango 标记。在构建标签时以及样式更新时(style-updated
信号。)
,您会调用 format_ui_string()
我正在使用 css 文件为我的界面设置主题。所以我现在有这个:
style_file = Gio.File.new_for_path('interface.css')
style_provider = Gtk.CssProvider()
style_provider.load_from_file(style_file)
Gtk.StyleContext.add_provider_for_screen(
Gdk.Screen.get_default(),
style_provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)
self.label = Gtk.Label()
self.label.set_name('foobar_label')
self.label.set_markup("<span color='blue'>foo </span>"
"<span color='green'>{} </span>"
"<span color='red'>"
"bar</span>".format(baz))
我想摆脱 set_markup 调用并使用 css 来设置样式。我真的必须为颜色创建三个单独的标签吗?
可以使用 CSS 为 Gtk.Label
的子字符串设置样式,尽管它确实涉及一些变通方法。
这是一个用Javascript写的例子:https://github.com/endlessm/eos-knowledge-lib/blob/d4c9666/js/app/utils.js#L170-L225
基本上它涉及临时添加另一种样式 class 到小部件的上下文以从您的 CSS 中获取额外的样式,然后将其转换为 Pango 标记。在构建标签时以及样式更新时(style-updated
信号。)
format_ui_string()