An Introduction to C & GUI Programming -----Simon Long 学习笔记 6
Radio buttons 这种选择要求在一个组内,这样才会选择一个去勾选其它的
#include <gtk/gtk.h> GtkWidget *txt; void end_program (GtkWidget *wid, gpointer ptr) { printf("bybye\n"); gtk_main_quit (); } void copy_text (GtkWidget *wid, gpointer ptr) { const char *input = gtk_entry_get_text (GTK_ENTRY (txt)); gtk_label_set_text (GTK_LABEL (ptr), input); } void check_toggle (GtkWidget *wid, gpointer ptr) { static int i = 0; i++; char arr[60] = {0}; sprintf(arr, "The state of the button checked %d times\n", i); printf ("The state of the button is %d\n", gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wid))); gtk_label_set_text (GTK_LABEL (ptr), arr); } int main (int argc, char *argv[]) { gtk_init (&argc, &argv); GtkWidget *win1 = gtk_window_new (GTK_WINDOW_TOPLEVEL); GtkWidget *win2 = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_title (GTK_WINDOW(win1), "QT"); gtk_window_set_title (GTK_WINDOW(win2), "GTK"); GtkWidget *btn1 = gtk_button_new_with_label ("Close window1"); GtkWidget *btn2 = gtk_button_new_with_label ("copy right"); GtkWidget *btn3 = gtk_button_new_with_label ("copy left"); GtkWidget *chk1 = gtk_check_button_new_with_label ("My check"); GtkWidget *chk2 = gtk_check_button_new_with_label ("check"); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chk1), FALSE); // 设置默认勾选状态 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chk2), TRUE); // 设置默认勾选状态 GtkWidget *rad1 = gtk_radio_button_new_with_label (NULL,"男"); GSList *group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (rad1)); GtkWidget *rad2 = gtk_radio_button_new_with_label (group,"女"); // 要在同一个组 GtkWidget *labelA = gtk_label_new ("LABELA"); GtkWidget *labelB = gtk_label_new ("LABELB"); GtkAdjustment *adj = gtk_adjustment_new (0, -100, 100, 1, 50, 0); txt = gtk_spin_button_new (adj, 0, 0); GtkWidget *table1 = gtk_grid_new (); GtkWidget *table2 = gtk_grid_new (); gtk_grid_attach (GTK_GRID (table1), labelA, 0, 0, 10, 10); gtk_grid_attach (GTK_GRID (table1), btn1, 20, 20, 10, 10); gtk_grid_attach (GTK_GRID (table1), btn3, 40, 40, 10, 10); gtk_grid_attach (GTK_GRID (table1), chk1, 60, 60, 10, 10); gtk_grid_attach (GTK_GRID (table2), labelB, 0, 0, 10, 10); gtk_grid_attach (GTK_GRID (table2), btn2, 10, 10, 10, 10); gtk_grid_attach (GTK_GRID (table2), txt, 0, 20, 10, 10); gtk_grid_attach (GTK_GRID (table2), chk2, 20, 20, 10, 10); gtk_grid_attach (GTK_GRID (table2), rad1, 30, 30, 10, 10); gtk_grid_attach (GTK_GRID (table2), rad2, 30, 40, 10, 10); g_signal_connect (btn1, "clicked", G_CALLBACK (end_program), NULL); g_signal_connect (btn2, "clicked", G_CALLBACK (copy_text), labelB); g_signal_connect (btn2, "clicked", G_CALLBACK (copy_text), labelA); g_signal_connect (btn3, "clicked", G_CALLBACK (copy_text), labelB); g_signal_connect (btn3, "clicked", G_CALLBACK (copy_text), labelA); g_signal_connect (win2, "delete_event", G_CALLBACK (end_program), NULL); g_signal_connect (chk1, "toggled", G_CALLBACK (check_toggle),labelA); gtk_container_add (GTK_CONTAINER (win1), table1); gtk_container_add (GTK_CONTAINER (win2), table2); gtk_widget_show_all (win1); gtk_widget_show_all (win2); gtk_main (); return 0; }
编译gcc `pkg-config --cflags gtk+-3.0` test.c -o hello `pkg-config --libs gtk+-3.0`
结果
下拉选项的
#include <gtk/gtk.h> GtkWidget *txt; void end_program (GtkWidget *wid, gpointer ptr) { printf("bybye\n"); gtk_main_quit (); } void copy_text (GtkWidget *wid, gpointer ptr) { const char *input = gtk_entry_get_text (GTK_ENTRY (txt)); gtk_label_set_text (GTK_LABEL (ptr), input); } void check_toggle (GtkWidget *wid, gpointer ptr) { static int i = 0; i++; char arr[60] = {0}; sprintf(arr, "The state of the button checked %d times\n", i); printf ("The state of the button is %d\n", gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wid))); gtk_label_set_text (GTK_LABEL (ptr), arr); } int main (int argc, char *argv[]) { gtk_init (&argc, &argv); GtkWidget *win1 = gtk_window_new (GTK_WINDOW_TOPLEVEL); GtkWidget *win2 = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_title (GTK_WINDOW(win1), "QT"); gtk_window_set_title (GTK_WINDOW(win2), "GTK"); GtkWidget *btn1 = gtk_button_new_with_label ("Close window1"); GtkWidget *btn2 = gtk_button_new_with_label ("copy right"); GtkWidget *btn3 = gtk_button_new_with_label ("copy left"); GtkWidget *chk1 = gtk_check_button_new_with_label ("My check"); GtkWidget *chk2 = gtk_check_button_new_with_label ("check"); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chk1), FALSE); // 设置默认勾选状态 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chk2), TRUE); // 设置默认勾选状态 GtkWidget *rad1 = gtk_radio_button_new_with_label (NULL,"男"); GSList *group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (rad1)); GtkWidget *rad2 = gtk_radio_button_new_with_label (group,"女"); GtkWidget *labelA = gtk_label_new ("LABELA"); GtkWidget *labelB = gtk_label_new ("LABELB"); GtkAdjustment *adj = gtk_adjustment_new (0, -100, 100, 1, 50, 0); txt = gtk_spin_button_new (adj, 0, 0); GtkWidget *comb = gtk_combo_box_text_new (); gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (comb),"A"); gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (comb),"B"); gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (comb),"C"); gtk_combo_box_set_active (GTK_COMBO_BOX (comb), 0); GtkWidget *table1 = gtk_grid_new (); GtkWidget *table2 = gtk_grid_new (); gtk_grid_attach (GTK_GRID (table1), labelA, 0, 0, 10, 10); gtk_grid_attach (GTK_GRID (table1), btn1, 20, 20, 10, 10); gtk_grid_attach (GTK_GRID (table1), btn3, 40, 40, 10, 10); gtk_grid_attach (GTK_GRID (table1), chk1, 60, 60, 10, 10); gtk_grid_attach (GTK_GRID (table2), labelB, 0, 0, 10, 10); gtk_grid_attach (GTK_GRID (table2), btn2, 10, 10, 10, 10); gtk_grid_attach (GTK_GRID (table2), txt, 0, 20, 10, 10); gtk_grid_attach (GTK_GRID (table2), chk2, 20, 20, 10, 10); gtk_grid_attach (GTK_GRID (table2), rad1, 30, 30, 10, 10); gtk_grid_attach (GTK_GRID (table2), rad2, 30, 40, 10, 10); gtk_grid_attach (GTK_GRID (table2), comb, 0, 40, 10, 10); g_signal_connect (btn1, "clicked", G_CALLBACK (end_program), NULL); g_signal_connect (btn2, "clicked", G_CALLBACK (copy_text), labelB); g_signal_connect (btn2, "clicked", G_CALLBACK (copy_text), labelA); g_signal_connect (btn3, "clicked", G_CALLBACK (copy_text), labelB); g_signal_connect (btn3, "clicked", G_CALLBACK (copy_text), labelA); g_signal_connect (win2, "delete_event", G_CALLBACK (end_program), NULL); g_signal_connect (chk1, "toggled", G_CALLBACK (check_toggle),labelA); gtk_container_add (GTK_CONTAINER (win1), table1); gtk_container_add (GTK_CONTAINER (win2), table2); gtk_widget_show_all (win1); gtk_widget_show_all (win2); gtk_main (); return 0; }
结果
将下拉选项与信号挂钩
#include <gtk/gtk.h> GtkWidget *txt; void end_program (GtkWidget *wid, gpointer ptr) { printf("bybye\n"); gtk_main_quit (); } void copy_text (GtkWidget *wid, gpointer ptr) { const char *input = gtk_entry_get_text (GTK_ENTRY (txt)); gtk_label_set_text (GTK_LABEL (ptr), input); } void check_toggle (GtkWidget *wid, gpointer ptr) { static int i = 0; i++; char arr[60] = {0}; sprintf(arr, "The state of the button checked %d times\n", i); printf ("The state of the button is %d\n", gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wid))); gtk_label_set_text (GTK_LABEL (ptr), arr); } void combo_changed (GtkWidget *wid, gpointer ptr) { char arr[60] = {0}; int sel = gtk_combo_box_get_active (GTK_COMBO_BOX (wid)); char *selected = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (wid)); sprintf (arr, "The value of the combo is %d %s\n", sel, selected); gtk_label_set_text (GTK_LABEL (ptr), arr); } int main (int argc, char *argv[]) { gtk_init (&argc, &argv); GtkWidget *win1 = gtk_window_new (GTK_WINDOW_TOPLEVEL); GtkWidget *win2 = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_title (GTK_WINDOW(win1), "QT"); gtk_window_set_title (GTK_WINDOW(win2), "GTK"); GtkWidget *btn1 = gtk_button_new_with_label ("Close window1"); GtkWidget *btn2 = gtk_button_new_with_label ("copy right"); GtkWidget *btn3 = gtk_button_new_with_label ("copy left"); GtkWidget *chk1 = gtk_check_button_new_with_label ("My check"); GtkWidget *chk2 = gtk_check_button_new_with_label ("check"); gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chk1), FALSE); // 设置默认勾选状态 gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chk2), TRUE); // 设置默认勾选状态 GtkWidget *rad1 = gtk_radio_button_new_with_label (NULL,"男"); GSList *group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (rad1)); GtkWidget *rad2 = gtk_radio_button_new_with_label (group,"女"); GtkWidget *labelA = gtk_label_new ("LABELA"); GtkWidget *labelB = gtk_label_new ("LABELB"); GtkWidget *labelC = gtk_label_new ("LABELC"); GtkAdjustment *adj = gtk_adjustment_new (0, -100, 100, 1, 50, 0); txt = gtk_spin_button_new (adj, 0, 0); GtkWidget *comb = gtk_combo_box_text_new (); gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (comb),"A"); gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (comb),"B"); gtk_combo_box_text_append_text (GTK_COMBO_BOX_TEXT (comb),"C"); gtk_combo_box_set_active (GTK_COMBO_BOX (comb), 0); GtkWidget *table1 = gtk_grid_new (); GtkWidget *table2 = gtk_grid_new (); gtk_grid_attach (GTK_GRID (table1), labelA, 0, 0, 10, 10); gtk_grid_attach (GTK_GRID (table1), labelC, 0, 20, 10, 10); gtk_grid_attach (GTK_GRID (table1), btn1, 20, 20, 10, 10); gtk_grid_attach (GTK_GRID (table1), btn3, 40, 40, 10, 10); gtk_grid_attach (GTK_GRID (table1), chk1, 60, 60, 10, 10); gtk_grid_attach (GTK_GRID (table2), labelB, 0, 0, 10, 10); gtk_grid_attach (GTK_GRID (table2), btn2, 10, 10, 10, 10); gtk_grid_attach (GTK_GRID (table2), txt, 0, 20, 10, 10); gtk_grid_attach (GTK_GRID (table2), chk2, 20, 20, 10, 10); gtk_grid_attach (GTK_GRID (table2), rad1, 30, 30, 10, 10); gtk_grid_attach (GTK_GRID (table2), rad2, 30, 40, 10, 10); gtk_grid_attach (GTK_GRID (table2), comb, 0, 40, 10, 10); g_signal_connect (btn1, "clicked", G_CALLBACK (end_program), NULL); g_signal_connect (btn2, "clicked", G_CALLBACK (copy_text), labelB); g_signal_connect (btn2, "clicked", G_CALLBACK (copy_text), labelA); g_signal_connect (btn3, "clicked", G_CALLBACK (copy_text), labelB); g_signal_connect (btn3, "clicked", G_CALLBACK (copy_text), labelA); g_signal_connect (win2, "delete_event", G_CALLBACK (end_program), NULL); g_signal_connect (chk1, "toggled", G_CALLBACK (check_toggle),labelA); g_signal_connect (comb, "changed", G_CALLBACK (combo_changed),labelC); gtk_container_add (GTK_CONTAINER (win1), table1); gtk_container_add (GTK_CONTAINER (win2), table2); gtk_widget_show_all (win1); gtk_widget_show_all (win2); gtk_main (); return 0; }
结果
启动时
点击下拉框时