欢迎您访问365答案网,请分享给你的朋友!
生活常识 学习资料

第62页的gtk+编程例子——菜单

时间:2023-06-24

主要是参考《GTK+:“菜单” (menubar)和“工具栏”(toolbars)》
https://www.cnblogs.com/hotsummer/archive/2011/03/01/1967731.html
例子中的vbox = gtk_vbox_new(FALSE, 0);是属于gtk+2.0的,要改为vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);才能在gtk+3.0编译通过,原作者的讲解是比较容易理解,在此要感谢原作者

以下gtk+编程例子是来自书籍《实用技术:开发Linux应用——用GTK+和GDK开发Linux图形用户界面应用》第62页的内容——菜单,结合自己的理解,写了一个简单的小程序

粥 粉 面 饭 其它
白粥 螺蛳粉 牛腩面 叉烧饭 云吞
猪红粥 桂林米粉 兰州拉面 扬州炒饭 萝卜糕
八宝粥 肠粉 捞面 煲仔饭 粽
艇仔粥 汤粉 竹升面 蒸饭 油炸鬼
皮蛋瘦肉粥 炒粉 刀削面 竹筒饭 蛋塔

在openSUSE-Leap-15.3-DVD-x86_64的gnome3.34.7桌面环境下编译的,其中pkg-config --cflags --libs gtk±3.0两边有反引号括起来的
ruhong@localhost:~/gtk> gcc page62.c -o page62 pkg-config --cflags --libs gtk±3.0
ruhong@localhost:~/gtk> ./page62

#include int main( int argc, char *argv[] ){GtkWidget *window;GtkWidget *vbox;GtkWidget *menubar;GtkWidget *zhou;GtkWidget *fen;GtkWidget *mian;GtkWidget *fan;GtkWidget *qita;GtkWidget *zhoumenu;GtkWidget *zhou1;GtkWidget *zhou2;GtkWidget *zhou3;GtkWidget *zhou4;GtkWidget *zhou5;GtkWidget *fenmenu;GtkWidget *fen1;GtkWidget *fen2;GtkWidget *fen3;GtkWidget *fen4;GtkWidget *fen5;GtkWidget *mianmenu;GtkWidget *mian1;GtkWidget *mian2;GtkWidget *mian3;GtkWidget *mian4;GtkWidget *mian5;GtkWidget *fanmenu;GtkWidget *fan1;GtkWidget *fan2;GtkWidget *fan3;GtkWidget *fan4;GtkWidget *fan5;GtkWidget *qitamenu;GtkWidget *qita1;GtkWidget *qita2;GtkWidget *qita3;GtkWidget *qita4;GtkWidget *qita5;gtk_init (&argc, &argv);window = gtk_window_new (GTK_WINDOW_TOPLEVEL);gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);gtk_window_set_default_size(GTK_WINDOW(window), 500, 200);gtk_window_set_title (GTK_WINDOW (window), "菜单");g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL);vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);gtk_container_add (GTK_CONTAINER (window), vbox);menubar = gtk_menu_bar_new ();gtk_box_pack_start (GTK_BOX (vbox), menubar, FALSE, TRUE, 0);zhou = gtk_menu_item_new_with_label ("粥");gtk_menu_shell_append (GTK_MENU_SHELL(menubar), zhou);fen = gtk_menu_item_new_with_label ("粉");gtk_menu_shell_append (GTK_MENU_SHELL(menubar), fen);mian = gtk_menu_item_new_with_label ("面");gtk_menu_shell_append (GTK_MENU_SHELL(menubar), mian);fan = gtk_menu_item_new_with_label ("饭");gtk_menu_shell_append (GTK_MENU_SHELL(menubar), fan);qita = gtk_menu_item_new_with_label ("其它");gtk_menu_shell_append (GTK_MENU_SHELL(menubar), qita);zhoumenu = gtk_menu_new ();gtk_menu_item_set_submenu (GTK_MENU_ITEM (zhou), zhoumenu);zhou1 = gtk_menu_item_new_with_label ("白粥");gtk_menu_shell_append (GTK_MENU_SHELL (zhoumenu), zhou1);zhou2 = gtk_menu_item_new_with_label ("猪红粥");gtk_menu_shell_append (GTK_MENU_SHELL (zhoumenu), zhou2);zhou3 = gtk_menu_item_new_with_label ("八宝粥");gtk_menu_shell_append (GTK_MENU_SHELL (zhoumenu), zhou3);zhou4 = gtk_menu_item_new_with_label ("艇仔粥");gtk_menu_shell_append (GTK_MENU_SHELL (zhoumenu), zhou4);zhou5 = gtk_menu_item_new_with_label ("皮蛋瘦肉粥");gtk_menu_shell_append (GTK_MENU_SHELL (zhoumenu), zhou5);fenmenu = gtk_menu_new ();gtk_menu_item_set_submenu (GTK_MENU_ITEM (fen), fenmenu);fen1 = gtk_menu_item_new_with_label ("螺蛳粉");gtk_menu_shell_append (GTK_MENU_SHELL (fenmenu), fen1);fen2 = gtk_menu_item_new_with_label ("桂林米粉");gtk_menu_shell_append (GTK_MENU_SHELL (fenmenu), fen2);fen3 = gtk_menu_item_new_with_label ("肠粉");gtk_menu_shell_append (GTK_MENU_SHELL (fenmenu), fen3);fen4 = gtk_menu_item_new_with_label ("汤粉");gtk_menu_shell_append (GTK_MENU_SHELL (fenmenu), fen4);fen5 = gtk_menu_item_new_with_label ("炒粉");gtk_menu_shell_append (GTK_MENU_SHELL (fenmenu), fen5);mianmenu = gtk_menu_new ();gtk_menu_item_set_submenu (GTK_MENU_ITEM (mian), mianmenu);mian1 = gtk_menu_item_new_with_label ("牛腩面");gtk_menu_shell_append (GTK_MENU_SHELL (mianmenu), mian1);mian2 = gtk_menu_item_new_with_label ("兰州拉面");gtk_menu_shell_append (GTK_MENU_SHELL (mianmenu), mian2);mian3 = gtk_menu_item_new_with_label ("捞面");gtk_menu_shell_append (GTK_MENU_SHELL (mianmenu), mian3);mian4 = gtk_menu_item_new_with_label ("竹升面");gtk_menu_shell_append (GTK_MENU_SHELL (mianmenu), mian4);mian5 = gtk_menu_item_new_with_label ("刀削面");gtk_menu_shell_append (GTK_MENU_SHELL (mianmenu), mian5);fanmenu = gtk_menu_new ();gtk_menu_item_set_submenu (GTK_MENU_ITEM (fan), fanmenu);fan1 = gtk_menu_item_new_with_label ("叉烧饭");gtk_menu_shell_append (GTK_MENU_SHELL (fanmenu), fan1);fan2 = gtk_menu_item_new_with_label ("扬州炒饭");gtk_menu_shell_append (GTK_MENU_SHELL (fanmenu), fan2);fan3 = gtk_menu_item_new_with_label ("煲仔饭");gtk_menu_shell_append (GTK_MENU_SHELL (fanmenu), fan3);fan4 = gtk_menu_item_new_with_label ("蒸饭");gtk_menu_shell_append (GTK_MENU_SHELL (fanmenu), fan4);fan5 = gtk_menu_item_new_with_label ("竹筒饭");gtk_menu_shell_append (GTK_MENU_SHELL (fanmenu), fan5);qitamenu = gtk_menu_new ();gtk_menu_item_set_submenu (GTK_MENU_ITEM (qita), qitamenu);qita1 = gtk_menu_item_new_with_label ("云吞");gtk_menu_shell_append (GTK_MENU_SHELL (qitamenu), qita1);qita2 = gtk_menu_item_new_with_label ("萝卜糕");gtk_menu_shell_append (GTK_MENU_SHELL (qitamenu), qita2);qita3 = gtk_menu_item_new_with_label ("粽");gtk_menu_shell_append (GTK_MENU_SHELL (qitamenu), qita3);qita4 = gtk_menu_item_new_with_label ("油炸鬼");gtk_menu_shell_append (GTK_MENU_SHELL (qitamenu), qita4);qita5 = gtk_menu_item_new_with_label ("蛋塔");gtk_menu_shell_append (GTK_MENU_SHELL (qitamenu), qita5);gtk_widget_show_all (window);gtk_main ();return 0;}

效果图如下

《GTK+:“菜单” (menubar)和“工具栏”(toolbars)》
https://www.cnblogs.com/hotsummer/archive/2011/03/01/1967731.html
例子中的vbox = gtk_vbox_new(FALSE, 0);是属于gtk+2.0的,要改为vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);才能在gtk+3.0编译通过,原作者的讲解是比较容易理解,在此要感谢原作者,重新排版如下:

#include int main( int argc, char *argv[]){GtkWidget *window;GtkWidget *vbox;GtkWidget *menubar;GtkWidget *filemenu;GtkWidget *file;GtkWidget *quit;gtk_init(&argc, &argv);window = gtk_window_new(GTK_WINDOW_TOPLEVEL);gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);gtk_window_set_default_size(GTK_WINDOW(window), 250, 200);gtk_window_set_title(GTK_WINDOW(window), "menu");g_signal_connect_swapped(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);gtk_container_add(GTK_CONTAINER(window), vbox);menubar = gtk_menu_bar_new();gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 3);file = gtk_menu_item_new_with_label("File");gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file);filemenu = gtk_menu_new();gtk_menu_item_set_submenu(GTK_MENU_ITEM(file), filemenu);quit = gtk_menu_item_new_with_label("Quit");gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), quit);g_signal_connect(G_OBJECT(quit), "activate", G_CALLBACK(gtk_main_quit), NULL);gtk_widget_show_all(window);gtk_main();return 0;}

Copyright © 2016-2020 www.365daan.com All Rights Reserved. 365答案网 版权所有 备案号:

部分内容来自互联网,版权归原作者所有,如有冒犯请联系我们,我们将在三个工作时内妥善处理。