Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6356723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T23:01:24+00:00 2026-05-24T23:01:24+00:00

#include <stdio.h> #include <stdlib.h> #include <gtk/gtk.h> /*Devo fare un pulsante che, una volta premuto,

  • 0
#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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T23:01:26+00:00Added an answer on May 24, 2026 at 11:01 pm

    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.

    GtkWidget * box = gtk_hbox_new(0,0);
    gtk_container_add(GTK_CONTAINER(window),box);
    

    Now add the table and the button to the box instead of to the window.

    gtk_container_add(GTK_CONTAINER(box), table);
    gtk_container_add(GTK_CONTAINER(box), button);
    

    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.

    gtk_container_add(GTK_CONTAINER(box), button);
    gtk_table_attach(GTK_TABLE(table), button, 1, 5, 2, 3, GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 0, 0);
    

    I thought it would be interesting (and perhaps helpful) to write the callback too.

    void callback()
       {
       const gchar * s1 = gtk_entry_get_text(GTK_ENTRY(num1));
       const gchar * s2 = gtk_entry_get_text(GTK_ENTRY(num2));
       int n1 = atoi(s1), n2=atoi(s2);
       n1=abs(n1), n2=abs(n2); // GCD defined to return non-negative value
       int i; for ( i=(n1>n2?n1:n2); i>=1; i-- ) if ( n1/i*i==n1 && n2/i*i==n2 ) break;
       char s3[32]; snprintf(s3,32,"%d",i);
       gtk_entry_set_text(GTK_ENTRY(mcdt),s3);
       }
    

    Here’s what it looks like with the hbox. Notice that the button is placed next to the table horizontally.

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

#include <stdio.h> #include <stdlib.h> #include <time.h> void initDeck (int deck[]); void showDeck (int deck[]);
#include<stdio.h> #include<string.h> #include<stdlib.h> int main() { // int char str[40],ch; FILE*fp,*fp1,*fp2; fp=fopen(ide_input,w); fp1=fopen(error_log,w); fp2=fopen(lex_output,w);
#include<stdio.h> #include<signal.h> #include<stdlib.h> void handler(int signo) { printf(First statement); system(date); exit(EXIT_SUCCESS); } int main()
My C code #include <stdio.h> #include <stdlib.h> #include help.h int test(int x, P *ut)
Test the following code: #include <stdio.h> #include <stdlib.h> main() { const char *yytext=0; const
#include <string.h> #include <stdlib.h> #include <stdio.h> int main(void) { unsigned char *stole; unsigned char
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>
I have a program here: #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <sys/ipc.h> #include
I tried to compile this example: #include <stdio.h> #include <stdlib.h> #include <stddef.h> main(){ size_t
Take the following snippet: 1 #include <stdio.h> 2 #include <stdlib.h> 3 int foo(char [6]);

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.