An Introduction to C & GUI Programming -----Simon Long 学习笔记 3
这一讲讲的是按键button
#include <gtk/gtk.h> 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); GtkWidget *btn = gtk_button_new_with_label ("Close window"); gtk_window_set_title (GTK_WINDOW(win1), "QT is better than GTK"); gtk_window_set_title (GTK_WINDOW(win2), "GTK is better than QT"); gtk_window_set_default_size(GTK_WINDOW(win1), 120, 240); gtk_window_set_default_size(GTK_WINDOW(win2), 240, 120); gtk_container_add (GTK_CONTAINER (win1), btn); gtk_widget_show (win2); gtk_widget_show_all (win1); gtk_main (); return 0; }
编译 gcc `pkg-config --cflags gtk+-3.0` test.c -o hello `pkg-config --libs gtk+-3.0`
结果
出现了一个新的函数 gtk_widget_show_all,把gtk_widget_show_all (win1)改为gtk_widget_show(win1)看看会出现什么情况
#include <gtk/gtk.h> 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); GtkWidget *btn = gtk_button_new_with_label ("Close window"); gtk_window_set_title (GTK_WINDOW(win1), "QT is better than GTK"); gtk_window_set_title (GTK_WINDOW(win2), "GTK is better than QT"); gtk_window_set_default_size(GTK_WINDOW(win1), 120, 240); gtk_window_set_default_size(GTK_WINDOW(win2), 240, 120 ); gtk_container_add (GTK_CONTAINER (win1), btn); gtk_widget_show (win1); gtk_widget_show (win2); gtk_main (); return 0; }
结果
发现按键不见了,猜测 gtk_widget_show_all 会显示1个窗口中所有的内容,多加几个按键试试看
#include <gtk/gtk.h> 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); 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 ("Close window3"); gtk_window_set_title (GTK_WINDOW(win1), "QT is better than GTK"); gtk_window_set_title (GTK_WINDOW(win2), "GTK is better than QT"); gtk_window_set_default_size(GTK_WINDOW(win1), 120, 240); gtk_window_set_default_size(GTK_WINDOW(win2), 240, 120 ); gtk_container_add (GTK_CONTAINER (win1), btn1); gtk_container_add (GTK_CONTAINER (win1), btn2); gtk_container_add (GTK_CONTAINER (win2), btn3); gtk_widget_show_all (win1); gtk_widget_show_all (win2); gtk_main (); return 0; }
结果
编译告警但是能运行,告警窗口win1 有1个widget,再加一个不行,也不知道为啥,先归类到待处理类别中。(可以用box解决这个问题,见学习笔记4)
按键button的信号回调函数
#include <gtk/gtk.h>
// 点击按键的时候会触发调用这个函数 void end_program (GtkWidget *wid, gpointer ptr) { printf("bybye\n"); 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); GtkWidget *btn1 = gtk_button_new_with_label ("Close window1"); GtkWidget *btn2 = gtk_button_new_with_label ("Close window2"); 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 ); g_signal_connect (btn1, "clicked", G_CALLBACK (end_program), NULL); g_signal_connect (btn2, "clicked", G_CALLBACK (end_program), NULL); gtk_container_add (GTK_CONTAINER (win1), btn1); gtk_container_add (GTK_CONTAINER (win2), btn2); gtk_widget_show_all (win1); gtk_widget_show_all (win2); gtk_main (); return 0; }
结果 : 无论点击窗口1还是2的close window,均会关闭2个窗口,并在shell下打印bye bye。
点击窗口的x可以关闭程序(
if we click the X at the top right, the window closes but the
program doesn’t exit. Let’s fix that.
The way we do this is to connect another handler to the signal generated when that X is
clicked. The name of this signal is delete_event , and it is generated from the window
widget)
#include <gtk/gtk.h> void end_program (GtkWidget *wid, gpointer ptr) { printf("bybye\n"); 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); GtkWidget *btn1 = gtk_button_new_with_label ("Close window1"); GtkWidget *btn2 = gtk_button_new_with_label ("Close window2"); 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 ); g_signal_connect (btn1, "clicked", G_CALLBACK (end_program), NULL); g_signal_connect (win2, "delete_event", G_CALLBACK (end_program), NULL); // 窗口点击X会触发delete_event信号 gtk_container_add (GTK_CONTAINER (win1), btn1); gtk_container_add (GTK_CONTAINER (win2), btn2); gtk_widget_show_all (win1); gtk_widget_show_all (win2); gtk_main (); return 0; }
结果:点击 Close window2没反应了,点击X关闭所有窗口了