gtk.Container
Prev | The gtk Class Reference | Next

## gtk.Container

gtk.Container — a base class for widgets that contain other widgets

## Synopsis

    class gtk.Container(gtk.Widget):
        def set_border_width(border_width)
        def get_border_width()
        def add(widget)
        def remove(widget)
        def set_resize_mode(resize_mode)
        def get_resize_mode()
        def check_resize()
        def forall(callback, callback_data)
        def foreach(callback, callback_data)
        def get_children()
        def propagate_expose(child, event)
        def set_focus_chain(focusable_widgets)
        def get_focus_chain()
        def unset_focus_chain()
        def set_reallocate_redraws(needs_redraws)
        def set_focus_child(child)
        def get_focus_child()
        def set_focus_vadjustment(adjustment)
        def get_focus_vadjustment()
        def set_focus_hadjustment(adjustment)
        def get_focus_hadjustment()
        def resize_children()
        def child_type()
        def add_with_properties(widget, first_prop_name, first_prop_value, ...)
        def child_set(child, first_prop_name, first_prop_value, ...)
        def child_get(child, first_prop_name, ...)
        def child_set_property(child, property_name, value)
        def child_get_property(child, property_name)

    Class Methods
        def install_child_property(property_id, pspec)
        def list_child_properties()

    Functions
        def gtk.container_class_install_child_property(klass, property_id, pspec)
        def gtk.container_class_list_child_properties(klass)

## Ancestry

    +-- gobject.GObject
      +-- gtk.Object
        +-- gtk.Widget
          +-- gtk.Container

## Implemented Interfaces

`gtk.Container` implements `gtk.Buildable`

## gtk.Container Properties

gtk.Object Properties

gtk.Widget Properties

"border-width" | Read/Write | The width of the empty border outside the containers children.
"child" | Write | The child widget in the container
"resize-mode" | Read/Write | Specify how resize events are handled. One of: `gtk.RESIZE_PARENT`, `gtk.RESIZE_QUEUE` or `gtk.RESIZE_IMMEDIATE`

## gtk.Container Style Properties

gtk.Widget Style Properties

## Attributes

"border_width" | Read | The width of the empty border outside the containers children.
"resize_mode" | Read | Specify how resize events are handled. One of: `gtk.RESIZE_PARENT`, `gtk.RESIZE_QUEUE` or `gtk.RESIZE_IMMEDIATE`
"focus_child" | Read | The child widget that has the focus
"need_resize" | Read | If True the container needs resizing
"reallocate_redraws" | Read | if True redraw the container when a child gets reallocated
"has_focus_chain" | Read | If True the container had its focus chain explicitly set

## gtk.Container Signal Prototypes

gobject.GObject Signal Prototypes

gtk.Object Signal Prototypes

gtk.Widget Signal Prototypes

"add" | `def callback(`container`, `widget`, `user_param1`, `...`)`
"check-resize" | `def callback(`container`, `user_param1`, `...`)`
"remove" | `def callback(`container`, `widget`, `user_param1`, `...`)`
"set-focus-child" | `def callback(`container`, `widget`, `user_param1`, `...`)`

## Description

The `gtk.Container` class provides common attributes and methods for a large number of widget subclasses that manage the layout of other widgets within the area of a window.

A `PyGTK` user interface is constructed by nesting widgets inside widgets. Container widgets are the inner nodes in the resulting tree of widgets: they contain other widgets. So, for example, you might have a `gtk.Window` containing a `gtk.Frame` containing a `gtk.Label`. If you wanted an image instead of a textual label inside the frame, you might replace the `gtk.Label` widget with a `gtk.Image` widget.

There are two major kinds of container widgets. Both are subclasses of the abstract `gtk.Container` base class.

The first type of container widget has a single child widget and derives from `gtk.Bin`. These containers are decorators, that add some kind of functionality to the child. For example, a `gtk.Button` makes its child into a clickable button; a `gtk.Frame` draws a frame around its child and a `gtk.Window` places its child widget inside a top-level window.

The second type of container can have more than one child; its purpose is to manage layout. This means that these containers assign sizes and positions to their children. For example, a `gtk.HBox` arranges its children in a horizontal row, and a `gtk.Table` arranges the widgets it contains in a two-dimensional grid.

To fulfill its task, a layout container must negotiate the size requirements with its parent and its children. This negotiation is carried out in two phases, size requisition and size allocation.

### Size Requisition

The size requisition of a widget is it's desired width and height. This is represented by a `gtk.Requisition`.

How a widget determines its desired size depends on the widget. A `gtk.Label`, for example, requests enough space to display all its text. Container widgets generally base their size request on the requisitions of their children.

The size requisition phase of the widget layout process operates top-down. It starts at a top-level widget, typically a GtkWindow. The top-level widget asks its child for its size requisition by calling gtk_widget_size_request(). To determine its requisition, the child asks its own children for their requisitions and so on. Finally, the top-level widget will get a requisition back from its child.

### Size Allocation

When the top-level widget has determined how much space its child would like to have, the second phase of the size negotiation, size allocation, begins. Depending on its configuration (see the `gtk.Window.set_resizable`() method), the top-level widget may be able to expand in order to satisfy the size request or it may have to ignore the size request and keep its fixed size. It then tells its child widget how much space it gets by calling the `size_allocate`() method. The child widget divides the space among its children and tells each child how much space it got, and so on. Under normal circumstances, a `gtk.Window` will always give its child the amount of space the child requested.

A child's size allocation is represented by a `gtk.gdk.Rectangle` that contains not only a width and height, but also a position (i.e. X and Y coordinates), so that containers can tell their children not only how much space is available, but also where they are positioned inside the space available to the container.

Widgets are required to honor the size allocation they receive; a size request is only a request, and widgets must be able to cope with any size.

### Child Properties

`gtk.Container` introduces child properties - these are object properties that are not specific to either the container or the contained widget, but rather to their relation. Typical examples of child properties are the "position" or "pack-type" of a widget which is contained in a `gtk.Box`.

Use the `install_child_property`() method to install child properties for a container class and the `list_child_properties`() function to get information about existing child properties.

To set the value of a child property, use the `child_set_property`(), or `child_set`() methods. To obtain the value of a child property, use the `child_get_property`(), or `child_get`() methods. To emit notification about child property changes, use the `gtk.Widget.child_notify`() method.

## Methods

### gtk.Container.set_border_width

        def set_border_width(border_width)

`border_width` : | The amount of blank space to leave outside the container. Valid values are in the range 0-65535 pixels.

The `set_border_width`() method sets the "border-width" property of the container. The border width of a container is the amount of space to leave around the outside of the container. The only exception to this is `gtk.Window`; because toplevel windows can't leave space outside, they leave the space inside. The border is added on all sides of the container.

### gtk.Container.get_border_width

        def get_border_width()

Returns : | the current border width

The `get_border_width`() method retrieves the value of the "border-width" property of the container. See `set_border_width`().

### gtk.Container.add

        def add(widget)

`widget` : | a widget to be placed inside the container

The `add`() method adds `widget` to the container. This method is typically used for simple containers such as `gtk.Window`, `gtk.Frame`, or `gtk.Button` that hold a single child widget. For layout containers that handle multiple children such as `gtk.Box` or `gtk.Table`, this function will pick default packing parameters that may not be correct. Containers that handle multiple children usually have additional methods such as `gtk.Box.pack_start`() and `gtk.Table.attach`() as an alternative to `add`(). Adding a widget to a container usually results in the resizing and redrawing of the container contents.

### gtk.Container.remove

        def remove(widget)

`widget` : | a current child of `container`

The `remove`() method removes `widget` from the container. `widget` must be inside the container. Note that the container will own a reference to `widget`, and that this may be the last reference held; so removing a widget from its container can cause that widget to be destroyed. If you want to use `widget` again, you should add a reference to it.

### gtk.Container.set_resize_mode

        def set_resize_mode(resize_mode)

`resize_mode` : | the new resize mode.

The `set-resize_mode`() method sets the "resize=mode" property of the container. The resize mode of a container determines whether a resize request will be passed to the container's parent (`gtk.RESIZE_PARENT`), queued for later execution (`gtk.RESIZE_QUEUE`) or executed immediately (`gtk.RESIZE_IMMEDIATE`).

### gtk.Container.get_resize_mode

        def get_resize_mode()

Returns : | the current resize mode

The get_resize_mode() method returns the value of the "resize-mode" property for of the container. See `set_resize_mode()`.

### gtk.Container.check_resize

        def check_resize()

The `check_resize`() method emits the "check-resize" signal on the container.

### gtk.Container.forall

        def forall(callback, callback_data=None)

`callback` : | a callback
`callback_data` : | the callback user data

The `forall`() method arranges to invoke `callback` on each child of the container including children that are considered "internal" (implementation details of the container). "Internal" children generally weren't added by the user of the container, but were added by the container implementation itself. Most applications should use the `foreach`() method, rather than the `forall`() method.

### gtk.Container.foreach

        def foreach(callback, callback_data=None)

`callback` : | a callback
`callback_data` : | the callback user data

The `foreach`() method arranges to invoke `callback` on each non-internal child of the container.

### gtk.Container.get_children

        def get_children()

Returns : | a list of the container's non-internal children.

The `get_children`() method returns the the container's non-internal children.

### gtk.Container.propagate_expose

        def propagate_expose(child, event)

`child` : | a child of the container
`event` : | a expose event sent to the container

The `propagate_expose`() method sends synthetic expose events to all children that don't have their own `gtk.gdk.Window`s when the container receives an expose event.

The `propagate_expose`() takes care of deciding whether an expose event needs to be sent to the child, intersecting the event's area with the child area, and sending the event.

In most cases, a container can simply either simply inherit the expose implementation from `gtk.Container`, or, do some drawing and then chain to the expose implementation from `gtk.Container`.

### gtk.Container.set_focus_chain

        def set_focus_chain(focusable_widgets)

`focusable_widgets` : | a list or tuple containing a chain of focusable widgets.

The `set_focus_chain`() method sets a focus chain, overriding the one computed automatically by GTK. In principle each widget in the chain should be a descendant of the container, but this is not enforced by this method, since it's allowed to set the focus chain before you pack the widgets, or have a widget in the chain that isn't always packed. The necessary checks are done when the focus chain is actually traversed.

### gtk.Container.get_focus_chain

        def get_focus_chain()

Returns : | a list containing the widgets in the focus chain if the focus chain of the container has been set explicitly or None if no focus chain has been explicitly set.

The `get_focus_chain`() method retrieves the focus chain of the container, if one has been set explicitly. If no focus chain has been explicitly set, GTK computes the focus chain based on the positions of the children. In that case, the method returns None.

### gtk.Container.unset_focus_chain

        def unset_focus_chain()

The `unset_focus_chain`() method removes a focus chain explicitly set with `set_focus_chain`().

### gtk.Container.set_reallocate_redraws

        def set_reallocate_redraws(needs_redraws)

`needs_redraws` : | the new value for the container's reallocate_redraws attribute.

The `set_reallocate_redraws`() method sets the reallocate_redraws attribute of the container to the value of `needs_redraws`. Containers requesting reallocation redraws get automatically redrawn if any of their children change allocation.

### gtk.Container.set_focus_child

        def set_focus_child(child)

`child` : | the child widget that will get the focus.

The `set_focus_child`() method emits the "set-focus-child" signal that arranges for the child widget referenced by `child` to get the focus and recalculates the container adjustments.

### gtk.Container.get_focus_child

        def get_focus_child()

Returns : | The child widget which has the focus inside container, or `None` if none is set.

### Note

This method is available in PyGTK 2.14 and above.

The `get_focus_child`() method returns the current focus child widget inside container.

### gtk.Container.set_focus_vadjustment

        def set_focus_vadjustment(adjustment)

`adjustment` : | The new vertical focus adjustment

The `set_focus_vadjustment`() method sets the vertical focus adjustment to the value of `adjustment`.

### gtk.Container.get_focus_vadjustment

        def get_focus_vadjustment()

Returns : | the vertical focus adjustment, or `None` if none has been set.

The get_focus_vadjustment() method retrieves the vertical focus adjustment for the container. See the `set_focus_vadjustment`() method.

### gtk.Container.set_focus_hadjustment

        def set_focus_hadjustment(adjustment)

`adjustment` : | The new horizontal focus adjustment

The `set_focus_hadjustment`() method sets the horizontal focus adjustment to the value of `adjustment`.

### gtk.Container.get_focus_hadjustment

        def get_focus_hadjustment()

Returns : | the horizontal focus adjustment, or `None` if none has been set.

The `get_focus_hadjustment`() method retrieves the horizontal focus adjustment for the container. See `set_focus_hadjustment`().

### gtk.Container.resize_children

        def resize_children()

The `resize_children`() method causes the container to recalculate its size and its children's sizes.

### gtk.Container.child_type

        def child_type()

Returns : | a type.

The `child_type`() method returns the type of the children that can be added to the container. Note that this may return a void type to indicate that no more children can be added, e.g. for a `gtk.Paned` which already has two children or a `gtk.Window` that already has a child.

### gtk.Container.add_with_properties

        def add_with_properties(widget, first_prop_name, first_prop_value, ...)

`widget` : | a widget to be added
`first_prop_name` : | the first property name
`first_prop_value` : | a value for the first property
`...` : | additional property name and value pairs

The `add_with_properties`() method adds the child widget specified by `widget` to the container while allowing the setting of zero or more container child property values at the same time. Containers supporting add with settable child properties are: `gtk.Box`, `gtk.Fixed`, `gtk.Notebook` and `gtk.Table`.

For example the following adds a button to a `gtk.Fixed` layout widget and sets the child properties "x" and "y" specifying the child position in the layout:

      fixed.add_with_properties(button, "x", 10, "y", 20")

### gtk.Container.child_set

        def child_set(child, first_prop_name, ...)

`child` : | the child widget
`first_prop_name` : | the first property name
`first_prop_value` : | the value of the first property
`...` : | additional property name and value pairs

The `child_set`() method sets the properties for `child` using the given property name and value pairs.

### gtk.Container.child_get

        def child_get(child, first_prop_name, ...)

`child` : | the child widget to get the child properties for
`first_prop_name` : | the first property name
`...` : | additional property names
Returns : | a tuple containing the property values requested

The `child_get`() method retrieves the requested container child properties for `child`.

### gtk.Container.child_set_property

        def child_set_property(child, property_name, value)

`child` : | the child widget
`property_name` : | the child property name
`value` : | a value to associate with the property

The `child_set_property`() method sets the property name specified by `property_name` with the value specified in `value`.

### gtk.Container.child_get_property

        def child_get_property(child, property_name)

`child` : | the child widget
`property_name` : | the child property name
Returns : | the value of the child property for the widget

The `child_get_property`() method retrieves the value of the child property specified by `property_name` for the widget `child`.

## Class Methods

### gtk.Container.install_child_property

        def install_child_property(property_id, pspec)

`property_id` : | an integer property ID
`pspec` : | a tuple containing a parameter specifications

### Note

This method is available in PyGTK 2.10 and above.

The `install_child_property`() method installs a child property for the container class using the integer ID specified by `property_id`. `pspec` is a tuple containing at least 5 items. The first 4 items contain the following parameter specification items:

- a string specifying the name of the property
- an object specifying the property type
- a string specifying the nickname for the property or `None`
- a string specifying the short description for the property or `None`

The last item must be a integer containing a combination of the GObject Param Flag Constants. Additional tuple items (if needed) are inserted between the fourth item and the last item depending on the property type:

`gobject.TYPE_CHAR` | minimum, maximum and default values
`gobject.TYPE_CHAR` | minimum, maximum and default values
`gobject.TYPE_BOOLEAN` | default value
`gobject.TYPE_INT` | minimum, maximum and default values
`gobject.TYPE_UINT` | minimum, maximum and default values
`gobject.TYPE_LONG` | minimum, maximum and default values
`gobject.TYPE_ULONG` | minimum, maximum and default values
`gobject.TYPE_INT64` | minimum, maximum and default values
`gobject.TYPE_UINT64` | minimum, maximum and default values
`gobject.TYPE_ENUM` | default value
`gobject.TYPE_FLAGS` | default value
`gobject.TYPE_FLOAT` | minimum, maximum and default values
`gobject.TYPE_DOUBLE` | minimum, maximum and default values
`gobject.TYPE_STRING` | default value
`gobject.TYPE_PARAM` | Not applicable
`gobject.TYPE_BOXED` | Not applicable
`gobject.TYPE_POINTER` | Not applicable
`gobject.TYPE_OBJECT` | Not applicable

### gtk.Container.list_child_properties

        def list_child_properties()

Returns : | the list of child properties

### Note

This method is available in PyGTK 2.10 and above.

The `list_child_properties`() method returns a list containing the child properties of the container class. See the `install_child_property`() method for more information.

## Functions

### gtk.container_class_install_child_property

        def gtk.container_class_install_child_property(klass, property_id, pspec)

`klass` : | a `gtk.Container` class or instance.
`property_id` : | an integer property ID
`pspec` : | a tuple containing a parameter specification

### Note

This function is available in PyGTK 2.4 and above.

### Warning

This function is deprecated in PyGTK 2.10 and above.

The `gtk.container_class_install_child_property`() function installs a child property for the container class specified by `klass` using the integer ID specified by `property_id`. `pspec` is a tuple containing at least 5 items. The first 4 items contain the following parameter specification items:

- a string specifying the name of the property
- an object specifying the property type
- a string specifying the nickname for the property or `None`
- a string specifying the short description for the property or `None`

The last item must be a integer containing a combination of the GObject Param Flag Constants. Additional tuple items (if needed) are inserted between the fourth item and the last item depending on the property type:

`gobject.TYPE_CHAR` | minimum, maximum and default values
`gobject.TYPE_CHAR` | minimum, maximum and default values
`gobject.TYPE_BOOLEAN` | default value
`gobject.TYPE_INT` | minimum, maximum and default values
`gobject.TYPE_UINT` | minimum, maximum and default values
`gobject.TYPE_LONG` | minimum, maximum and default values
`gobject.TYPE_ULONG` | minimum, maximum and default values
`gobject.TYPE_INT64` | minimum, maximum and default values
`gobject.TYPE_UINT64` | minimum, maximum and default values
`gobject.TYPE_ENUM` | default value
`gobject.TYPE_FLAGS` | default value
`gobject.TYPE_FLOAT` | minimum, maximum and default values
`gobject.TYPE_DOUBLE` | minimum, maximum and default values
`gobject.TYPE_STRING` | default value
`gobject.TYPE_PARAM` | Not applicable
`gobject.TYPE_BOXED` | Not applicable
`gobject.TYPE_POINTER` | Not applicable
`gobject.TYPE_OBJECT` | Not applicable

### gtk.container_class_list_child_properties

        def gtk.container_class_list_child_properties(klass)

`klass` : | a `gtk.Container` class or instance.
Returns : | a tuple containing the list of child properties

### Note

This function is available in PyGTK 2.4 and above.

### Warning

This function is deprecated in PyGTK 2.10 and above.

The `gtk.container_class_list_child_properties`() function returns a tuple containing the child properties of the container class specified by `klass`.

## Signals

### The "add" gtk.Container Signal

        def callback(container, widget, user_param1, ...)

`container` : | the container that received the signal
`widget` : | the child widget
`user_param1` : | the first user parameter (if any) specified with the `connect`() method
`...` : | additional user parameters (if any)

The "add" signal is emitted when `widget` is added to the `container`.

### The "check-resize" gtk.Container Signal

        def callback(container, user_param1, ...)

`container` : | the container that received the signal
`user_param1` : | the first user parameter (if any) specified with the `connect`() method
`...` : | additional user parameters (if any)

The "check-resize" signal is emitted when the `check_resize`() method is called forcing the recalculation of the container and its children. See the `set_resize_mode`() method for details.

### The "remove" gtk.Container Signal

        def callback(container, widget, user_param1, ...)

`container` : | the container that received the signal
`widget` : | the child widget
`user_param1` : | the first user parameter (if any) specified with the `connect`() method
`...` : | additional user parameters (if any)

The "remove" signal is emitted when `widget` is removed from `container`.

### The "set-focus-child" gtk.Container Signal

        def callback(container, widget, user_param1, ...)

`container` : | the container that received the signal
`widget` : | the child widget
`user_param1` : | the first user parameter (if any) specified with the `connect`() method
`...` : | additional user parameters (if any)

The "set-focus-child" signal is emitted when the `set_focus_child`() method is called. `widget` is set as the child in `container` with the focus.

Prev | Up | Next
gtk.ComboBoxEntry | Home | gtk.Curve