C语言 尝试将GtkBox类型的小部件添加到GtkWindow

b4lqfgs4  于 2023-06-05  发布在  其他
关注(0)|答案(1)|浏览(490)
#include <gtk/gtk.h>
#include <stdbool.h>

int main ( int argc, char *argv[] )
{
  GtkWidget   *page;
  GtkWidget   *vbox;
  GtkWidget   *toolbar;
  GtkWidget   *Label1;

  GtkToolItem *new;
  GtkToolItem *open;
  GtkToolItem *save;
  GtkToolItem *exit;
  GtkToolItem *setting;
  GtkToolItem *about;

  void NEW_PROJECT ();
  void EXIT_FROM_SOFTWARE ();

  gtk_init ( &argc, &argv );

  /* Window configure */
  page = gtk_window_new ( GTK_WINDOW_TOPLEVEL );
  gtk_window_set_title        ( GTK_WINDOW(page), "Stack over flow" );
  gtk_window_set_default_size ( GTK_WINDOW(page), 800, 500 );
  gtk_window_set_resizable    ( GTK_WINDOW(page), FALSE );
  g_signal_connect            ( G_OBJECT (page), "destroy", G_CALLBACK (gtk_main_quit), NULL );
  /* Window configure */

  GtkWidget *button;
  button=gtk_frame_new ("An example of different info bars");
  /* Add the button to window */
  gtk_container_add(GTK_CONTAINER(page),button);

  /* Toolbar configure */
  vbox = gtk_box_new            ( TRUE, 0 );
  gtk_container_add             ( GTK_CONTAINER (page), vbox );
  toolbar = gtk_toolbar_new     ();
  gtk_toolbar_set_style         ( GTK_TOOLBAR (toolbar), GTK_TOOLBAR_ICONS );
  new = gtk_tool_button_new     ( gtk_image_new_from_icon_name ("tab-new-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR), NULL );
  gtk_toolbar_insert            ( GTK_TOOLBAR (toolbar), new, -1 );
  open = gtk_tool_button_new    ( gtk_image_new_from_icon_name ("emblem-default-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR), NULL );
  gtk_toolbar_insert            ( GTK_TOOLBAR (toolbar), open, -1 );
  save = gtk_tool_button_new    ( gtk_image_new_from_icon_name ("user-trash-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR), NULL );
  gtk_toolbar_insert            ( GTK_TOOLBAR (toolbar), save, -1 );
  gtk_toolbar_insert            ( GTK_TOOLBAR (toolbar), gtk_separator_tool_item_new(), -1 );
  about = gtk_tool_button_new   ( gtk_image_new_from_icon_name ("mail-mark-junk-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR), NULL );
  gtk_toolbar_insert            ( GTK_TOOLBAR (toolbar), about, -1 );
  setting = gtk_tool_button_new ( gtk_image_new_from_icon_name ("applications-system-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR), NULL );
  gtk_toolbar_insert            ( GTK_TOOLBAR (toolbar), setting, -1 );
  gtk_toolbar_insert            ( GTK_TOOLBAR (toolbar), gtk_separator_tool_item_new(), -1 );
  exit = gtk_tool_button_new    ( gtk_image_new_from_icon_name ("edit-delete-symbolic", GTK_ICON_SIZE_SMALL_TOOLBAR), NULL );
  gtk_toolbar_insert            ( GTK_TOOLBAR (toolbar), exit, -1 );
  gtk_box_pack_start            ( GTK_BOX (vbox), toolbar, FALSE, FALSE, 5 );
  //Signal Connection to the widghts
  g_signal_connect              ( G_OBJECT (exit), "clicked",
        G_CALLBACK              ( EXIT_FROM_SOFTWARE), NULL );
  g_signal_connect              ( G_OBJECT (new), "clicked",
        G_CALLBACK              ( NEW_PROJECT), NULL );
  /* Toolbar configure */


  gtk_widget_show_all           ( page );

  gtk_main                      ();
  return 0;
}

void NEW_PROJECT () {}
void EXIT_FROM_SOFTWARE () {}

我有GTKButtongtk_box_new,但当我运行代码时,只显示其中一个。
编译器报告:

$ gcc Setup.c -o Setup  `pkg-config --cflags --libs gtk+-3.0` -rdynamic && ./Setup 

(Setup:3937): Gtk-WARNING **: 22:32:11.813: Attempting to add a widget with type GtkBox to a GtkWindow, but as a GtkBin subclass a GtkWindow can only contain one widget at a time; it already contains a widget of type GtkFrame

你可以看到:

但是当我从源代码中删除这个块时:

GtkWidget *button;
button=gtk_frame_new ("An example of different info bars");
gtk_container_add(GTK_CONTAINER(page), button);

软件运行如下:

据我所知,库不能同时显示两个小部件。那么在这种情况下,如何制作复杂的软件呢?也许我没有正确地编写代码,但在这种情况下,编译器会给予错误。我搜索了互联网,但没有找到类似的项目在您的网站上没有类似的情况下,在谷歌解决我的问题。
我在Fedora Gnome中使用GTK3。

np8igboo

np8igboo1#

据我所知,库不能同时显示两个小部件。
不对。唯一的限制是GtkBin一次不能 * 包含 * 多个widget。
我们在这里看到:

page = gtk_window_new ( GTK_WINDOW_TOPLEVEL );

pageGtkWindow,它是GtkBin的子类。
button添加到page

gtk_container_add(GTK_CONTAINER(page),button);

然后尝试将vbox添加到page

gtk_container_add( GTK_CONTAINER (page), vbox );

这将导致错误消息:

Attempting to add a widget with type GtkBox to a GtkWindow, but as a GtkBin subclass a GtkWindow can only contain one widget at a time; it already contains a widget of type GtkFrame

相反,尝试将button添加到vbox,而不是直接添加到page,因为GtkBox可以同时容纳多个项目,但GtkWindow不能。

相关问题