我正在使用gtk +和c。我已经创建了菜单栏,现在想要附加一些带有一些按钮的表格。我已经编写了所有必要的API例程,但是,我面临的问题是,如果我首先创建菜单栏然后表格没有渲染到窗口而且我渲染表格第一个菜单栏没有在它的位置看到..我不会'我知道为什么。这是代码
/* Button Boxes
*
* The Button Box widgets are used to arrange buttons with padding.
*/
#include <gtk/gtk.h>
GtkWidget *window,*button,*container,*menubar,*fileM,*menu,*vbox,*aboutM,*quitM,*bdrM;
GtkWidget *image,*table,*Fileframe,*framecnt;
bool status=false;
void aboutDialog()
{
GtkWidget *box;
box=gtk_message_dialog_new(GTK_WINDOW(window),GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_INFO,GTK_BUTTONS_OK,"EXAMPLE");;
gtk_dialog_run(GTK_DIALOG(box));
gtk_widget_destroy(box);
}
void Warning_quit()
{
GtkWidget *box;
box=gtk_message_dialog_new(GTK_WINDOW(window),GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_WARNING,GTK_BUTTONS_YES_NO,"Sure want to quit..");
gtk_dialog_run(GTK_DIALOG(box));
gtk_widget_destroy(box);
gtk_main_quit();
}
void toggle_Border()
{
if(status==false)
{
gtk_window_set_decorated(GTK_WINDOW(window),true);
gtk_menu_item_set_label(GTK_MENU_ITEM(bdrM),"fix");
status=true;
}
else if(status==true)
{
gtk_window_set_decorated(GTK_WINDOW(window),false);
gtk_menu_item_set_label(GTK_MENU_ITEM(bdrM),"move");
status=false;
}
}
void initDecoration()
{
vbox=gtk_vbox_new(false,0);
menubar=gtk_menu_bar_new();
fileM=gtk_menu_item_new_with_label("file");
aboutM=gtk_menu_item_new_with_label("about");
quitM=gtk_menu_item_new_with_label("quit");
bdrM=gtk_menu_item_new_with_label("move");
menu=gtk_menu_new();
////////////////////////
gtk_container_add(GTK_CONTAINER(window),vbox);//attach vbox with window..
gtk_menu_shell_append(GTK_MENU_SHELL(menubar),fileM);//attach menu into menubar
gtk_menu_item_set_submenu(GTK_MENU_ITEM(fileM),menu);
gtk_menu_shell_append(GTK_MENU_SHELL(menu),aboutM);
gtk_menu_shell_append(GTK_MENU_SHELL(menubar),bdrM);
gtk_menu_shell_append(GTK_MENU_SHELL(menu),quitM);//attach quit menu with menu..
gtk_box_pack_start(GTK_BOX(vbox),menubar,0,0,1);//attach menubar with vbox//
//attach signal intrrupt for menus..
g_signal_connect(G_OBJECT(aboutM),"activate",G_CALLBACK(aboutDialog),0);//signal for about menu..
g_signal_connect(G_OBJECT(quitM),"activate",G_CALLBACK(Warning_quit),0);//signal for quit menu..
g_signal_connect(G_OBJECT(bdrM),"activate",G_CALLBACK(toggle_Border),0);
}
void initTable()
{
GtkWidget *button;
gtk_container_add(GTK_CONTAINER(window),table); //table attached to main window..
button=gtk_button_new_with_label("button");
table=gtk_table_new(4,4,true); //table created..
gtk_table_attach_defaults(GTK_TABLE(table),button,0,1,0,1); //attaching frame container to table cell..
}
int main(int argc, char *argv[])
{
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);
//setup menubar;
initDecoration();
initTable();
g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),0);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
答案 0 :(得分:1)
问题是你试图将多个孩子添加到同一个容器GtkWindow,它只能容纳一个孩子。
你确实创建了一个vbox,但在你没有添加GtkTable之后,GtkTable从Gtk + 3.4开始就被弃用了。
另请注意,您在创建之前将GtkTable添加到窗口,GtkWindow会抱怨这一点。
因此,您的initTable
函数应为:
void initTable()
{
GtkWidget *button;
button=gtk_button_new_with_label("button");
table=gtk_table_new(4,4,true); //table created..
gtk_table_attach_defaults(GTK_TABLE(table),button,0,1,0,1); //attaching frame container to table cell..
gtk_box_pack_start(GTK_BOX(vbox),table,0,0,1);//attach menubar with vbox//
}
为了编译,我们必须定义true
,false
和bool
(由于包含,不确定您使用的是哪个框架)。
完整的源代码是:
/* Button Boxes
*
* The Button Box widgets are used to arrange buttons with padding.
*/
#include <gtk/gtk.h>
#define true TRUE
#define false FALSE
#define bool gboolean
GtkWidget *window,*button,*container,*menubar,*fileM,*menu,*vbox,*aboutM,*quitM,*bdrM;
GtkWidget *image,*table,*Fileframe,*framecnt;
bool status=false;
void aboutDialog()
{
GtkWidget *box;
box=gtk_message_dialog_new(GTK_WINDOW(window),GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_INFO,GTK_BUTTONS_OK,"EXAMPLE");;
gtk_dialog_run(GTK_DIALOG(box));
gtk_widget_destroy(box);
}
void Warning_quit()
{
GtkWidget *box;
box=gtk_message_dialog_new(GTK_WINDOW(window),GTK_DIALOG_DESTROY_WITH_PARENT,GTK_MESSAGE_WARNING,GTK_BUTTONS_YES_NO,"Sure want to quit..");
gtk_dialog_run(GTK_DIALOG(box));
gtk_widget_destroy(box);
gtk_main_quit();
}
void toggle_Border()
{
if(status==false)
{
gtk_window_set_decorated(GTK_WINDOW(window),true);
gtk_menu_item_set_label(GTK_MENU_ITEM(bdrM),"fix");
status=true;
}
else if(status==true)
{
gtk_window_set_decorated(GTK_WINDOW(window),false);
gtk_menu_item_set_label(GTK_MENU_ITEM(bdrM),"move");
status=false;
}
}
void initDecoration()
{
vbox=gtk_vbox_new(false,0);
menubar=gtk_menu_bar_new();
fileM=gtk_menu_item_new_with_label("file");
aboutM=gtk_menu_item_new_with_label("about");
quitM=gtk_menu_item_new_with_label("quit");
bdrM=gtk_menu_item_new_with_label("move");
menu=gtk_menu_new();
////////////////////////
gtk_container_add(GTK_CONTAINER(window),vbox);//attach vbox with window..
gtk_menu_shell_append(GTK_MENU_SHELL(menubar),fileM);//attach menu into menubar
gtk_menu_item_set_submenu(GTK_MENU_ITEM(fileM),menu);
gtk_menu_shell_append(GTK_MENU_SHELL(menu),aboutM);
gtk_menu_shell_append(GTK_MENU_SHELL(menubar),bdrM);
gtk_menu_shell_append(GTK_MENU_SHELL(menu),quitM);//attach quit menu with menu..
gtk_box_pack_start(GTK_BOX(vbox),menubar,0,0,1);//attach menubar with vbox//
//attach signal intrrupt for menus..
g_signal_connect(G_OBJECT(aboutM),"activate",G_CALLBACK(aboutDialog),0);//signal for about menu..
g_signal_connect(G_OBJECT(quitM),"activate",G_CALLBACK(Warning_quit),0);//signal for quit menu..
g_signal_connect(G_OBJECT(bdrM),"activate",G_CALLBACK(toggle_Border),0);
}
void initTable()
{
GtkWidget *button;
button=gtk_button_new_with_label("button");
table=gtk_table_new(4,4,true); //table created..
gtk_table_attach_defaults(GTK_TABLE(table),button,0,1,0,1); //attaching frame container to table cell..
gtk_box_pack_start(GTK_BOX(vbox),table,0,0,1);//attach menubar with vbox//
}
int main(int argc, char *argv[])
{
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);
//setup menubar;
initDecoration();
initTable();
g_signal_connect(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),0);
gtk_widget_show_all(window);
gtk_main();
return 0;
}
编译:
gcc -o main main.c `pkg-config --cflags --libs gtk+-3.0`