Use Gtk + to develop GUI applications on Linux

Source: Internet
Author: User
Use Gtk + to develop the Linux GUI application software-general Linux technology-Linux programming and kernel information. The following is a detailed description. Generally, a container component can only accommodate one component. If you want to arrange multiple sub-components in a container component, You can first put the multiple sub-components into a combo or composite table component, then, add the combox or composite table component to the container component.

   1. How to establish Gtk + applications

1. Basic Concepts

We usually use the Gtk + Library to develop GUI applications on Linux. To gain an in-depth understanding of the application of Gtk +, we must first clarify several concepts.

(1) concept of components

Application entities such as Windows, menus, and buttons in the Gtk + Library are called widgets ). The component has an object-oriented feature. Its specific structure is defined by the Gtk + Library. It is transparent to programmers who use the component unless you want to create your own component. When arranging components, we only need to care about the processing functions of the components. The purpose of doing so is to shield the application programmers from the details of the components, enables programmers to focus more on application functions.

(2) container Concept

Components are mainly divided into two categories: container and miscellaneous ). Most components belong to the container class. They can accommodate other components like containers. When arranging components, you often need to follow one rule: Except for the box and table components, other container components can only accommodate one component. The miscellaneous component is simpler than the container component, but it cannot accommodate other components.

(3) concepts of message and call-back Functions

The program must be able to respond to user operations. In GUI-based programming, "messages" or "signals" are necessary. You can click menus, buttons, input user data, query running results, and drag and drop to generate messages or signals. The signal may need to be processed by the software. In this case, the programmer needs to write the message callback function. Messages or signals are similar to events in Windows. In Gtk +, various signals are often generated. Most of the signals generated by the user are ignored. Only the signals concerned by the program are called by corresponding processing functions in the program. Programmers need to register the relationship between signals and processing functions and tell Gtk + which signals or events are concerned by the program.

2. Application of components (widgets)

When calling components of Gtk +, perform the following steps:

(1) header file containing the called component

The header file is usually stored in the/usr/include/gtk/directory, which contains the definition of the component and the operation functions of the component. When we use the component, you can often view the call format or parameters of component functions in this directory. We do not need to include all components in the header file, because the header file of the component is included in the next file named gtk. h under the Directory, which contains the header files of all components. Generally, the system specifies the compiled include directory as/usr/include, so we only need to specify include.

(2) Declaration component

The process of declaring a component is to comply with the principles first stated and used in the C language. Generally, the pointer of the component structure should be declared at the beginning of the program, which assigns an address to the component.

(3) component pointer Initialization

Call the component creation function to adjust the component attributes (such as size, position, and status) and then display the component. This cannot be ignored, if you have created only the component but forgot to display it (gtk_widget_show), you will not be able to see the component during the application, and you will not be able to operate the component.

(4) arrange the component location and hierarchy

The relationship between components is usually the relationship between the container and the container, that is, there are containers in the container, nesting layer by layer. Remember that a container component can only accommodate one component. If you want to arrange multiple sub-components in a container component, You can first put the multiple sub-components into a combo or composite table component, then, add the combox or composite table component to the container component. Note that the parent container component must be created before the child container component. The display of the parent container component must be after the display of the Child container component.

(5) Compile event processing functions

The component header file defines the events (messages) that can be responded by the component. However, not all messages are concerned by the program in the application, programmers must write event processing functions.

   II. Application Example

In order to have a specific understanding of the application of the Gtk + Library, let's look at a simple application instance.

1. initialize Gtk +

To write a Gtk + program, you must call the gtk_init function to initialize the Gtk + Library. We first pass the argc and argv variables of the application to the gtk_init function, and check the Gtk + options mainly used for debugging. If any such Gtk + parameter appears in the Variable list, move it. After running the gtk_init function, they should not appear in the application. The code for initializing Gtk + is as follows:
CODE: gtk_init (& argc, & argv );
Related Article

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.