記住下面幾個格式看,下面的代碼
聲明變量:GtkAbc*abc=gtk_abc_new()聲明控件;
賦值:gtk_abc_set_label(controlName,value);
添加到父容器:gtk_container_add(controFather,controlChildl);
顯示控件:gtk_widget_show(controlName);
一般默認值:gtk_box_pack_start()的後三個 值,一般為FALSE,FALSE,0
#include <stdio.h> #include<gtk/gtk.h> int main(int argc, char *argv[]) { //初始化gtk gtk_init(NULL,NULL); //定義一個指向window的指針 GtkWindow*window=gtk_window_new(GTK_WINDOW_TOPLEVEL); //窗體關閉之後執行 g_signal_connect(window,"destroy",gtk_main_quit,NULL); //設置標題 gtk_window_set_title(window,"標題"); //每個被添加的控件都要有show gtk_widget_show(window); //添加一個box,使用垂直布局 //GTK_ORIENTATION_HORIZONTAL為水平布局 GtkBox*box=gtk_box_new(GTK_ORIENTATION_VERTICAL,0); gtk_container_add(window,box); gtk_widget_show(box); GtkLabel*lb1=gtk_label_new("盒子布局"); gtk_container_add(box,lb1); gtk_box_pack_start(box,lb1,FALSE,FALSE,0); gtk_widget_show(lb1); GtkEntry*entry1=gtk_entry_new(); gtk_container_add(box,entry1); gtk_box_pack_start(box,entry1,FALSE,FALSE,0); gtk_widget_show(entry1); GtkButton*btn1=gtk_button_new(); gtk_button_set_label(btn1,"按鈕一"); gtk_container_add(box,btn1); gtk_box_pack_start(box,btn1,FALSE,FALSE,0); gtk_widget_show(btn1); gtk_main(); return 0; }
運行結果如下
gtk_grid_attach(grid1,btn1,1,2,3,4):
grid1表示父容器;
btn1表示被添加的控件;
1表示該控件距離左邊一個網格;
2表示該控件距離上邊兩個網格;
3表示該控件占據3個網格的寬度;
4表示該控件占據4個網格的高度;
記住幾句話:
1.網格布局的關鍵詞是Grid#include <stdio.h> #include<gtk/gtk.h> int main(int argc, char *argv[]) { gtk_init(NULL,NULL); GtkWindow*window=gtk_window_new(GTK_WINDOW_TOPLEVEL); gtk_window_set_title(window,"網格布局"); g_signal_connect(window,"destroy",gtk_main_quit,NULL); gtk_widget_show(window); GtkGrid*grid1=gtk_grid_new(); gtk_container_add(window,grid1); gtk_widget_show(grid1); GtkButton*btn1=gtk_button_new(); gtk_button_set_label(btn1,"111111"); gtk_grid_attach(grid1,btn1,1,1,1,1); gtk_container_add(grid1,btn1); gtk_widget_show(btn1); GtkButton*btn2=gtk_button_new(); gtk_button_set_label(btn2,"222222"); gtk_grid_attach(grid1,btn2,0,0,1,1); gtk_container_add(grid1,btn2); gtk_widget_show(btn2); gtk_main(); return 0; }
運行結果如下圖
今天,就寫這麼多了;我的目標並不高,希望每天寫一篇,具體細節等學習深入了,再慢慢補充。