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 8479037
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:54:51+00:00 2026-06-10T18:54:51+00:00

How do you get the text from a gtk entry widget and then convert

  • 0

How do you get the text from a gtk entry widget and then convert that to an integer value. Notice in my code I include a wrapper struct called Window that contains pointers to widgets. In the main, I declare an instance of a Window and then build the correct widgets with the appropriate GTK function calls. I then pass that window object to the function that handles the clicked action. I want to then calculate the numerator divided by the denominator in integer format. Below is my attempt. All the code works except for the button_clicked function. Any ideas?

#include <gtk/gtk.h>
#include <stdlib.h>

struct Window
{
    GtkWidget *numerator;
    GtkWidget *denominator;
    GtkWidget *button;
    GtkWidget *label;
};


void button_clicked(GtkWidget *widget, gpointer data)
{
    Window* w = (Window*)data;
    char buf[10];

    char buffer[200];

    GtkEntry* e = (GtkEntry*)w->numerator;
    const gchar* entry1 = gtk_entry_get_text(e);

    char* test = (char*)gchar;
    int r = atoi(test);


    sprintf(buf,"%d",r);

    GtkWidget *label = w->label;
    gtk_label_set_text(GTK_LABEL(label), buf);
}


int main(int argc, char*argv[])
{
    GtkWidget *window;
    GtkWidget *table;
    Window w;


    //Set up my window
    gtk_init(&argc,&argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Division");
    gtk_window_set_default_size(GTK_WINDOW(window),500,500);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);

    //Create my table and add it to the window
    table = gtk_table_new(4,2,FALSE);
    gtk_container_add(GTK_CONTAINER(window),table);

    //Create instances of all my widgets
    w.numerator = gtk_entry_new();
    w.denominator = gtk_entry_new();
    w.button = gtk_button_new_with_label("Click");
    w.label = gtk_label_new("result");

    //Attack the widgets to the table
    gtk_table_attach(GTK_TABLE(table), w.numerator,0,1,0,1,GTK_FILL,GTK_FILL,5,5);
    gtk_table_attach(GTK_TABLE(table), w.denominator,0,1,1,2,GTK_FILL,GTK_FILL,5,5);
    gtk_table_attach(GTK_TABLE(table), w.button,0,1,2,3,GTK_FILL,GTK_FILL,5,5);
    gtk_table_attach(GTK_TABLE(table), w.label,0,1,3,4,GTK_FILL,GTK_FILL,5,5);

    //attach the click action to with the button to invoke the button_clicked function
    g_signal_connect(G_OBJECT(w.button),"clicked",G_CALLBACK(button_clicked),&w);   
      g_signal_connect_swapped(G_OBJECT(window),"destroy",G_CALLBACK(gtk_main_quit),NULL);

    gtk_widget_show_all(window);


    gtk_main();

    return 0;
}
  • 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-06-10T18:54:52+00:00Added an answer on June 10, 2026 at 6:54 pm

    If I see this correctly, in your “test code” all you’re trying to do is set the label string to the contents of “w->numerator”, right?

    The line

    char* test = (char*)gchar;
    

    looks fishy to me and doesn’t even compile, it looks like a typo.
    Change the “gchar” to “entry1”, and it should do what you want it to.

    I have a recommendation for you though: use GtkSpinButton instead of GtkEntry. It is like a custom Entry made for numerical values, and the retrieval of such is many times easier.

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

Sidebar

Related Questions

I'm using code like this to get text from off the Clipboard. Dim DataObj
I try to get text from website that I show in a textview. I
I have the following code snippet I'm trying to get the text from: <span
I am creating an application that can get text from the selected rectangle on
I have a gtk.Notebook and i want get text of label from current gtk.noteBook
I have this code: #include <gtkmm.h> #include <unistd.h> void send_msg(Gtk::Entry* entry, int* fd); Glib::ustring*
I want to get text from literal that is inside a gridview, i used
I want to get text from literal that is inside a gridview, But when
is it possible to get text from images with tesseract asynchronously? id love to
I want to get text from current active line (the line where the caret

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.