Gtk3 Study Notes 2 Hello world in GTK +

Source: Internet
Author: User
Tags gtk signal handler

The previous section describes how to create a codeblocks development environment for gtk3 on Windows. This section describes Example 1. Hello world in GTK +.

The example is very simple. It implements a window and a common button, and the effect is


Source code:

# Include <GTK/GTK. h>/* the following are two callback functions */static void print_hello (gtkwidget * widget, gpointer data) {g_print ("Hello world \ n ");} static gboolean on_delete_event (gtkwidget * widget, gdkevent * event, gpointer data) {/* If you return false in the "delete_event" signal handler, * GTK will emit the "Destroy" signal. returning true means * You don't want the window to be destroyed. ** this is useful for poppin G up 'Are you sure you want to quit? '* Type dialogs. */g_print ("delete event occurred \ n"); Return true;} int main (INT argc, char * argv []) {/* gtkwidget is the storage type for widgets */gtkwidget * window; gtkwidget * button;/* This is called in all GTK applications. arguments are parsed * from the command line and are returned to the application. */gtk_init (& argc, & argv);/* Create a new window, and set its title */window = gtk_window_new (gtk_window_toplevel); gtk_window_set_title (gtk_window (window ), "Hello");/* when the window emits the "delete-Event" signal (which is emitted * by GTK + in response to an event coming from the window manager, * usually as a result of clicking the "close" window control), we * ask it to call the on_delete_event () function as defined above. ** the data passed to the callback function is null and is ignored * in the callback function. */g_signal_connect (window, "delete-Event", g_callback (on_delete_event), null);/* Here we connect the "Destroy" event to the gtk_main_quit () function. ** this signal is emitted when we call gtk_widget_destroy () on the window, * or if we return false in the "delete_event" callback. */g_signal_connect (window, "Destroy", g_callback (gtk_main_quit), null);/* sets the Border width of the window. */gtk_container_set_border_width (gtk_container (window), 10);/* creates a new button with the label "Hello World ". */button = gtk_button_new_with_label ("Hello World");/* When the button starts es the "clicked" signal, it will call the * function print_hello () passing it null as its argument. ** the print_hello () function is defined above. */g_signal_connect (button, "clicked", g_callback (print_hello), null);/* The g_signal_connect_swapped () function will connect the "clicked" signal * of the button to the gtk_widget_destroy () function; instead of calling it * using the button as its argument, it will swap it with the user data * argument. this will cause the window to be destroyed by calling * gtk_widget_destroy () on the window. */g_signal_connect_swapped (button, "clicked", g_callback (gtk_widget_destroy), window);/* This packs the button into the window. A gtkwindow inherits from gtkbin, * which is a special container that can only have one child */gtk_container_add (gtk_container (window), button ); /* the final step is to display this newly created widget... */gtk_widget_show (button );/*... and the window */gtk_widget_show (window);/* All GTK applications must have a gtk_main (). control ends here * and waits for an event to occur (like a key press or a mouse event), * Until gtk_main_quit () is called. */gtk_main (); Return 0 ;}

Starting from the next blog, the length of the article will increase, and some functions will be described in Chinese in relative detail.

In fact, many of the basic functions of gtk3 and gtk2 have not changed. You can refer to the gtk2 tutorial in Chinese to read the gtk3 tutorial on the official website. My blog is just about it. I will write this article here.


Gtk3 Study Notes 2 Hello world in GTK +

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.