#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
/*Devo fare un pulsante che, una volta premuto, legga
* i due numeri e ne calcoli l'MCD*/
int main(int argc, char *argv[]){
GtkWidget *window;
GtkWidget *table;
GtkWidget *label1;
GtkWidget *label2;
GtkWidget *label3;
GtkWidget *num1;
GtkWidget *num2;
GtkWidget *mcdt;
GtkWidget *button;
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_title(GTK_WINDOW(window), "MCD Calculator");
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
table = gtk_table_new(4, 4, FALSE);
gtk_container_add(GTK_CONTAINER(window), table);
label1 = gtk_label_new("Num1");
label2 = gtk_label_new("Num2");
label3 = gtk_label_new("MCD");
gtk_table_attach(GTK_TABLE(table), label1, 0, 1, 0, 1, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK,5, 5);
gtk_table_attach(GTK_TABLE(table), label2, 0, 1, 1, 2, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK,5, 5);
gtk_table_attach(GTK_TABLE(table), label3, 0, 1, 3, 4, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK,5, 5);
num1 = gtk_entry_new();
num2 = gtk_entry_new();
mcdt = gtk_entry_new();
gtk_table_attach(GTK_TABLE(table), num1, 1, 2, 0, 1, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
gtk_table_attach(GTK_TABLE(table), num2, 1, 2, 1, 2, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
gtk_table_attach(GTK_TABLE(table), mcdt, 1, 2, 3, 4, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
button = gtk_button_new_with_label ("Calcola MCD");
gtk_container_add(GTK_CONTAINER(window), button);
gtk_table_attach(GTK_TABLE(table), button, 1, 5, 2, 3, GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 0, 0);
//g_signal_connect (button, "clicked", G_CALLBACK (callback), (gpointer) "Calcola");
gtk_widget_show_all(window);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
return EXIT_SUCCESS;
}
This code give me this warning:
*Gtk-WARNING **: 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 GtkTable*
I’ve read that I have to use vbox or hbox, but I didn’t understand how to use them.
A box widget allows you to add multiple sub-widgets, arranged either horizontally or vertically. The table widget is similar, but it allows for a 2-dimensional grid layout. In other words, hbox and vbox are just simplified versions of the table that you are already using.
To use a box, just create an hbox or a vbox and add it to the window.
Now add the table and the button to the box instead of to the window.
In the code that you posted you are adding the button to two different containers. You should disable one of the following lines. If you disable the line with gtk_container_add(), then you don’t actually need to use a box, since you are adding the button to the existing table.
I thought it would be interesting (and perhaps helpful) to write the callback too.
Here’s what it looks like with the hbox. Notice that the button is placed next to the table horizontally.