Skip to content

Gtk.CenterBox

class — extends Widget, Accessible, Buildable, ConstraintTarget, Orientable

Arranges three children in a row, keeping the middle child centered as well as possible.

<picture> <source srcset="centerbox-dark.png" media="(prefers-color-scheme: dark)"> <img alt="An example GtkCenterBox" src="centerbox.png"> </picture>

To add children to GtkCenterBox, use CenterBox.set_start_widget, CenterBox.set_center_widget and CenterBox.set_end_widget.

The sizing and positioning of children can be influenced with the align and expand properties of the children.

GtkCenterBox as GtkBuildable

The GtkCenterBox implementation of the GtkBuildable interface supports placing children in the 3 positions by specifying “start”, “center” or “end” as the “type” attribute of a <child> element.

CSS nodes

GtkCenterBox uses a single CSS node with the name “box”,

The first child of the GtkCenterBox will be allocated depending on the text direction, i.e. in left-to-right layouts it will be allocated on the left and in right-to-left layouts on the right.

In vertical orientation, the nodes of the children are arranged from top to bottom.

Accessibility

Until GTK 4.10, GtkCenterBox used the AccessibleRole.group role.

Starting from GTK 4.12, GtkCenterBox uses the AccessibleRole.generic role.

Constructors

new

@classmethod
def new(cls) -> Widget

Creates a new GtkCenterBox.

Methods

get_baseline_position

def get_baseline_position(self) -> BaselinePosition

Gets the baseline position of the center box.

See CenterBox.set_baseline_position.

get_center_widget

def get_center_widget(self) -> Widget | None

Gets the center widget.

get_end_widget

def get_end_widget(self) -> Widget | None

Gets the end widget.

get_shrink_center_last

def get_shrink_center_last(self) -> bool

Gets whether the center widget shrinks after other children.

get_start_widget

def get_start_widget(self) -> Widget | None

Gets the start widget.

set_baseline_position

def set_baseline_position(self, position: BaselinePosition | int) -> None

Sets the baseline position of a center box.

This affects only horizontal boxes with at least one baseline aligned child. If there is more vertical space available than requested, and the baseline is not allocated by the parent then position is used to allocate the baseline with respect to the extra space available.

Parameters:

  • position — the baseline position

set_center_widget

def set_center_widget(self, child: Widget | None = ...) -> None

Sets the center widget.

To remove the existing center widget, pass NULL.

Parameters:

  • child — the new center widget

set_end_widget

def set_end_widget(self, child: Widget | None = ...) -> None

Sets the end widget.

To remove the existing end widget, pass NULL.

Parameters:

  • child — the new end widget

set_shrink_center_last

def set_shrink_center_last(self, shrink_center_last: bool) -> None

Sets whether to shrink the center widget after other children.

By default, when there's no space to give all three children their natural widths, the start and end widgets start shrinking and the center child keeps natural width until they reach minimum width.

If shrink_center_last is false, start and end widgets keep natural width and the center widget starts shrinking instead.

Parameters:

  • shrink_center_last — whether to shrink the center widget after others

set_start_widget

def set_start_widget(self, child: Widget | None = ...) -> None

Sets the start widget.

To remove the existing start widget, pass NULL.

Parameters:

  • child — the new start widget

Properties

baseline_position

baseline_position: BaselinePosition | int  # read/write

The position of the baseline aligned widget if extra space is available.

center_widget

center_widget: Widget  # read/write

The widget that is placed at the center position.

end_widget

end_widget: Widget  # read/write

The widget that is placed at the end position.

In vertical orientation, the end position is at the bottom. In horizontal orientation, the end position is at the trailing edge with respect to the text direction.

shrink_center_last

shrink_center_last: bool  # read/write

Whether to shrink the center widget after other children.

By default, when there's no space to give all three children their natural widths, the start and end widgets start shrinking and the center child keeps natural width until they reach minimum width.

If false, start and end widgets keep natural width and the center widget starts shrinking instead.

start_widget

start_widget: Widget  # read/write

The widget that is placed at the start position.

In vertical orientation, the start position is at the top. In horizontal orientation, the start position is at the leading edge with respect to the text direction.