CSS 边距未应用于 Gtk 文本视图

问题描述

我有以下最小的可重现示例代码

#!/usr/bin/python3
#-*-coding: utf-8-*-

import gi
gi.require_version("Gtk","3.0")
from gi.repository import Gtk,Gdk

win = Gtk.Window()
win.connect("destroy",lambda _: Gtk.main_quit())
Box = Gtk.VBox()
win.add(Box)
view = Gtk.TextView()
buf = view.get_buffer()
buf.set_text("Hello,this is some text !")
prov = Gtk.Cssprovider.new()
prov.load_from_data("""
text {
    color: red;
    background-color: yellow;
    margin: 30px;
}
""".encode())
ctx = win.get_style_context()
ctx.add_provider_for_screen(Gdk.Screen.get_default(),prov,800)
Box.add(view)
win.show_all()
Gtk.main()

字体和背景被着色,但没有应用边距。为什么?如何解决

注意:当我使用以下 CSS 将边距应用于框时

text {
    color: red;
    background-color: yellow;
}
Box {
    margin: 30px;
}

添加 `蟒蛇 view.set("margin-left",10) view.set("margin_right,10) ...

the margins are applied. So the cause must be a wrong selector ...

解决方法

textview 中的边距没有 css 节点。您需要使用 set_left_margin(left_margin) set_right_margin(right_margin)set_justification(justification) 之类的函数。

使用

Gtk.TextView().set_left_margin(10) /*value is in pixels*/

CSS 节点

textview.view
├── border.top
├── border.left
├── text
│   ╰── [selection]
├── border.right
├── border.bottom
╰── [window.popup]

css 节点只出现在 c 文档中 c documentation

这是py文档py documentation