Getting started with GTK: Layout exercise calculator and gtk getting started layout Calculator
Next, let's do a layout exercise, such:
We use table layout. The table layout reference coordinates are as follows:
The line editing control (GtkEntry) is used here ).
Create row Editing:
GtkWidget * gtk_entry_new (void );
Returned value: Row editing pointer row
Edit content settings:
Void gtk_entry_set_text (GtkEntry * entry,
Const gchar * text );
Entry: edit a row.
Text: content to be set
Set whether to allow row Editing:
Void gtk_editable_set_editable (GtkEditable * editable,
Gboolean is_editable );
Editable: Row editing
Is_editable: TRUE indicates that the data can be edited. FALSE indicates that the data cannot be edited.
The complete code is as follows:
# Include <gtk/gtk. h> // header file int main (int argc, char * argv []) {gtk_init (& argc, & argv ); // initialize // The window operation GtkWidget * window = gtk_window_new (GTK_WINDOW_TOPLEVEL); // create the window gtk_widget_set_size_request (window, 270,320 ); // table operation GtkWidget * table = gtk_table_new (5, 4, TRUE); // table layout, 5 rows and 4 columns // The operation GtkWidget * entry = gtk_entry_new (); // create gtk_entry_set_text (GTK_ENTRY (entry), "2 + 2 = 4") for row editing "); // set the content gtk_editable_set_editable (GTK_EDITABLE (entry), FALSE) for row editing. // You Cannot edit the content for row editing, only display the operation GtkWidget * button0 = gtk_button_new_with_label ("0"); // button 0 GtkWidget * button1 = gtk_button_new_with_label ("1 "); // button 1 GtkWidget * button2 = gtk_button_new_with_label ("2"); // button 2 GtkWidget * button3 = gtk_button_new_with_label ("3 "); // button 3 GtkWidget * button4 = gtk_button_new_with_label ("4"); // button 4 GtkWidget * button5 = gtk_button_new_with_label ("5 "); // button 5 GtkWidget * button6 = gtk_button_new_with_label ("6"); // button 6 GtkWidget * button7 = gtk_button_new_with_label ("7 "); // button 7 GtkWidget * button8 = gtk_button_new_with_label ("8"); // button 8 GtkWidget * button9 = gtk_button_new_with_label ("9 "); // button 9 GtkWidget * button_add = gtk_button_new_with_label ("+"); // Add GtkWidget * button_minus = gtk_button_new_with_label ("-"); // subtract GtkWidget * button_multiply = gtk_button_new_with_label ("*"); // multiply GtkWidget * button_divide = gtk_button_new_with_label ("/"); // except GtkWidget * button_equal = gtk_button_new_with_label ("="); // equals to GtkWidget * button_detele = gtk_button_new_with_label ("c "); // unregister // layout operation gtk_container_add (GTK_CONTAINER (window), table); // put the table into the window gtk_table_attach_defaults (GTK_TABLE (table), entry, 0, 4, 0, 1 ); // edit the row into the table gtk_table_attach_defaults (GTK_TABLE (table), button0, 0, 1, 4, 5); // click the button to add the table gtk_table_attach_defaults (GTK_TABLE (table), button1, 0, 1, 3, 4); gtk_table_attach_defaults (GTK_TABLE (table), button2, 1, 2, 3, 4); gtk_table_attach_defaults (GTK_TABLE (table), button3, 2, 3, 3, 4); gtk_table_attach_defaults (GTK_TABLE (table), button4, 0, 1, 2, 3); gtk_table_attach_defaults (GTK_TABLE (table), button5, 1, 2, 2, 3 ); gtk_table_attach_defaults (GTK_TABLE (table), button6, 2, 3, 2, 3); gtk_table_attach_defaults (GTK_TABLE (table), button7, 0, 1, 1, 2 ); gtk_table_attach_defaults (GTK_TABLE (table), button8, 1, 2, 1, 2); gtk_table_attach_defaults (GTK_TABLE (table), button9, 2, 3, 1, 2 ); gtk_table_attach_defaults (GTK_TABLE (table), button_add, 1, 2, 4, 5); gtk_table_attach_defaults (GTK_TABLE (table), button_minus, 2, 3, 4, 5 ); gtk_table_attach_defaults (GTK_TABLE (table), button_multiply, 3, 4, 2, 3); gtk_table_attach_defaults (GTK_TABLE (table), button_divide, 3, 4, 3, 4 ); gtk_table_attach_defaults (GTK_TABLE (table), button_equal, 3, 4, 4, 5); gtk_table_attach_defaults (GTK_TABLE (table), button_detele, 3, 4, 1, 2 ); gtk_widget_show_all (window); // display all controls gtk_main (); // enter the event loop return 0 ;}
Click here to download the source code.