Gtk.Container


Gtk.Container — Base class for widgets which contain other widgets

Object Hierarchy:

    GObject
    ╰── GInitiallyUnowned
        ╰── Gtk.Widget
            ╰── Gtk.Container
                ├── Gtk.Bin
                ├── Gtk.Box
                ├── Gtk.Fixed
                ├── Gtk.FlowBox
                ├── Gtk.Grid
                ├── Gtk.HeaderBar
                ├── Gtk.Paned
                ├── Gtk.IconView
                ├── Gtk.Layout
                ├── Gtk.ListBox
                ├── Gtk.MenuShell
                ├── Gtk.Notebook
                ├── Gtk.Socket
                ├── Gtk.Stack
                ├── Gtk.Table
                ├── Gtk.TextView
                ├── Gtk.Toolbar
                ├── Gtk.ToolItemGroup
                ├── Gtk.ToolPalette
                ╰── Gtk.TreeView

Functions:


Signals:

  • “add” (container, user_data)
  • “check-resize” (container, user_data)
  • “remove” (container, user_data)
  • “set-focus-child” (container, user_data)

Description:

A GTK+ 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 GtkFrame 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 in GTK+. 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, which 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 GtkHBox arranges its children in a horizontal row, and a Gtk.Grid arranges the widgets it contains in a two-dimensional grid.

Height for width geometry management:

GTK+ uses a height-for-width (and width-for-height) geometry management system.

Height-for-width means that a widget can change how much vertical space it needs, depending on the amount of horizontal space that it is given (and similar for width-for-height).

There are some things to keep in mind when implementing container widgets that make use of GTK+’s height for width geometry management system. First, it’s important to note that a container must prioritize one of its dimensions, that is to say that a widget or container can only have a GtkSizeRequestMode that is %GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH or %GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT. However, every widget and container must be able to respond to the APIs for both dimensions, i.e. even if a widget has a request mode that is height-for-width, it is possible that its parent will request its sizes using the width-for-height APIs.

To ensure that everything works properly, here are some guidelines to follow when implementing height-for-width (or width-for-height) containers.

Each request mode involves 2 virtual methods. Height-for-width apis run through Gtk.Widget::get_preferred_width() and then through Gtk.Widget::get_preferred_height_for_width().

When handling requests in the opposite GtkSizeRequestMode it is important that every widget request at least enough space to display all of its content at all times.

When Gtk.Widget::get_preferred_height() is called on a container that is height-for-width, the container must return the height for its minimum width. This is easily achieved by simply calling the reverse apis implemented for itself as follows:


    def goo_container_get_preferred_height(widget, min_height, nat_height):
        if i_am_in_height_for_width_mode:
            min_width, nat_width = widget.get_preferred_width()
            min_height, nat_height = widget.get_preferred_height_for_width(min_width, min_height, nat_height)
        else:
            # ... many containers support both request modes, execute the
            # real width-for-height request here by returning the
            # collective heights of all widgets that are stacked
            # vertically (or whatever is appropriate for this container)

            ...

Similarly, when Gtk.Widget::get_preferred_width_for_height() is called for a container or widget that is height-for-width, it then only needs to return the base minimum width like so:

def foo_container_get_preferred_width_for_height (widget, for_height):
   if i_am_in_height_for_width_mode:        
       min_width, nat_width = widget.get_preferred_width ()       
   else:        
       # ... execute the real width-for-height request here based on
       # the required width of the children collectively if the
       # container were to be allocated the said height ...

    return min_height, nat_height

Height for width requests are generally implemented in terms of a virtual allocation of widgets in the input orientation. Assuming an height-for-width request mode, a container would implement the get_preferred_height_for_width() virtual function by first calling Gtk.Widget::get_preferred_width() for each of its children.

For each potential group of children that are lined up horizontally, the values returned by Gtk.Widget::get_preferred_width() should be collected in an array of GtkRequestedSize structures.

Any child spacing should be removed from the input for_width and then the collective size should be allocated using the gtk_distribute_natural_allocation() convenience function.

The container will then move on to request the preferred height for each child by using Gtk.Widget::get_preferred_height_for_width() and using the sizes stored in the GtkRequestedSize array.

To allocate a height-for-width container, it’s again important to consider that a container must prioritize one dimension over the other. So if a container is a height-for-width container it must first allocate all widgets horizontally using a GtkRequestedSize array and gtk_distribute_natural_allocation() and then add any extra space (if and where appropriate) for the widget to expand.

After adding all the expand space, the container assumes it was allocated sufficient height to fit all of its content. At this time, the container must use the total horizontal sizes of each widget to request the height-for-width of each of its children and store the requests in a GtkRequestedSize array for any widgets that stack vertically (for tabular containers this can be generalized into the heights and widths of rows and columns).

The vertical space must then again be distributed using Gtk.distribute_natural_allocation() while this time considering the allocated height of the widget minus any vertical spacing that the container adds. Then vertical expand space should be added where appropriate and available and the container should go on to actually allocating the child widgets.

See [Gtk.Widget’s geometry management section][geometry-management] to learn more about implementing height-for-width geometry management for widgets.

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 Gtk.Container::class_install_child_property() to install child properties for a container class and Gtk.Container::class_find_child_property() or Gtk.Container::class_list_child_properties() to get information about existing child properties.

To set the value of a child property, use Gtk.Container::child_set_property(), Gtk.Container::child_set() or Gtk.Container::child_set_valist().

To obtain the value of a child property, use Gtk.Container::child_get_property(), Gtk.Container::child_get() or Gtk.Container::child_get_valist(). To emit notification about child property changes, use Gtk.Widget::child_notify().

Gtk.Container as GtkBuildable

The Gtk.Container implementation of the GtkBuildable interface supports a \ element for children, which can contain multiple \ elements that specify child properties for the child.

Since 2.16, child properties can also be marked as translatable using the same translatable, comments and context attributes that are used for regular properties.

Since 3.16, containers can have a \ element containing multiple \ elements, one for each child that should be added to the focus chain. The name attribute gives the id of the widget.

An example of these properties in UI definitions:

<object class="GtkBox">
  <child>
    <object class="GtkEntry" id="entry1"/>
    <packing>
      <property name="pack-type">start</property>
    </packing>
  </child>
  <child>
    <object class="GtkEntry" id="entry2"/>
  </child>
  <focus-chain>
    <widget name="entry1"/>
    <widget name="entry2"/>
  </focus-chain>
</object>

Function Details:

add()

add (self, widget:Gtk.Widget)

Adds widget to container. Typically used for simple containerssuch as Gtk.Window, Gtk.Frame, or Gtk.Button; for more complicatedlayout containers such as Gtk.Box or Gtk.Grid, this function willpick default packing parameters that may not be correct. Soconsider functions such as Gtk.Box:pack_start`()` andGtk.Grid:attach() as an alternative to Gtk.Container:add() inthose cases. A widget may be added to only one container at a time;you can’t place the same widget inside two different containers. Note that some containers, such as Gtk.ScrolledWindow or Gtk.ListBox,may add intermediate children between the added widget and thecontainer.


remove()

remove (self, widget:Gtk.Widget)

Removes widget from container. widget must be inside container.Note that container will own a reference to widget, and that thismay be the last reference held; so removing a widget from itscontainer can destroy that widget. If you want to use widgetagain, you need to add a reference to it while it’s not insidea container, using g_object_ref(). If you don’t want to use widgetagain it’s usually more efficient to simply destroy it directlyusing `Gtk.Widget:destroy()` since this will remove it from thecontainer and help break any circular reference count cycles.


get_resize_mode()

get_resize_mode (self) -> Gtk.ResizeMode

Gtk.Container:get_resize_mode has been deprecated since version 3.12 and should not be used in newly-written code. Resize modes are deprecated. They aren’t necessary anymore since frame clocks and might introduce obscure bugs if used. Returns the resize mode for the container. SeeGtk.Container:set_resize_mode().

  • Returns: the current resize mode

set_resize_mode()

set_resize_mode (self, resize_mode:Gtk.ResizeMode)

Gtk.Container:set_resize_mode has been deprecated since version 3.12 and should not be used in newly-written code. Resize modes are deprecated. They aren’t necessary anymore since frame clocks and might introduce obscure bugs if used. Sets the resize mode for the container. The resize mode of a container determines whether a resize requestwill be passed to the container’s parent, queued for later executionor executed immediately.


check_resize()

check_resize (self)

foreach()

foreach (self, callback:Gtk.Callback, callback_data=None)

Invokes callback on each non-internal child of container. SeeGtk.Container:forall() for details on what constitutes an“internal” child. Most applications should useGtk.Container:foreach(), rather than Gtk.Container:forall().


get_children()

get_children (self) -> list

Returns the container’s non-internal children. SeeGtk.Container:forall() for details on what constitutes an "internal" child.

  • Returns: a newly-allocated list of the container’s non-internal children.

get_path_for_child()

get_path_for_child (self, child:Gtk.Widget) -> Gtk.WidgetPath

Returns a newly created widget path representing all the widget hierarchyfrom the toplevel down to and including child.

  • Returns: A newly created Gtk.WidgetPath

set_reallocate_redraws()

set_reallocate_redraws (self, needs_redraws:bool)

Gtk.Container:set_reallocate_redraws has been deprecated since version 3.14 and should not be used in newly-written code. Call `Gtk.Widget:queue_draw()in your size_allocate handler. Sets thereallocate_redraws` flag of the container to the given value. Containers requesting reallocation redraws get automaticallyredrawn if any of their children changed allocation.


get_focus_child()

get_focus_child (self) -> Gtk.Widget

Returns the current focus child widget inside container. This is not thecurrently focused widget. That can be obtained by calling`Gtk.Window:get_focus()`.

  • Returns: The child widget which will receive thefocus inside containerwhen the conatineris focussed,or None if none is set.

  • Since: 2.14


set_focus_child()

set_focus_child (self, child:Gtk.Widget=None)

Sets, or unsets if child is None, the focused child of container. This function emits the Gtk.Container`::set_focus_child` signal of`container`. Implementations of `Gtk.Container` can override thedefault behaviour by overriding the class closure of this signal. This is function is mostly meant to be used by widgets. Applications can useGtk.Widget:grab_focus() to manually set the focus to a specific widget.


get_focus_vadjustment()

get_focus_vadjustment (self) -> Gtk.Adjustment

Retrieves the vertical focus adjustment for the container. SeeGtk.Container:set_focus_vadjustment().

  • Returns: the vertical focus adjustment, or None ifnone has been set.

set_focus_vadjustment()

set_focus_vadjustment (self, adjustment:Gtk.Adjustment)

Hooks up an adjustment to focus handling in a container, so when achild of the container is focused, the adjustment is scrolled toshow that widget. This function sets the vertical alignment. See`Gtk.ScrolledWindow:get_vadjustment()for a typical way of obtainingthe adjustment andGtk.Container:set_focus_hadjustment()` for settingthe horizontal adjustment. The adjustments have to be in pixel units and in the same coordinatesystem as the allocation for immediate children of the container.


get_focus_hadjustment()

get_focus_hadjustment (self) -> Gtk.Adjustment

Retrieves the horizontal focus adjustment for the container. SeeGtk.Container:set_focus_hadjustment().

  • Returns: the horizontal focus adjustment, or None ifnone has been set.

set_focus_hadjustment()

set_focus_hadjustment (self, adjustment:Gtk.Adjustment)

Hooks up an adjustment to focus handling in a container, so when a childof the container is focused, the adjustment is scrolled to show thatwidget. This function sets the horizontal alignment.See `Gtk.ScrolledWindow:get_hadjustment()for a typical way of obtainingthe adjustment andGtk.Container:set_focus_vadjustment()` for settingthe vertical adjustment. The adjustments have to be in pixel units and in the same coordinatesystem as the allocation for immediate children of the container.


resize_children()

resize_children (self)

Gtk.Container:resize_children has been deprecated since version 3.10 and should not be used in newly-written code.


child_type()

child_type (self) -> GType

Returns the type of the children supported by the container. Note that this may return G_TYPE_NONE to indicate that no morechildren can be added, e.g. for a Gtk.Paned which already has twochildren.

  • Returns: a GType.

child_get_property()

child_get_property (self, child:Gtk.Widget, property_name:str, value:GObject.Value)

Gets the value of a child property for child and container.


child_set_property()

child_set_property (self, child:Gtk.Widget, property_name:str, value:GObject.Value)

Sets a child property for child and container.


child_notify()

child_notify (self, child:Gtk.Widget, child_property:str)

Emits a “child-notify” signal for thechild propertychild_property on the child. This is an analogue of g_object_notify() for child properties. Also see `Gtk.Widget:child_notify()`.

  • Since: 3.2

child_notify_by_pspec()

child_notify_by_pspec (self, child:Gtk.Widget, pspec:GObject.ParamSpec)

Emits a “child-notify” signal for thechild property specified bypspec on the child. This is an analogue of g_object_notify_by_pspec() for child properties.

  • Since: 3.18

forall()

forall (self, callback:Gtk.Callback, callback_data=None)

Invokes callback on each child of container, including childrenthat are considered “internal” (implementation details of thecontainer). “Internal” children generally weren’t added by the userof the container, but were added by the container implementationitself. Most applications should use Gtk.Container:foreach(),rather than Gtk.Container:forall(). [virtual forall]


get_border_width()

get_border_width (self) -> int

Retrieves the border width of the container. SeeGtk.Container:set_border_width().

  • Returns: the current border width

set_border_width()

set_border_width (self, border_width:int)

Sets the border width of the container. The border width of a container is the amount of space to leavearound the outside of the container. The only exception to this isGtk.Window; because toplevel windows can’t leave space outside,they leave the space inside. The border is added on all sides ofthe container. To add space to only one side, use a specific“margin” property on the child widget, for example“margin-top”.


propagate_draw()

propagate_draw (self, child:Gtk.Widget, cr:cairo.Context)

When a container receives a call to the draw function, it must sendsynthetic “draw” calls to all children that don’t have theirown GdkWindows. This function provides a convenient way of doing this.A container, when it receives a call to its “draw” function,calls Gtk.Container:propagate_draw() once for each child, passing inthe cr the container received. Gtk.Container:propagate_draw() takes care of translating the origin of cr,and deciding whether the draw needs to be sent to the child. It is aconvenient and optimized way of getting the same effect as calling`Gtk.Widget:draw()on the child directly. In most cases, a container can simply either inherit the“draw” implementation fromGtk.Container, or do some drawingand then chain to the ::draw implementation fromGtk.Container`.


set_focus_chain()

set_focus_chain (self, focusable_widgets:list)

Sets a focus chain, overriding the one computed automatically by GTK+. In principle each widget in the chain should be a descendant of thecontainer, but this is not enforced by this method, since it’s allowedto set the focus chain before you pack the widgets, or have a widgetin the chain that isn’t always packed. The necessary checks are donewhen the focus chain is actually traversed.


unset_focus_chain()

unset_focus_chain (self)

Removes a focus chain explicitly set with Gtk.Container:set_focus_chain().


results matching ""

    No results matching ""