【发布时间】:2021-03-25 16:54:20
【问题描述】:
StackOverflow 上的另一个用户在使用 CSS 设计 Gtk 应用程序时遇到了一些问题。我找到了部分问题的解决方案,但对于一个我什么都不知道。原帖是用 C 代码,但是下面的 Python Minimal Reproducible Example 有同样的问题:
#!/usr/bin/python3
#-*-coding: utf-8-*-
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
win = Gtk.Window()
win.connect("destroy", lambda _: Gtk.main_quit())
box = Gtk.VBox()
win.add(box)
button = Gtk.Button("Button")
prov = Gtk.CssProvider.new()
prov.load_from_data("""
button {
color: red;
background-color: green;
margin: 30px;
}
""".encode())
ctx = win.get_style_context()
ctx.add_provider(prov, 1000)
box.add(button)
win.show_all()
Gtk.main()
问题是,边距没有显示,字体和背景颜色也没有显示,而在这个例子中它们正确显示:
#!/usr/bin/python3
#-*-coding: utf-8-*-
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
win = Gtk.Window()
win.connect("destroy", lambda _: Gtk.main_quit())
box = Gtk.VBox()
win.add(box)
button = Gtk.Button("Button")
prov = Gtk.CssProvider.new()
prov.load_from_data("""
* {
color: red;
background-color: green;
margin: 30px;
}
""".encode())
ctx = button.get_style_context()
ctx.add_provider(prov, 1000)
box.add(button)
win.show_all()
Gtk.main()
这使我得出结论,选择器 button 不知何故没有达到我想要和期望的效果。为什么选择器错误,哪个是选择按钮的正确选择器?
【问题讨论】:
-
来自文档:
#define GTK_STYLE_PROVIDER_PRIORITY_USER 800 You should not use priorities higher than this, to give the user the last word.也许您的 GTK 主题会覆盖您的风格?你用 adwaita 试过了吗? -
@AlexanderDmitriev 不,我在我的机器上测试了提供的示例,优先级为 1000,但它仍然没有帮助。这里的问题是一个错误的选择器,只是我不知道什么是正确的,工作的选择器......
标签: python css styles gtk gtk3