将 Gtk.Widget 放入带有顶部填充的 Box 中

Placing Gtk.Widget in Box with top padding

我有一个这样的盒子:

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk
[...]
box_outer = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=0)
box_outer.pack_start(Gtk.Label('Label1'), False, False, 100)
box_outer.pack_start(Gtk.Label('Label2'), False, False, 0)

我希望第一个标签距顶部 100 像素。第二个标签应该在它的正下方。正如我发现的那样,填充参数总是为所有四个方向设置填充。怎么才能设置为一个方向?

使用 Widget.set_margin_top() 作为小部件顶部的边距(而不是填充或间距)。

https://lazka.github.io/pgi-docs/#Gtk-3.0/classes/Widget.html#Gtk.Widget.set_margin_top

所以

box_outer.pack_start(Gtk.Label('Label1'), False, False, 100)

你会用

label = Gtk.Label('Label1')
label.set_margin_top(100)
box_outer.pack_start(label, False, False, 0)