gtk3 Tutorial => Getting started with gtk3 (2024)

Remarks

GTK+ 3 also known as Gtk3 is a multi-platform GUI toolkit, it is written in C but has bindings for a lot of languages including C++, Python, Vala and Ruby. (For the full list see the Gtk website).

Gtk+ is part of the GNU Project and falls under the GNU LGPL licences meaning it is allowed to be used by all developers, including those developing proprietary software, without any license fees or royalties.

Credits: loosely based on http://www.gtk.org/

Versions

[C] "Hello World" in Gtk+

#include <gtk/gtk.h>// callback function which is called when button is clickedstatic void on_button_clicked(GtkButton *btn, gpointer data) { // change button label when it's clicked gtk_button_set_label(btn, "Hello World");}// callback function which is called when application is first startedstatic void on_app_activate(GApplication *app, gpointer data) { // create a new application window for the application // GtkApplication is sub-class of GApplication // downcast GApplication* to GtkApplication* with GTK_APPLICATION() macro GtkWidget *window = gtk_application_window_new(GTK_APPLICATION(app)); // a simple push button GtkWidget *btn = gtk_button_new_with_label("Click Me!"); // connect the event-handler for "clicked" signal of button g_signal_connect(btn, "clicked", G_CALLBACK(on_button_clicked), NULL); // add the button to the window gtk_container_add(GTK_CONTAINER(window), btn); // display the window gtk_widget_show_all(GTK_WIDGET(window));}int main(int argc, char *argv[]) { // create new GtkApplication with an unique application ID GtkApplication *app = gtk_application_new( "org.gtkmm.example.HelloApp", G_APPLICATION_FLAGS_NONE ); // connect the event-handler for "activate" signal of GApplication // G_CALLBACK() macro is used to cast the callback function pointer // to generic void pointer g_signal_connect(app, "activate", G_CALLBACK(on_app_activate), NULL); // start the application, terminate by closing the window // GtkApplication* is upcast to GApplication* with G_APPLICATION() macro int status = g_application_run(G_APPLICATION(app), argc, argv); // deallocate the application object g_object_unref(app); return status;} 

[C++] "Hello World" in gtkmm

#include <gtkmm/application.h>#include <gtkmm/applicationwindow.h>#include <gtkmm/button.h>// main window of the applicationclass HelloWorldWindow : public Gtk::ApplicationWindow { // a simple push button Gtk::Button btn;public: HelloWorldWindow() : btn("Click me!") {// initialize button with a text label // when user presses the button "clicked" signal is emitted // connect an event handler for the signal with connect() // which accepts lambda expression, among other things btn.signal_clicked().connect( [this]() { btn.set_label("Hello World"); }); // add the push button to the window add(btn); // make the window visible show_all(); }};int main(int argc, char *argv[]) { // This creates an Gtk+ application with an unique application ID auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example.HelloApp"); HelloWorldWindow hw; // this starts the application with our window // close the window to terminate the application return app->run(hw);} 

Installation or Setup

Python

Windows

The easiest way to install GTK3 for Python is by using PyGObject for Windows. It offers an installer that installs most things you need to develop GTK appilcations.

The number of options the PyGObject installer offers can be daunting, but for most GTK projects the only option you have to select is GTK+ 3.xx .

C++

The C++ binding for Gtk+ is known as gtkmm.

Windows

On Microsoft Windows gtkmm can be installed through MSYS2 environment. Once MSYS2 environment is set up by installing the installer and updating the package list, install gtkmm with

pacman -S mingw-w64-x86_64-gtkmm3 #64 bitpacman -S mingw-w64-i686-gtkmm3 #32 bit 

Install pkg-config for easily obtaining compiler and linker flags and GNU autotools build integration

pacman -S pkg-config 

Now gtkmm application can be compiled, linked and run from within MSYS2 environment.

# enable C++ 14 support if needed# -mwindows flag is to suppress the background command-prompt window # for GUI applicationsg++ -mwindows -std=c++14 -o app.exe app.cpp `pkg-config --cflags --libs gtkmm-3.0`./app.exe 

But the executable won't run outside the MSYS2 shell because of missing standard environment variables for .dll lookup. The following .dlls need to be copied from <MSYS2 INSTALLATION DIRECTORY>\mingw64\lib\ (for 64-bit installation) into the application directory (where the .exe is located) manually. The version numbers may change according to the installation.

libatk-1.0-0.dlllibatkmm-1.6-1.dlllibbz2-1.dlllibcairo-2.dlllibcairo-gobject-2.dlllibcairomm-1.0-1.dlllibepoxy-0.dlllibexpat-1.dlllibffi-6.dlllibfontconfig-1.dlllibfreetype-6.dlllibgcc_s_seh-1.dlllibgdk_pixbuf-2.0-0.dlllibgdk-3-0.dlllibgdkmm-3.0-1.dlllibgio-2.0-0.dlllibgiomm-2.4-1.dlllibglib-2.0-0.dlllibglibmm-2.4-1.dlllibgmodule-2.0-0.dlllibgobject-2.0-0.dlllibgtk-3-0.dlllibgtkmm-3.0-1.dlllibharfbuzz-0.dlllibiconv-2.dlllibintl-8.dlllibpango-1.0-0.dlllibpangocairo-1.0-0.dlllibpangoft2-1.0-0.dlllibpangomm-1.4-1.dlllibpangowin32-1.0-0.dlllibpixman-1-0.dlllibpng16-16.dlllibsigc-2.0-0.dlllibstdc++-6.dlllibwinpthread-1.dllzlib1.dll 

After this step the program should run. But it won't find standard icon sets for Gtk+, i.e. the Adwaita icon theme, so icons may not load. The icons and a few other files need to be copied into application directory so that the application can load them.

From <MSYS2 INSTALL DIRECTORY>

mingw64 | +-- lib | +-- gdk-pixbuf-2.0share | +-- icons | +-- Adwaita | +-- hicolor (fallback icon theme for Gtk+) 

To application directory, with same directory structure.

starter kit

#include <gtk/gtk.h>static void destroy(GtkWidget *widget, gpointer data){gtk_main_quit();}int main(int argc, char *argv[]){gtk_init(&argc, &argv);GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);gtk_window_set_title(GTK_WINDOW(window), "Window");g_signal_connect(window, "destroy", G_CALLBACK(destroy), NULL);GtkWidget *k;k= gtk_fixed_new(); gtk_container_add(GTK_CONTAINER(window), k); GtkWidget* la,*r; la = gtk_button_new_with_label (",mkl"); gtk_fixed_put (GTK_FIXED (k), la,50,237); gtk_widget_set_size_request(la, 98, 90);// gtk_container_set_border_width(GTK_CONTAINER (la) , 5); r = gtk_button_new_with_label (",kii"); gtk_fixed_put (GTK_FIXED (k), r,150,237); gtk_widget_set_size_request(r, 98, 90); gtk_widget_set_size_request(GTK_WIDGET(window),300,349);gtk_widget_show_all(GTK_WIDGET(window));gtk_main();return 0;} 

compile:

c++ starterkit.c `pkg-config --libs --cflags gtk+-3.0` -o p 

and

./p 

gtk3 Tutorial => Getting started with gtk3 (1) PDF - Download gtk3 for free



Previous Next

gtk3 Tutorial => Getting started with gtk3 (2024)

References

Top Articles
Latest Posts
Article information

Author: Gregorio Kreiger

Last Updated:

Views: 6439

Rating: 4.7 / 5 (77 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Gregorio Kreiger

Birthday: 1994-12-18

Address: 89212 Tracey Ramp, Sunside, MT 08453-0951

Phone: +9014805370218

Job: Customer Designer

Hobby: Mountain biking, Orienteering, Hiking, Sewing, Backpacking, Mushroom hunting, Backpacking

Introduction: My name is Gregorio Kreiger, I am a tender, brainy, enthusiastic, combative, agreeable, gentle, gentle person who loves writing and wants to share my knowledge and understanding with you.