/GTK 4.0


Initialization — Library initialization and main loop

Types and Values


#include <gtk/gtk.h>


Before using GTK, you need to initialize it using gtk_init(); this connects to the windowing system, sets up the locale and performs other initialization tasks. gtk_init() exits the application if errors occur; to avoid this, you can use gtk_init_check(), which allows you to recover from a failed GTK initialization - you might start up your application in text mode instead.

Like all GUI toolkits, GTK uses an event-driven programming model. When the user is doing nothing, GTK sits in the “main loop” and waits for input. If the user performs some action - say, a mouse click - then the main loop “wakes up” and delivers an event to GTK. GTK forwards the event to one or more widgets.

When widgets receive an event, they frequently emit one or more “signals”. Signals notify your program that "something interesting happened" by invoking functions you’ve connected to the signal with g_signal_connect(). Functions connected to a signal are often called “callbacks”.

When your callbacks are invoked, you would typically take some action - for example, when an Open button is clicked you might display a GtkFileChooserDialog. After a callback finishes, GTK will return to the main loop and await more user input.

Typical main() function for a GTK application

See GMainLoop in the GLib documentation to learn more about main loops and their features.


gtk_disable_setlocale ()

gtk_disable_setlocale (void);

Prevents gtk_init(), gtk_init_check() and gtk_parse_args() from automatically calling setlocale (LC_ALL, ""). You would want to use this function if you wanted to set the locale for your program to something other than the user’s locale, or if you wanted to set different values for different locale categories.

Most programs should not need to call this function.

gtk_get_default_language ()

PangoLanguage *
gtk_get_default_language (void);

Returns the PangoLanguage for the default language currently in effect. (Note that this can change over the life of an application.) The default language is derived from the current locale. It determines, for example, whether GTK uses the right-to-left or left-to-right text direction.

This function is equivalent to pango_language_get_default(). See that function for details.


the default language as a PangoLanguage, must not be freed.

[transfer none]

gtk_get_locale_direction ()

gtk_get_locale_direction (void);

Get the direction of the current locale. This is the expected reading direction for text and UI.

This function depends on the current locale being set with setlocale() and will default to setting the GTK_TEXT_DIR_LTR direction otherwise. GTK_TEXT_DIR_NONE will never be returned.

GTK sets the default text direction according to the locale during gtk_init(), and you should normally use gtk_widget_get_direction() or gtk_widget_get_default_direction() to obtain the current direction.

This function is only needed rare cases when the locale is changed after GTK has already been initialized. In this case, you can use it to update the default text direction as follows:

main (int argc, char **argv)
 GtkWidget *window;
  // Initialize i18n support with bindtextdomain(), etc.

  // ...

  // Initialize the widget set
  gtk_init ();

  // Create the main window
  window = gtk_window_new ();

  // Set up our GUI elements

  // ...

  // Show the application window
  gtk_widget_show (window);

  // Enter the main event loop, and wait for user interaction
  while (!done)
    g_main_context_iteration (NULL, TRUE);

  // The user lost interest
  return 0;


the GtkTextDirection of the current locale

gtk_init ()

gtk_init (void);

Call this function before using any other GTK functions in your GUI applications. It will initialize everything needed to operate the toolkit and parses some standard command line options.

If you are using GtkApplication, you don't have to call gtk_init() or gtk_init_check(); the “startup” handler does it for you.

This function will terminate your program if it was unable to initialize the windowing system for some reason. If you want your program to fall back to a textual interface you want to call gtk_init_check() instead.

GTK calls signal (SIGPIPE, SIG_IGN) during initialization, to ignore SIGPIPE signals, since these are almost never wanted in graphical applications. If you do need to handle SIGPIPE for some reason, reset the handler after gtk_init(), but notice that other libraries (e.g. libdbus or gvfs) might do similar things.

gtk_init_check ()

gtk_init_check (void);

This function does the same work as gtk_init() with only a single change: It does not terminate the program if the windowing system can’t be initialized. Instead it returns FALSE on failure.

This way the application can fall back to some other means of communication with the user - for example a curses or command line interface.


TRUE if the windowing system has been successfully initialized, FALSE otherwise

gtk_is_initialized ()

gtk_is_initialized (void);

Use this function to check if GTK has been initialized with gtk_init() or gtk_init_check().


the initialization status

gtk_get_debug_flags ()

gtk_get_debug_flags (void);

Returns the GTK debug flags that are currently active.

This function is intended for GTK modules that want to adjust their debug output based on GTK debug flags.


the GTK debug flags.

gtk_set_debug_flags ()

gtk_set_debug_flags (GtkDebugFlags flags);

Sets the GTK debug flags.



the debug flags to set

Types and Values



Use this priority for functionality related to size allocation.

It is used internally by GTK+ to compute the sizes of widgets. This priority is higher than GDK_PRIORITY_REDRAW to avoid resizing a widget which was just redrawn.

enum GtkDebugFlags




















See Also

See the GLib manual, especially GMainLoop and signal-related functions such as g_signal_connect()

© 2005–2020 The GNOME Project
Licensed under the GNU Lesser General Public License version 2.1 or later.