Open In App

How to create GUI in C programming using GTK Toolkit

Introduction to GTK

Many programming languages bolster GUI improvement as one of the centrepieces of its language highlights. C has no such library connected to it like the string library, IO library, etc, that we every now and again use. This weakness opened the skyline for engineers to pick from a wide assortment of GUI library toolbox accessible in C. GTK+ is one of them. It represents GIMP (GNU Image Manipulation Program) Toolkit and can be utilized to program current GUI interfaces.

The beneficial thing about GTK+ is that it is steady, developed, and its starting point can be followed back to the past times of X Windows that structure the centre GUI arrangement of Linux today. GTK is completely written in C and the GTK+ programming that we regularly use in Linux is additionally written in C. The work area administrators, for example, GNOME and XFCE, likewise are manufactured utilizing GTK.



A GTK+ application isn’t limited to the Linux stage no one but; it very well may be ported to non-UNIX/Linux stages also.

Here, we will cling to the fundamental type of GTK+, which is its C avatar on the Linux stage. The official webpage to download GTK+ is https://www.gtk.org. The site contains API documentation, instructional exercises, and other Gnome libraries that are frequently utilized alongside GTK. Truth be told, GTK is based over libraries, for example,



When composing code with GTK, we regularly locate that a significant number of the primitive data types are prefixed with ‘g‘ as in

These data types guarantee that the code can be recompiled on any platform without rolling out any improvements. These data types are characterized in these libraries to help in making it platform-independent.

GUI programming inherent object-oriented in it which is the main issue. In this, a procedural worldview doesn’t fit consummately in this scheme. Thus, regardless of GTK being written in C, it gives object-oriented help through GObject. Note that this item arranged help has nothing to do with C++. C++ has its own GTK library, called gtkmm. GObject encourages a portion of the object-oriented principles, similar to polymorphism and inheritance with the assistance of macros. The following diagram illustrates the hierarchical relation.

GtkWindow inherits GtkBin, which itself is a child of GtkContainer; in this manner, an object of GtkWindow can call the function defined in GtkBin or GtkContainer. This is an example of object-oriented implementation in C by GTK.

GUI With GTK

Let us comprehend a couple of things from our first GTK code in C.

Below is the implementation of the above steps:




#include <gtk/gtk.h>
  
static int counter = 0;
  
void greet(GtkWidget* widget, gpointer data)
{
    // printf equivalent in GTK+
    g_print("Welcome to GTK\n");
    g_print("%s clicked %d times\n",
            (char*)data, ++counter);
}
  
void destroy(GtkWidget* widget, gpointer data)
{
    gtk_main_quit();
}
  
int main(int argc, char* argv[])
{
  
    GtkWidget* window;
    GtkWidget* button;
    gtk_init(&argc, &argv);
  
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  
    g_signal_connect(window, "destroy",
                     G_CALLBACK(destroy), NULL);
    /* Let's set the border width of the window to 20.
    * You may play with the value and see the
    * difference. */
    gtk_container_set_border_width(GTK_CONTAINER(window), 20);
  
    button = gtk_button_new_with_label("Click Me!");
  
    g_signal_connect(GTK_OBJECT(button),
                     "clicked", G_CALLBACK(greet),
                     "button");
  
    gtk_container_add(GTK_CONTAINER(window), button);
  
    gtk_widget_show_all(window);
  
    gtk_main();
  
    return 0;
}

Conclusion
GTK+ has all the GUI segments one needs to make an expert looking interface. The essential thought of GUI occasion driven programming with GTK+ isn’t very different from the one appeared in the model. Include a couple of more parts, utilize various sorts of compartments, play with designs, and obviously always remember to counsel the GTK+ documentation. Proficient developers frequently utilized RAD tools, for example, Glade to plan the GUI interface rapidly. Be that as it may, in the first place, have a go at composing the code without any preparation to get a vibe of what goes where and how it is really done. It will compensate you later.


Article Tags :