【问题标题】:How to put borders around the cells of a Gtk.Grid, just like in an HTML table?如何在 Gtk.Grid 的单元格周围放置边框,就像在 HTML 表格中一样?
【发布时间】: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 问题的链接):

  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 的单元格周围制作边框。

【问题讨论】:

标签: python python-2.7 gtk gtk3 msys2


【解决方案1】:

我阅读了这个 SO 问题及其唯一答案:@MichaelKanis 在对我的问题的评论中推荐的Which GTK+ elements support which CSS properties?。现在,我使用Frames 来包含我的Labels。 Windows 10 上 Adwaita 主题中的 Frames 默认有一个小边框,因此网格看起来几乎像一个 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-06-15
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 2017-11-09
    • 2020-02-22
    • 2012-10-11
    • 2014-03-03
    相关资源
    最近更新 更多