Understanding GTK

D

DevynCJohnson

Guest
The GIMP ToolKit (GTK) is a widget-toolkit used to create GUIs on a variety of systems (thus making GTK cross-platform). GTK is commonly and incorrectly thought to stand for "GNOME ToolKit", but is actually stands for "GIMP ToolKit" because it was first created to design an user interface for GIMP. GTK is an object-oriented toolkit written in C (GTK itself is not a language). GTK is entirely open-source under the LGPL license. GTK is a widely used toolkit for GUIs and many tools are available for GTK. (http://www.gtk.org/)

Many of you may have wondered about the difference between "GTK" and "GTK+". GTK was released first, but was not object-oriented. Later, the GTK developers rewrote the code to make GTK object-oriented. After that, they started calling it "GTK+". However, people still use "GTK" and "GTK+" interchangeably as you can see in this very article. GTK2 and GTK3 refer to GTK version 2 and 3, respectively. "GTK2", "GTK+ 2", and "GTK2+" are all the same is true for GTK3 written in those different styles.

GObject is often associated with GTK, but what is it and why? GObject (GLib Object System) is a library that allows transparent compatibility between computer languages. In other words, GObject acts as glue code. GObject is part of the GLib library.

Many bindings are available for GTK. GTK Minus Minus (gtkmm) is the C++ binding for GTK (http://www.gtkmm.org/en/). GTK Sharp (GTK#) is the .NET binding (http://www.mono-project.com/GtkSharp). Two Python bindings exist - PyGTK and PyGObject (PyGI). PyGTK only supports GTK2 while PyGObject supports GTK2 and GTK3. PyGTK will be deprecated as applications migrate from GTK2 to GTK3. PyGObject is available to Python2 and Python3 as "python-gi" and "python3-gi", respectively, while PyGTK is only available for Python2.

PyGObject Downloads: https://wiki.gnome.org/Projects/PyGObject

The Glade Interface Designer (or just "Glade") is a WYSIWYG editor for GTK3 (older versions support GTK2). Glade allows a user to design an interface (GUI) and export it as an XML file. This XML file is then used to generate source code in the desired language. Glade v3.8.x is the newest version that supports GTK2. From Glade v3.10 and above, only GTK3 is supported. Developers can have both Glade version 3.8.x and 3.10+ installed simultaneously on their system without conflicts. (https://glade.gnome.org/)

glade_ide.png


NOTE: By the way, "wxGlade" is not related to GTK or Glade.

The XML format of the exported Glade file is called "GtkBuilder". This is the new format that succeeded the former format called "GladeXML". GladeXML is also called "libglade". GtkBuilder supports both GTK v2 and v3. GladeXML uses the "*.glade" file extension while GtkBuilder uses the ".xml" file extension. Developers can convert from GladeXML to GtkBuilder by using the gtk-builder-convert utility (https://developer.gnome.org/gtk2/stable/gtk-builder-convert.html).

"Code Sketching" is the process of reading the XML file made by Glade and generating source code. Code Sketchers are programs that perform code sketching. Gladex is a code sketcher that reads GladeXML files and writes Python, Perl, and Ruby code for GTK2. GLC is like Gladex, but it only supports Python. Many other code sketchers exists, but they are for GTK2 using the GladeXML format. To make GTK3 code and use the GtkBuilder format, use a special command using your language binding to generate the code. For example, in Python3 (using PyGObject), I can import GTK and use the "Gtk.Builder()" command to generate my GUI. For those of you using different bindings, view the documentation and tutorials for your GTK binding to learn how to generate a GUI from your GtkBuilder file. (http://python-gtk-3-tutorial.readthedocs.org/en/latest/builder.html)

Python3 Example for using a GtkBuild file -
Code:
from gi.repository import Gtk
builder = Gtk.Builder()
builder.add_from_file("example.glade")
window = builder.get_object("window1")
window.show_all()


Sample GtkBuilder Glade file -
Code:
<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkWindow" id="window1">
  <property name="can_focus">False</property>
  <signal name="delete-event" handler="onDeleteWindow" swapped="no"/>
  <child>
  <object class="GtkButton" id="button1">
  <property name="label" translatable="yes">button</property>
  <property name="use_action_appearance">False</property>
  <property name="visible">True</property>
  <property name="can_focus">True</property>
  <property name="receives_default">True</property>
  <property name="use_action_appearance">False</property>
  <signal name="pressed" handler="onButtonPressed" swapped="no"/>
  </object>
  </child>
  </object>
</interface>

As we can see, GTK is a useful and simplistic widget toolkit that allows developers to design easy-to-use GUIs for their applications.

Further Reading
 

Attachments

  • slide.JPG
    slide.JPG
    73.1 KB · Views: 65,145
Last edited:

Members online


Top