Attempting to add a widget with type GtkButton to a GtkWindow, but as a GtkBin subclass a GtkWindow can only contain one

Post Reply
User avatar
Administrator
Site Admin
Posts: 1616
Joined: Thu Mar 22, 2018 10:19 am

Attempting to add a widget with type GtkButton to a GtkWindow, but as a GtkBin subclass a GtkWindow can only contain one

Post by Administrator » Thu Dec 06, 2018 11:18 pm

If your getting the following error while trying to run your application, it means you need to pack your widgets.

Code: Select all

Attempting to add a widget with type GtkButton 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 GtkButton

horizontal using GtkGrid

Code: Select all

  #include <gtk/gtk.h>

    int main( int   argc,char *argv[] )
    {
     
       	GtkWidget *window;
       	GtkWidget *button;
       	GtkWidget *button2;
  	GtkWidget *grid;

       	gtk_init (&argc, &argv);
     
       	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
     


       	/* Let's set the border width of the window to 20.
        * You may play with the value and see the
        * difference. */
       	gtk_container_set_border_width
          (GTK_CONTAINER (window), 10);
     
       	button = gtk_button_new_with_label ("Hello World!");
       	button2 = gtk_button_new_with_label ("Hello World2!");
     
     	/* Here we construct the container that is going pack our buttons */
  	grid = gtk_grid_new ();

  	/* Pack the container in the window */
  	gtk_container_add (GTK_CONTAINER (window), grid);


	gtk_grid_attach (GTK_GRID (grid), button, 0, 0, 1, 1);
  	gtk_grid_attach (GTK_GRID (grid), button2, 1, 0, 1, 1);
     

       	gtk_widget_show_all(window); 
     
       	gtk_main ();
     
       	return 0;
    }
vertical using GtkGrid

Code: Select all

  #include <gtk/gtk.h>

     
    int main( int   argc,char *argv[] )
    {
     
       	GtkWidget *window;
       	GtkWidget *button;
       	GtkWidget *button2;
  	GtkWidget *grid;

       	gtk_init (&argc, &argv);
     
       	window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
     


       	/* Let's set the border width of the window to 20.
        * You may play with the value and see the
        * difference. */
       	gtk_container_set_border_width
          (GTK_CONTAINER (window), 10);
     
       	button = gtk_button_new_with_label ("Hello World!");
       	button2 = gtk_button_new_with_label ("Hello World2!");
     
     	/* Here we construct the container that is going pack our buttons */
  	grid = gtk_grid_new ();

  	/* Pack the container in the window */
  	gtk_container_add (GTK_CONTAINER (window), grid);


	gtk_grid_attach (GTK_GRID (grid), button, 0, 0, 1, 1);
  	gtk_grid_attach (GTK_GRID (grid), button2, 0, 1, 1, 1);
     

       	gtk_widget_show_all(window); 
     
       	gtk_main ();
     
       	return 0;
    }
Screenshot from 2018-12-06 23-20-55.png
Example of vertical buttons in GDK+
Screenshot from 2018-12-06 23-20-55.png (9.16 KiB) Viewed 893 times

Reference
https://developer.gnome.org/gtk-tutoria ... /c354.html
Packing Tutorial https://developer.gnome.org/gtk3/stable/ch01s02.html

Post Reply