Gtk Examples


Here are a few examples which you would like to look at when you start learning about the GTK widgets. These are the list of examples which will guide you in your learning. This page is still being expanded, so check back for more examples.

GTK is essentially an object oriented application programmers interface (API). Although written completely in C,it is implemented using the idea of classes and callback functions (pointers to functions).

PyGTK is a set of bindings for the GTK widget set. It provides an object oriented interface that is slightly higher level than the C one. It automatically does all the type casting and reference counting that you would have to do normally with the C API.



Base:
Let us begin with an example that creates a window and has no way of exiting except to be killed by using the shell.
Base

Button: Let us now look at an example which packs a button in a window. There is still no way of exiting except to be killed by using the shell.
Button
There is no functionality attached to the above example.To add functionality to the widgets we use signals.

To know about signals see the link . ABOUT GTK SIGNALS

Adding Functionality to Button:
Let us now use the signals concept to add functionality to our widgets. we will look at the same Button example but will add signals to it.
Button

Button Widget:
Now, Let us have a look at the Button Widget.
Buttons are of 4 types
Normal Buttons:

Normal Buttons
The above helloworld example illustrates a Normal Button

Toggle Buttons:

Toggle Buttons
The following example illustates the toggle button.
ToggleButton


Check Buttons:

Check Buttons
The following example illustrates a check button.
CheckButton


Till now, our examples used only one widget so we could simply use the GTKContainer add() method to "pack" the widget into the window. When creating an application, we would want to put more than one widget inside a window. But when we want to put more than one widget into a window, how do we control where that widget is positioned? This is where packing comes in.
PACKING WIDGETS
The following example shows how to pack the button widgets into the window using a vertical box.
Packing Buttons



In the above example we pack 3 different buttons into a vertical box from top to bottom.
Let's now look at an example where we pack boxes into boxes to get the desired look. In this example we pack a horizontal box into the window(GTK container) and pack two vertical boxes into the horizontal box from left to right. In the vertical box towards the left we pack three different buttons and in the other vertical box, we pack radiobuttons.

Radio Buttons:

Radio Buttons
An,example to illustrate radio buttons is Radiobutton illustration



Packing Example:
Example dealing with packing of boxes. Packing boxes



Text Entry:
Let us now have a look at the TextEntrywidget.
The following example illustrates the TextEntry widget TextEntry Example



Combo Box:
The Combo Box is a simple widget which is a collection of other widgets. The widgets consists of a text entry box and pull down menu from which the user can select one of a set of predefined entries.
About Combo Box

An example of combobox is shown ComboBox



Dialog Widget:
Dialog widget is actually just a window with a few things pre-packed into it. It simply creates a window, and then packs a VBox into the top, which contains a separator and then an HBox called the "action_area". It is used for pop-up messages to the user.
Dialogs

Scrolled Windows:
Scrolled windows are used to create a scrollable area with another widget inside it. You may insert any type of widget into a scrolled window, and it will be accessible regardless of the size by using the scrollbars.
Scrolled Window

Scrolled Window Exampleis an example which illustrates the scrolled window and dialog. A table with a grid of togglebuttons is packed into the scrolled window.



Text View:
TextView widgets and their associated objects provide a powerful framework for multiline text editing. A textbuffer contains the text which is displayed by one or more textview widgets.
TextView

An example of a text view widget that uses scrollbar widget, radiobuttons, checkbuttons, togglebuttons, separators and various textview settings such as editable text, cursor visible, wrapmode, justification is shown below.
Text View Example



File Selection:
The file selection widget is a quick way to display a File dialog box. It comes complete with Ok, Cancel and Help buttons.
File Selection

File Selection example


GTK has various widgets that can be visually adjusted by the user using the mouse or the keyboard, such as range widgets which include scrollbar widgets and scale widgets.

GTK uses Adjustment object, which is not a widget but a way for widgets to store and pass adjustment information in an abstract and flexible form. Adjustments ae used to store the configuration parameters and values of range widgets, such as scrollbars and scale controls.

Adjustment objects can emit signals , just like widgets and these signals can be used to allow the program to react to user input on adjustable widgets and also to propagate adjustment values transparently between adjustable widgets.
Many of widgets create adjustments automatically , but in some cases we may have to create one.
Creating Adjustments

Before we look into an example using adjustments let us have a look at frames which are very useful when creating applications.

Frames:Frames can be used to enclose one or a group of widgets with a box which can optionally be labelled, the position and style of the label can be altered to suit our interest.Using Frames

Now, making use of adjustment objects and frames let us look at how spinbutton is set up.

Spin Button:

The SpinButton widget is generally used to allow user to select a value from the range of numeric values. It consists of a text entry box with up and down arrow buttons attached to the side.selecting one of the button causes the value to "spin" up and down the range of possible values. The entry box may also be edited to enter a specific value.

The SpinButton uses an Adjustment object to hold information about the range of values that the spin button can take. This makes for a powerful SpinButton widget.
More about SpinButton

An example of spinbutton Spinbutton illustration


Menu Widget:

The GtkMenuBar, GtkMenu, and GtkMenuItem widgets are used in conjunction with each other to provide drop-down menu lists.
The GtkMenuBar is essentially a horizontal packing box for the menu items. Individual GtkMenu widgets such as File and Edit are then packed inside the menu bar. Then, the GtkMenuItem widgets are packed inside a GtkMenu widget.

For example,the GtkMenu widget File contains three GtkMenuItem widgets, New, Open, and Exit.
The GtkWindow widget itself contains a GtkVBox. The GtkMenuBar is added to the uppermost box of the GtkVBox, and the GtkText widget is added below that.

More about menubar and Menu bar creation methods Menu Widget
Menu bar example Menubar


Menubar with a text window Menubar with text window


NoteBook:
The NoteBook Widget is a collection of "pages" that overlap each other; each page contains different information with only one page visible at a time. This widget has become more common lately in GUI programming, and it is a good way to show blocks of similar information that warrant separation in their display.
More about Note Book

Notebook example Notebook example


CList Widget:
The CList widget has replaced the List widget.
The CList widget is a multi-column list widget that is capable of handling literally thousands of rows of information. Each column can optionally have a title, which itself is optionally active, allowing us to bind a callback to its selection.
About CList

example of clists: Clist1

another exampleClist2