GTK 对话框边距不起作用

GTK Dialog 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()

我发现 window : 300x300 的大小不再有效。 你能帮帮我吗?

Gtk 小部件基于 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

或者,您可以使用 setter functions:

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

选择的答案是可行的方法,但我想从 Gtk 版本 3.12 开始添加它 set_margin_left(),并且 set_margin_right() 已被弃用。

现在正确的方法是使用set_margin_start()set_margin_end()

这里是 documentation 和所选答案的修改示例。

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

顶部和底部的setter功能相同

P.S - 我是 Gtk 的新手 API。我遇到了这个答案,但在尝试时收到了弃用警告。