python - 如何在 Gtk.Grid 的单元格周围放置边框,就像在 HTML 表格中一样?

标签 python python-2.7 gtk gtk3 msys2

我尝试基于 Gtk.Grid 制作一个类似 HTML 的表格(单元格之间和表格周围有边框)。由于该表仅包含标签,我开始尝试设置标签的样式。我试过这个:

label {
    border: 1px solid black;
}

但它不起作用,尽管在同一个 CSS 文件中我能够将窗口的背景颜色设置为浅蓝色

所以我将问题简化为以下两个文件。

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

但我不知道如何设置标签的样式,因为上面的代码不能按我在注释中指定的方式工作。终端中没有打印任何错误或警告。

Screenshot

我安装了这个版本的 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 问题链接):

  1. https://lazka.github.io/pgi-docs/#Gtk-3.0/classes/Label.html#Gtk.Label
  2. https://developer.gnome.org/gtk3/stable/GtkLabel.html#GtkLabel.description
  3. https://developer.gnome.org/gtk3/stable/theming.html
  4. 这个问题告诉我在 GTK+ 应用程序中加载 CSS 文件的说明:CSS styling in GTKSharp

在这些情况下,我该如何设计标签样式?或者,至少,请指导我通过其他方式在 Gtk.Grid 的单元格周围制作边框。

最佳答案

我读了这个问题及其唯一答案:Which GTK+ elements support which CSS properties?正如 @MichaelKanis 在对我的问题的评论中所推荐的。目前,我使用 Frame 来包含我的 Label。 Windows 10 上 Adwaita 主题中的 Frame 默认情况下有一个小边框,因此网格看起来几乎像一个 HTML 表格。

我的代码的相关部分是这样的:

def set_value_at(self, row, col, val):
    l = Gtk.Label(None)
    l.set_markup(val)

    f = Gtk.Frame()
    f.add(l)

    self.attach(f, col, row, 1, 1)
    return f, l

关于python - 如何在 Gtk.Grid 的单元格周围放置边框,就像在 HTML 表格中一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45523612/

相关文章:

python - dict 在迭代期间改变了大小,但没有在 Python 中编辑它

python - 如何制作具有两个位置步长的下三角矩阵

haskell - GTK 中的小部件间距

html - 将 html 页面渲染到 gtkwindow

gtk - 在 PyGTK/GTK 中动态添加/删除笔记本页面

Python SVM 设置具有序列错误的数组元素

python - 无法捕获来自 'docker stop' 的 TERM 信号

python - 如何仅获取与模式匹配的文件名

python - 如何通过可变字符数扩展固定长度的 Python 列表?

python - 将函数编码为 JSON