GTK Beginner Learning: Using glade

Source: Internet
Author: User

After setting up the environment, the Glade tool can be started by knocking Glade at the terminal.


The overall block diagram of the Glade:


Common control Selection: Lists commonly used controls, commonly used in three categories: Top (main window, etc.), containers (various layout containers, etc.), control and display (buttons, notes, picture controls, etc.)

When the mouse is placed in the control, the Chinese text of the control is automatically displayed, and it can also be set artificially, such as:




Interface Editing Area: Drag and drop the controls here to make the appropriate layout

Control Monitor: You can see all the controls on the interface, and with this control selected, you can see the specific type of the control


Property Editing Area: Edit the frequently used properties of the selected control, such as the Window Settings title, window type, display location on the screen, and so on.

Toolbar: The following buttons are commonly used NEW: Create a new glade file
Open: Open a Glade file that already exists
Save: Save a Glade file
Select: Press this button to select the control
Drag to resize: Press this button to move the control's position, change the size of the control



now start learning the Glade operation.
When you select a control, you must first press the toolbar's Select button
Shortcut keys for Windows that support Undo (Ctrl + Z) and recovery (ctrl+y) when operating

The process and layout of the operation are consistent:
1) Select the main window and set the corresponding properties of the window as needed
2) Select Layout container
3) Select the appropriate control as needed, and set the corresponding properties of the control as needed

First step: Select the main window and set the corresponding properties of the window as needed
1) Select Window


2) Set Window caption: we learn glade together; fixed window size; setting Window Position: Center


3) Set the width and height of the window



Step Two: Select the layout container (fixed layout allows the user to arbitrarily layout, so we select this layout)




The third step: Select the appropriate control as needed, set the corresponding properties of the control as needed (each control is set to the same method, with "button" for example)
1) Select two buttons



2) Adjust the position of the button and its size (there are two ways to set it)
A) in the interface editing area by drag-and-drop method to adjust


B) set by the attribute edit area (start coordinates set first, then width and height)

A) Set the start coordinate of the control



b) Set the width and height of the control




4) Set the properties of the button

A) The first button as a normal button with text content




B) The second button is a button without a border
button to go to border





other controls are similar in how they are operated, and are not listed here.


In the code operation, we need to be concerned about how to get the control of this interface through code, as in this example (main window, button), and in the interface, each control has an identity name, which is the name of the control monitor area. In our code, we get the controls in the interface using this identifier name :




This identity name can be modified, such as




Here, our interface has been set up (a main window put a fixed layout, the layout also put 2 buttons), save this interface can be used, save the time to choose the appropriate path to save the default way, the file suffix can be arbitrary, in order to easily identify the file, we'd better use the. Glade suffix, Save here as Test.glade.


Code manipulation

Can be easily divided into two steps:

1) Read the Glade file

Create Gtkbuilder object, Gtkbuilder in <gtk/gtk.h> declaration

Gtkbuilder *builder = Gtk_builder_new ();

Read information about the Test.glade file, stored in the builder pointer variable

Gtk_builder_add_from_file (Builder, "./test.glade", NULL);


2) Get the controls in the Glade file

Get the window control pointer, and note that "Window1" matches the name of the flag in Glade

Gtkwidget *window = gtk_widget (Gtk_builder_get_object (builder, "Window1"));


The code is as follows:

#include <gtk/gtk.h> int main (int argc,char *argv[]) {//1.GTK initialize Gtk_init (&ARGC,&ARGV);//2. Create Gtkbuilder object, Gtkbuilder in <gtk/gtk.h> declaration gtkbuilder *builder = gtk_builder_new ();//3. Read Test.glade file information, save in Builder if (!gtk_builder_add_from_file (builder, "Test.glade", NULL)) {printf ("Connot load file!");} 4. Get the window pointer, note "Window1" to match the Glade inside the label noun gtkwidget *window = gtk_widget (Gtk_builder_get_object (builder, "Window1")); Gtkbutton *button = Gtk_button (Gtk_builder_get_object (builder, "Button1")); const char *text = Gtk_button_get_label ( button);p rintf ("text=%s\n", text);
Gtk_widget_show_all (window); Gtk_main (); return 0; }


Operation Result:


By using the Glade tool, we can quickly design the user interface by dragging and dropping the control, and we can make the corresponding layout very intuitively. However, if we want to implement more functions, such as setting the background map for the window, let the button do the corresponding action, we also have to be implemented through the code. Glade just assists us in designing the window, it is not omnipotent.



SOURCE Download: http://download.csdn.net/download/lianghe_work/8936799

Turn from:

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

GTK Beginner Learning: Using glade

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.