Search code examples
pythonpython-3.xgtkgtk3

GTK Dialog Margin not working


I have created a little dialog with a grid. Here is it:

enter image description here

But I don't understand why the margin not working:

class PreferencesDialog(Gtk.Dialog):

    def __init__(self, parent):
        Gtk.Dialog.__init__(self, "Preferences", parent, 0)

        self.set_default_size(300, 300)

        grid = Gtk.Grid(column_spacing=10,
                         row_spacing=10)

        label = Gtk.Label("Custom Location")
        switch = Gtk.Switch()
        switch.set_active(False)

        grid.add(label)
        grid.margin_left = 20
        grid.margin_right = 20
        grid.margin_top = 20
        grid.margin_bottom = 20

        grid.attach(switch, 1, 0, 1, 1)

        box = self.get_content_area()
        box.add(grid)
        self.show_all()

And I see that the size of the window : 300x300 is not working anymore. Could you help me?


Solution

  • Gtk widgets are based on GObject, which means you have to access the widget's properties through the props attribute:

    grid.props.margin_left = 20
    grid.props.margin_right = 20
    grid.props.margin_top = 20
    grid.props.margin_bottom = 20
    

    Alternatively, you can use the setter functions:

    grid.set_margin_left(20)
    grid.set_margin_right(20)
    grid.set_margin_top(20)
    grid.set_margin_bottom(20)