Search code examples
pythongtk3pygtk

Placing Gtk.Widget in Box with top padding


I have a box like this:

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)

I want the first label to be 100 pixels from the top. The second label should be directly under it. As I found out, the padding parameter always sets the padding for all four directions. How can I set it for only one direction?


Solution

  • Use Widget.set_margin_top() for margin (instead of padding or spacing) at the top of a widget.

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

    So instead of

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

    you'd use

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