Friday, December 6, 2013

Hello GTK+ 2.0

Modified Hello GTK+
Last post show how to Install gtk+ on Raspberry Pi and with a very simple "Hello World" to GTK+. It's modified version of the Hello World:

  • To terminal terminate the application when the GtkWindow is destroyed, connect "destroy" to gtk_main_quit callback using g_signal_connect()
  • Add a GtkWidget of button to print "Hello GTK+\n" on screen by calling g_print().
Example code:
#include <gtk/gtk.h>

static void hello(GtkWidget *widget, gpointer   data)
{
 g_print("Hello GTK+\n");
}

int main(int argc, char *argv[])
{
 GtkWidget *window;
 GtkWidget *buttonHello;
 
 gtk_init(&argc, &argv);
 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
 
 buttonHello = gtk_button_new_with_label("Hello GTK+");
 g_signal_connect(buttonHello, "clicked", 
  G_CALLBACK(hello), NULL);
 
 //terminate the application when the GtkWindow is destroyed
 g_signal_connect (window, "destroy", 
  G_CALLBACK(gtk_main_quit), NULL);
  
 gtk_container_add(GTK_CONTAINER (window), buttonHello);
 gtk_widget_show(buttonHello);
 gtk_widget_show(window);

 gtk_main();

 return(0);
}


Compile and run the program as describe in last post.

No comments: