Linux from the beginning to go GTK +-3.0 (5), gtk-3.0
In practice, it is very cumbersome to use the gtk function to create the UI. You need to write a lot of code. How can we quickly establish a unified UI layout?
Fortunately, GTK provides a GtkBuilder for quick interface creation. It reads an xml file. Create an interface as described in the xml file. Therefore, we need to write xml files.
Create example. ui with the following content:
<?xml version="1.0" encoding="UTF-8"?><interface> <object id="window" class="GtkWindow"> <property name="visible">True</property> <property name="title">GtkBuilder</property> <property name="border-width">30</property> <child> <object id="grid" class="GtkGrid"> <property name="visible">True</property> <child> <object id="print_hello" class="GtkButton"> <property name="visible">True</property> <property name="label">Button 1</property> </object> <packing> <property name="left-attach">0</property> <property name="top-attach">0</property> </packing> </child> <child> <object id="print_entry" class="GtkButton"> <property name="visible">True</property> <property name="label">Button 2</property> </object> <packing> <property name="left-attach">1</property> <property name="top-attach">0</property> </packing> </child> <child> <object id="entry" class="GtkEntry"> <property name="visible">True</property> </object> <packing> <property name="left-attach">0</property> <property name="top-attach">1</property> <property name="width">2</property> </packing> </child> </object> <packing> </packing> </child> </object></interface>
Create the source file example. c. The content is as follows:
# Include <gtk/gtk. h> static void print_hello (GtkWidget * button, gpointer data); static void activate (GtkApplication * app, gpointer data); static void print_entry (GtkWidget * button, gpointer entry ); int main (int argc, char ** argv) {GtkApplication * app; int app_status; app = gtk_application_new ("org. rain. example ", G_APPLICATION_FLAGS_NONE); g_signal_connect (app," activate ", G_CALLBACK (activate ), NULL); app_status = g_application_run (G_APPLICATION (app), argc, argv); g_object_unref (app); return app_status;} static void print_hello (GtkWidget * button, gpointer data) {printf ("Hello, world! \ N ");} static void print_entry (GtkWidget * button, gpointer entry) {const gchar * entry_buf; // get the content of the input box and return a const gchar pointer, gchar is char entry_buf = gtk_entry_get_text (GTK_ENTRY (entry); if ('\ 0 '! = Entry_buf [0]) printf ("% s \ n", entry_buf);} static void activate (GtkApplication * app, gpointer data) {GtkBuilder * builder; GObject * window; GObject * button; GObject * entry; // create a GtkBuilder. Builder = gtk_builder_new (); // Add the UI description file to Builder. Gtk_builder_add_from_file (builder, "example. ui", NULL); // you can use this function to obtain the object created by Builder and obtain it based on the id of the object set in the example. ui file. Window = gtk_builder_get_object (builder, "window"); gtk_application_add_window (app, GTK_WINDOW (window); // note that the function returns a pointer to the GObject object. Button = gtk_builder_get_object (builder, "print_hello"); // set the button label gtk_button_set_label (GTK_BUTTON (button), "PrintHello"); g_signal_connect (button, "clicked ", g_CALLBACK (print_hello), NULL); // gets the input box object. Entry = gtk_builder_get_object (builder, "entry"); button = gtk_builder_get_object (builder, "print_entry"); gtk_button_set_label (GTK_BUTTON (button), "PrintEntry "); // when print_entry is called, the user parameter entry is passed. G_signal_connect (button, "clicked", G_CALLBACK (print_entry), entry );}
Compile and run:
gcc example.c `pkg-config --cflags --libs gtk+-3.0`./a.out
The running result is as follows: