GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Sep 20, 2014 3:57 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: GTK 3 simple program problem
PostPosted: Thu Sep 26, 2013 5:19 pm 
Offline

Joined: Thu Sep 26, 2013 4:25 pm
Posts: 2
Hi! I am doing a simple GTK 3 program but it give the following error:

Code:
Gtk-CRITICAL **: gtk_entry_get_text: assertion `GTK_IS_ENTRY (entry)' failed


There program is very simple just I want when you push the button set text on the entry box and also to the terminal.

Here is the program:

Code:
#include <gtk/gtk.h>

static void
print_hello (GtkWidget *widget, GtkWidget *name,
gpointer data)
{
g_print ("Hello World\n");
const gchar *entry_text1;
entry_text1 = gtk_entry_get_text (GTK_ENTRY (name));
g_print ("Contents of entries:\n%s", entry_text1);
}

static void
print_hello2 (GtkWidget *name)
{
gtk_entry_set_text (GTK_ENTRY (name), "hello");
//g_print ("Hello World\n");
}


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

/* This is called in all GTK applications. Arguments are parsed
* from the command line and are returned to the application.
*/
gtk_init (&argc, &argv);

/* create a new window, and set its title */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Grid");
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);

/* 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);

button = gtk_button_new_with_label ("Button 1");
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);

/* Place the first button in the grid cell (0, 0), and make it fill
* just 1 cell horizontally and vertically (ie no spanning)
*/
gtk_grid_attach (GTK_GRID (grid), button, 0, 0, 1, 1);

button = gtk_button_new_with_label ("Button 2");
g_signal_connect_swapped (button, "clicked", G_CALLBACK (print_hello2), NULL);

/* Place the second button in the grid cell (1, 0), and make it fill
* just 1 cell horizontally and vertically (ie no spanning)
*/
gtk_grid_attach (GTK_GRID (grid), button, 1, 0, 1, 1);

name = gtk_entry_new ();
//gtk_entry_set_text (GTK_ENTRY (name), "hello");
gtk_grid_attach (GTK_GRID (grid), name, 0, 1, 2, 1);

button = gtk_button_new_with_label ("Quit");
g_signal_connect (button, "clicked", G_CALLBACK (gtk_main_quit), NULL);

/* Place the Quit button in the grid cell (0, 1), and make it
* span 2 columns.
*/
gtk_grid_attach (GTK_GRID (grid), button, 0, 2, 2, 1);

/* Now that we are done packing our widgets, we show them all
* in one go, by calling gtk_widget_show_all() on the window.
* This call recursively calls gtk_widget_show() on all widgets
* that are contained in the window, directly or indirectly.
*/
gtk_widget_show_all (window);

/* All GTK applications must have a gtk_main(). Control ends here
* and waits for an event to occur (like a key press or a mouse event),
* until gtk_main_quit() is called.
*/
gtk_main ();

return 0;
}

compile as:
Code:
gcc `pkg-config --cflags gtk+-3.0` -o hello hello.c `pkg-config --libs gtk+-3.0`

Thanks


Top
 Profile  
 
 Post subject: Re: GTK 3 simple program problem
PostPosted: Thu Sep 26, 2013 5:52 pm 
Offline
GTK+ Geek

Joined: Fri Sep 04, 2009 6:53 am
Posts: 68
Code:
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), NULL);


should be

Code:
g_signal_connect (button, "clicked", G_CALLBACK (print_hello), name);
(after you have initialized name


Top
 Profile  
 
 Post subject: Re: GTK 3 simple program problem
PostPosted: Thu Sep 26, 2013 8:48 pm 
Offline

Joined: Thu Sep 26, 2013 4:25 pm
Posts: 2
Thanks! I did try that before after searching on the net but it was not working

I did modified now and move "name = gtk_entry_new ()" before those g_signal_connect. and its working now, thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group