beli3ver
beli3ver

Reputation: 405

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?

Upvotes: 2

Views: 734

Answers (2)

Shahan M
Shahan M

Reputation: 543

The selected answer is the way to go but I wanted to add that as of Gtk version 3.12 set_margin_left(), and set_margin_right() were deprecated.

The right way now is to use set_margin_start(), and set_margin_end().

Here is the documentation and a modified example from the selected answer.

grid.set_margin_start(20)
grid.set_margin_end(20)

The setter functions for the top and bottom are the same.

P.S - I am new to Gtk API. I came across this answer and got the deprecation warning when I tried it.

Upvotes: 0

Aran-Fey
Aran-Fey

Reputation: 43306

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)

Upvotes: 4

Related Questions