I have made a Python application which uses GTK. I want to send the user a dialog asking for confirmation for an action, however after creating the dialog based on this tutorial, I noticed that there is no apparent way to center-align the 'Cancel' and 'OK' buttons.
The relavent code from that tutorial is as follows:
class DialogExample(Gtk.Dialog):
def __init__(self, parent):
Gtk.Dialog.__init__(self, "My Dialog", parent, 0,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK))
self.set_default_size(150, 100)
label = Gtk.Label("This is a dialog to display additional information")
box = self.get_content_area()
box.add(label)
self.show_all()
In the example above, the buttons are aligned to the right. Is there any way to center align the buttons using this method of creating dialogs?
Question: Aligning dialog buttons to center
You want to center the action_area
of a Gtk.Dialog
, which is of type Gtk.ButtonBox
.
Get the action_area
Note: Deprecated since version 3.12: Direct access to the action area is discouraged
a_area = self.get_action_area()
You need the parent
of the action_area
, which is of type Gtk.Box
Get the parent
box.
box = a_area.props.parent
To center a Gtk.Widget
you have to reset the default packing
expand=False
to True
.
To all other packing
options no change.
box.set_child_packing(a_area, True, False, 0, Gtk.PackType.END)
Now, you can center the action_area
a_area.set_center_widget(None)
Output:
Tested with Python: 3.5 - gi.__version__: 3.22.0