This article is intended to outline some of the basic concepts used in GTK.

Widgets

Buttons, labels, scrollbars and even windows are known as widgets in GTK. Every object you can put into a window is a widget.

Widgets have the following things associated with them:

  • Properties - The properties of a widget, such as what size it is, how many characters it will accept and so on. Properties can be read only or read/write.

  • Style Properties - Properties related to how the widget looks, such as its colour and border style.

  • Signals - Signals which are emitted when an event occurs involving the widget (e.g. clicking the widget). You can use signals to run functions when such an event occurs.

  • Methods - Functions which can be called to manipulate the widget and pass or receive information from it. For example, you might call the get_text() method to get the text currently contained by the widget.

Containers

Certain widgets are able to contain other widgets. For example, a Window widget can contain one widget, which can itself contain several more widgets, all of which may be able to contain widgets too. You can think of the widgets in a GTK project as being organised as a tree.

Containers are useful for arranging widgets in a window. If the parent widget of other widgets is hidden, then those widgets will be hidden too.

Below is a list of useful containers:

  • Window - This is a normal window, the kind that you see everywhere on your desktop. Windows can only directly contain one widget themselves, so you'll normally want to put another type of container in your window first.

  • VBox - A container with a column of invisible 'slots' which widgets can be put into. Useful for organising things vertically.

  • HBox - Similar to VBox, but horizontal instead of vertical.

Events and Signals

Widgets can be made to perform functions when an event occurs. An example of an event is a button being clicked. When the event occurs, the widget sends out a signal, in this case the clicked signal. You can find a list of valid signals in the GTK documentation.

These signals can be received by signal handling functions which you have defined. You can connect functions in your code to certain signals, so that when the signal is received your function is run. For example, when the button is clicked, we might want to run a function which pops up a message.


Categories: CategoryPythonRecipes Parent: (unknown) | Discuss this page

PythonRecipes/GTKBasics (last edited 2011-04-09 03:56:44 by k.dejong)