Labels and layout 标签和布局
Label---主要用于显示文字,用户不能修改文字
demo
#include <gtk/gtk.h> void end_program (GtkWidget *wid, gpointer ptr) { printf("bybye "); gtk_main_quit (); } 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"); gtk_window_set_default_size(GTK_WINDOW(win1), 120, 240); gtk_window_set_default_size(GTK_WINDOW(win2), 240, 120); GtkWidget *btn1 = gtk_button_new_with_label ("Close window1"); GtkWidget *btn2 = gtk_button_new_with_label ("Close window2"); GtkWidget *labelA = gtk_label_new ("LABELA"); GtkWidget *labelB = gtk_label_new ("LABELB"); gtk_container_add (GTK_CONTAINER (win1), labelA); gtk_container_add (GTK_CONTAINER (win2), labelB); g_signal_connect (btn1, "clicked", G_CALLBACK (end_program), NULL); g_signal_connect (win2, "delete_event", G_CALLBACK (end_program), NULL); 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`
结果
作者在这里也说明了为什么1个window只能有1个widget
a GtkWindow can only contain one widget at a time, and that this one already contains a GtkButton.
So you can only put one widget into a window, but we want to have two – a button and a label; that’s not going to work
解决方法---BOX
#include <gtk/gtk.h>
void end_program (GtkWidget *wid, gpointer ptr) { printf("bybye "); gtk_main_quit (); } 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"); gtk_window_set_default_size(GTK_WINDOW(win1), 120, 240); gtk_window_set_default_size(GTK_WINDOW(win2), 240, 120); GtkWidget *btn1 = gtk_button_new_with_label ("Close window1"); GtkWidget *btn2 = gtk_button_new_with_label ("Close window2"); GtkWidget *labelA = gtk_label_new ("LABELA"); GtkWidget *labelB = gtk_label_new ("LABELB"); gtk_label_set_text(GTK_LABEL(labelA), "LABELAAAAAA"); gtk_label_set_text(GTK_LABEL(labelB), "LABELBBBBBB"); GtkWidget *box1 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5); GtkWidget *box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5); gtk_box_pack_start (GTK_BOX (box1), btn1, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (box1), labelA, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (box2), btn2, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (box2), labelB, TRUE, TRUE, 0); g_signal_connect (btn1, "clicked", G_CALLBACK (end_program), NULL); g_signal_connect (win2, "delete_event", G_CALLBACK (end_program), NULL); gtk_container_add (GTK_CONTAINER (win1), box1); gtk_container_add (GTK_CONTAINER (win2), box2); gtk_widget_show_all (win1); gtk_widget_show_all (win2); gtk_main (); return 0; }
结果
2个新函数,gtk_box_new,gtk_box_pack_start函数细节见 https://developer.gnome.org/gtk3/stable/GtkBox.html
点击按键,显示按键点击次数
#include <gtk/gtk.h> void end_program (GtkWidget *wid, gpointer ptr) { printf("bybye "); gtk_main_quit (); } void count (GtkWidget *wid, gpointer ptr) { char buffer[30] = { 0 }; static int count = 0; count++; sprintf (buffer, "Button pressed %d times", count); gtk_label_set_text (GTK_LABEL (ptr), buffer); } 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"); gtk_window_set_default_size(GTK_WINDOW(win1), 120, 240); gtk_window_set_default_size(GTK_WINDOW(win2), 240, 120); GtkWidget *btn1 = gtk_button_new_with_label ("Close window1"); GtkWidget *btn2 = gtk_button_new_with_label ("Close window2"); GtkWidget *btn3 = gtk_button_new_with_label ("count "); GtkWidget *labelA = gtk_label_new ("LABELA"); GtkWidget *labelB = gtk_label_new ("LABELB"); gtk_label_set_text(GTK_LABEL(labelA), "LABELAAAAAA"); gtk_label_set_text(GTK_LABEL(labelB), "LABELBBBBBB"); GtkWidget *box1 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5); GtkWidget *box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5); gtk_box_pack_start (GTK_BOX (box1), btn1, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (box1), labelA, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (box1), btn3, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (box2), btn2, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (box2), labelB, TRUE, TRUE, 0); g_signal_connect (btn1, "clicked", G_CALLBACK (end_program), NULL); g_signal_connect (btn3, "clicked", G_CALLBACK (count), labelB); g_signal_connect (win2, "delete_event", G_CALLBACK (end_program), NULL); gtk_container_add (GTK_CONTAINER (win1), box1); gtk_container_add (GTK_CONTAINER (win2), box2); gtk_widget_show_all (win1); gtk_widget_show_all (win2); gtk_main (); return 0; }
结果
启动时
点击count 按钮时
点击count时,绑定多个label会如何呢
#include <gtk/gtk.h> void end_program (GtkWidget *wid, gpointer ptr) { printf("bybye "); gtk_main_quit (); } void count (GtkWidget *wid, gpointer ptr) { char buffer[30] = { 0 }; static int count= 0; count++; sprintf (buffer, "Button pressed %d times", count); gtk_label_set_text (GTK_LABEL (ptr), buffer); } 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"); gtk_window_set_default_size(GTK_WINDOW(win1), 120, 240); gtk_window_set_default_size(GTK_WINDOW(win2), 240, 120); GtkWidget *btn1 = gtk_button_new_with_label ("Close window1"); GtkWidget *btn2 = gtk_button_new_with_label ("Close window2"); GtkWidget *btn3 = gtk_button_new_with_label ("count "); GtkWidget *labelA = gtk_label_new ("LABELA"); GtkWidget *labelB = gtk_label_new ("LABELB"); gtk_label_set_text(GTK_LABEL(labelA), "LABELAAAAAA"); gtk_label_set_text(GTK_LABEL(labelB), "LABELBBBBBB"); GtkWidget *box1 = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 5); GtkWidget *box2 = gtk_box_new (GTK_ORIENTATION_VERTICAL, 5); gtk_box_pack_start (GTK_BOX (box1), btn1, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (box1), labelA, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (box1), btn3, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (box2), btn2, TRUE, TRUE, 0); gtk_box_pack_start (GTK_BOX (box2), labelB, TRUE, TRUE, 0); g_signal_connect (btn1, "clicked", G_CALLBACK (end_program), NULL); g_signal_connect (btn3, "clicked", G_CALLBACK (count), labelB); g_signal_connect (btn3, "clicked", G_CALLBACK (count), labelA); g_signal_connect (win2, "delete_event", G_CALLBACK (end_program), NULL); gtk_container_add (GTK_CONTAINER (win1), box1); gtk_container_add (GTK_CONTAINER (win2), box2); gtk_widget_show_all (win1); gtk_widget_show_all (win2); gtk_main (); return 0; }
结果,实际点击1次但显示2次,又碰到麻烦了,先归入待处理分类。