GTK+ By Example/GTK+/Hello World

This small tutorial demonstrates creating a simple window with a single button. When the button is clicked, the contents of that label on the button is printed to stdout.

The label is packed into the button, which is in turn packed into the window.

Firstly, you must include the gtk header file.

 #include<gtk/gtk.h>

Next, two callback functions prototypes are defined, one for closing the window, and one for clicking on the button. Callback functions are basically functions that are called by lower-level code (in this case, GTK itself) by being passed the function address.

 static gint delete_callback(GtkWidget* w, GdkEventAny* e, gpointer data);
 static void button_callback(GtkWidget* w ,gpointer data);

Full source

edit
 #include<gtk/gtk.h>
 
 static gint delete_callback(GtkWidget* w, GdkEventAny* e, gpointer data);
 static void button_callback(GtkWidget* w ,gpointer data); 
 
 int main(int argc, char* argv[])
 {
   GtkWidget* window;
   GtkWidget* button;
   GtkWidget* label;
   
   gtk_init(&argc, &argv);
   
   window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   button = gtk_button_new();
   label = gtk_label_new("Hello World");
   
   gtk_container_add(GTK_CONTAINER(button),label);
   gtk_container_add(GTK_CONTAINER(window), button);
   
   gtk_window_set_title(GTK_WINDOW(window), " Hello World");
   gtk_container_set_border_width(GTK_CONTAINER(button),10);
   gtk_window_set_default_size(GTK_WINDOW(window),400,400);         
   
   gtk_signal_connect(GTK_OBJECT(window),
 		     "delete_event",
 		     GTK_SIGNAL_FUNC(delete_callback),
 		     NULL);
   
   gtk_signal_connect(GTK_OBJECT(button),
 		     "clicked",
 		     GTK_SIGNAL_FUNC(button_callback),
 		     label);
   
   gtk_widget_show_all(window);
   gtk_main();
   
   return 0;
   
 }
 
 static gint delete_callback(GtkWidget* w, GdkEventAny* e, gpointer data)
 {
   gtk_main_quit();
 }
 static void button_callback(GtkWidget* w ,gpointer data)
 {
   GtkWidget* label;
   gchar* text;
   
   label = GTK_WIDGET(data);
   gtk_label_get(GTK_LABEL(label), &text);
   
   printf("%s \n", text);
   
 }

In Gtk2-Perl

edit
#!/usr/bin/perl

use Gtk2 "-init";
use warnings;
use strict;

my $window = new Gtk2::Window;
my $button = new Gtk2::Button('Reset');
$window->add($button);

$window->set_title("Hello, world!");
$button->set_border_width(10);
$window->set_default_size(400,400);

$window->signal_connect("delete_event", \&delete_callback);
$button->signal_connect("clicked", \&button_callback);
$window->show_all;
Gtk2->main;

sub delete_callback{
    Gtk2->main_quit;
}
sub button_callback{
    my $label = $button->get_child;
    $label->set_text ("Support your local bakery");
    $window->show_all;
}

In Gtk2Hs Haskell

edit
module Main where

import Graphics.UI.Gtk

main :: IO ()
main = do
  initGUI
  window  windowNew
  button  buttonNewWithLabel "Reset"
  set window [windowTitle := "Hello",
              windowDefaultWidth := 400,
              windowDefaultHeight := 400,
              containerChild := button]
  set button [containerBorderWidth := 10]
  onClicked button $ buttonSetLabel button "Support your local pâtisserie"
  onDestroy window mainQuit
  widgetShowAll window
  mainGUI