Chapter 6. Widget Overview

The general steps to creating a widget in GTK are:

  1. Creation - one of various functions to create a new widget. The returned widget is an object and you can use it to get/set values or do something. These are all detailed in this section.

  2. Set the attributes of the widget. This can be done on widget creation.

  3. Pack the widget into a container using the appropriate call such as add or pack method. This can be done on widget creation.

  4. Connect all signals and events we wish to use to the appropriate handlers.

  5. If it is toplevel window, show() the widget. Except toplevel window, show is default.

show method lets GTK know that we are done setting the attributes of the widget, and it is ready to be displayed. You may also use hide metehod to make it disappear again. The order in which you show the widgets is not important, but I suggest showing the window last so the whole window pops up at once rather than seeing the individual widgets come up on the screen as they're formed. The children of a widget (a window is a widget too) will not be displayed until the window itself is shown using the show() method.

6.1. Type Conversion

You'll notice as you go on that you need a type conversion. What you will see are:

method as_widget : Gtk.widget Gtk.obj
method coerce : widget

These are all used to cast arguments in functions. You'll see them in the examples, and can usually tell when to use them simply by looking at the function's declaration.

For example:

box#pack button#corece;

This casts the button into a widget.