【发布时间】:2017-08-05 15:32:40
【问题描述】:
我尝试基于 Gtk.Grid 制作一个类似 HTML 的表格(在单元格之间和表格周围有边框)。由于表格只包含标签,我开始尝试设置标签的样式。我试过这个:
label {
border: 1px solid black;
}
但它不起作用,虽然在同一个 CSS 文件中我能够将窗口的背景颜色设置为 lightblue。
所以我将我的问题简化为以下两个文件。
test.py:
import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk
win = Gtk.Window()
win.connect("delete-event", Gtk.main_quit)
win.add(Gtk.Label("These words should be red."))
provider = Gtk.CssProvider()
provider.load_from_path("style.css")
win.get_style_context().add_provider(provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)
#win.add(Gtk.Label("These words should pe red.")) # the same behavior: the label's text is not painted red
win.show_all()
Gtk.main()
style.css:
window {
background-color: lightblue; /* this works */ }
label {
color: red; /* this does not work */ }
在 MSYS2 MinGW 32 位中,我尝试了这两个:
hope@hope-PC MINGW32 ~/python+gtk/test
$ winpty python2 test.py
还有这个
hope@hope-PC MINGW32 ~/python+gtk/test
$ python2 test.py
但是我不知道如何设置标签的样式,因为上面的代码不能按照我想要的方式工作,正如我在 cmets 中指定的那样。终端中没有打印错误或警告。
我安装了这个版本的 GTK+ 和 Python 2.7:
hope@hope-PC MINGW32 ~/python+gtk/test
$ pacman -Q mingw-w64-i686-gtk3
mingw-w64-i686-gtk3 3.22.16-1
hope@hope-PC MINGW32 ~/python+gtk/test
$ python2 --version
Python 2.7.13
我使用截至 2017 年 8 月 5 日的最新 Windows 10 Creators 升级,并安装了所有更新。我使用 MSYS2 并通过终端中的 pacman -Syyu 指令完成所有升级和更新。
以下是一些相关链接(3 个指向官方文档的链接和 1 个指向另一个 SO 问题的链接):
- https://lazka.github.io/pgi-docs/#Gtk-3.0/classes/Label.html#Gtk.Label
- https://developer.gnome.org/gtk3/stable/GtkLabel.html#GtkLabel.description
- https://developer.gnome.org/gtk3/stable/theming.html
- 这个问题向我提供了在 GTK+ 应用程序中加载 CSS 文件的说明:CSS styling in GTKSharp
在这些情况下,如何设置标签样式?或者,至少,请指导我通过其他方式在 Gtk.Grid 的单元格周围制作边框。
【问题讨论】:
-
看看stackoverflow.com/questions/32162372/… 并非所有小部件都支持所有 CSS 属性。
标签: python python-2.7 gtk gtk3 msys2