简体   繁体   English

CSS 边距未应用于 Gtk textview

[英]CSS margins not being applied to a Gtk textview

I have the following minimal reproducible example code:我有以下最小的可重现示例代码:

#!/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()

The font and background gets colored, but the margins are not applied.字体和背景被着色,但不应用边距。 Why, and what would fix it?为什么,什么会解决它?

Note: When I apply the margins to the box with following CSS注意:当我使用以下 CSS 将边距应用于框时

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

or add `python view.set("margin-left", 10) view.set("margin_right, 10)...或添加`python view.set("margin-left", 10) view.set("margin_right, 10)...

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

There are not css nodes to margins in textview. textview 中的边距没有 css 节点。 You need to use functions like set_left_margin(left_margin) set_right_margin(right_margin) or set_justification(justification) .您需要使用set_left_margin(left_margin) set_right_margin(right_margin)set_justification(justification)之类的函数。

usage用法

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

CSS nodes CSS 节点

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

css nodes only appears in c docs c documentation css 节点仅出现在 c 文档c 文档

this is the py documentation py documentation这是 py 文档py 文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM