 |
GTK+ Forums Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
|
|
|
| Author |
Message |
|
|
Turtel Familiar Face
Joined: 01 Mar 2010 Posts: 34 Location: India
|
Posted: Tue Mar 09, 2010 6:17 am Post subject: computation in a window |
|
|
I want to take data1 and data2 (both are fraction numbers or integers not text) then do something and print the results in dialog or frame with text.
I am also getting following two errors on compilation and while running how to remove them:-
1.In function ‘enter_callback’:
ex.c:11: warning: assignment discards qualifiers from pointer target type
2.whie running i am getting error Gtk-CRITICAL **: gtk_box_pack: assertion `child->parent == NULL' failed
The code is given below compile it and see. Could anyone please correct me.................
| Code: (C) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
| #include <gtk/gtk.h>
void enter_callback( GtkWidget *widget,
GtkWidget *entry )
{
gchar *entry_text;
entry_text = gtk_entry_get_text(GTK_ENTRY(entry));
printf("Entry contents: %s\n", entry_text);
}
static do_something_on_click(GtkWidget *frame)
{
gint *data1,*data2,data3;
data3=*data1*(*data2);
}
int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *hbox1;
GtkWidget *hbox2;
GtkWidget *frame;
GtkWidget *entry;
GtkWidget *button;
GtkWidget *bbox;
static gint *data1,*data2,data3;
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 10);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_container_set_border_width (GTK_CONTAINER (window), 20);
hbox1 = gtk_hbox_new (FALSE, 5);
gtk_container_add (GTK_CONTAINER (vbox), hbox1);
gtk_box_pack_start (GTK_BOX (vbox), hbox1, FALSE, FALSE, 0);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
entry = gtk_entry_new_with_max_length (50);
gtk_signal_connect(GTK_OBJECT(entry), "activate",
GTK_SIGNAL_FUNC(enter_callback),
entry);
gtk_entry_set_text (GTK_ENTRY (entry), "data1");
gtk_entry_select_region (GTK_ENTRY (entry),
0, GTK_ENTRY(entry)->text_length);
data1 = gtk_entry_get_text(GTK_ENTRY(entry));
gtk_box_pack_start (GTK_BOX (hbox1), entry, FALSE, FALSE, 0);
hbox2 = gtk_hbox_new (FALSE, 5);
gtk_container_add (GTK_CONTAINER (vbox), hbox2);
gtk_box_pack_start (GTK_BOX (vbox), hbox2, FALSE, FALSE, 0);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
entry = gtk_entry_new_with_max_length (50);
gtk_signal_connect(GTK_OBJECT(entry), "activate",
GTK_SIGNAL_FUNC(enter_callback),
entry);
gtk_entry_set_text (GTK_ENTRY (entry), "data2");
gtk_entry_select_region (GTK_ENTRY (entry),
0, GTK_ENTRY(entry)->text_length);
data2 = gtk_entry_get_text(GTK_ENTRY(entry));
gtk_box_pack_start (GTK_BOX (hbox2), entry, FALSE, FALSE, 0);
frame = gtk_frame_new (NULL);
bbox = gtk_hbutton_box_new ();
gtk_container_set_border_width (GTK_CONTAINER (bbox), 5);
gtk_container_add (GTK_CONTAINER (frame), bbox);
gtk_button_box_set_child_size (GTK_BUTTON_BOX (bbox), 85, 20);
button = gtk_button_new_with_label ("Compute");
gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
GTK_SIGNAL_FUNC(do_something_on_click),
GTK_OBJECT (window));
gtk_container_add (GTK_CONTAINER (bbox), button);
gtk_box_pack_start (GTK_BOX (vbox),frame,TRUE,TRUE,0);
frame = gtk_frame_new (NULL);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 10);
gtk_frame_set_label( GTK_FRAME(frame), "Results of the computaion \nto come below in this frame.");
gtk_frame_set_label_align( GTK_FRAME(frame), 0.0, 0.0);
gtk_container_add (GTK_CONTAINER (vbox), frame);
gtk_box_pack_start (GTK_BOX (vbox),frame,TRUE,TRUE,0);
gtk_widget_show_all (window);
gtk_main ();
return(0);
}
/* example-end */ | Added CodeBB -dreblen
Last edited by Turtel on Thu Mar 11, 2010 9:58 am; edited 1 time in total |
|
| Back to top |
|
 |
owl102 GTK+ Geek
Joined: 23 May 2008 Posts: 68
|
Posted: Tue Mar 09, 2010 6:43 am Post subject: Re: computation in a window |
|
|
| Turtel wrote: |
gint *data1,*data2,*data3;
data3=data1*data2; |
You are trying to multiply two pointers here which doesn't make sense.
| Quote: |
gtk_container_add (GTK_CONTAINER (vbox), hbox1);
gtk_box_pack_start (GTK_BOX (vbox), hbox1, FALSE, FALSE, 0);
|
Just use the gtk_box_pack_start() here and remove the gtk_container_add() call. (Same with hbox2) |
|
| Back to top |
|
 |
Turtel Familiar Face
Joined: 01 Mar 2010 Posts: 34 Location: India
|
Posted: Thu Mar 11, 2010 10:07 am Post subject: |
|
|
| I have corrected the pointer issue. But your second suggestion does not work. Non of compilation error have gone.In fact I am wondering whether it is possible to do things that is possible in C programming through a gtk+ GUI? |
|
| Back to top |
|
 |
tadeboro Never Seen the Sunlight
Joined: 23 Jul 2008 Posts: 2114 Location: Slovenia
|
Posted: Thu Mar 11, 2010 1:44 pm Post subject: |
|
|
Hello.
You're using a bunch of deprecated functions, a lot of stuff is duplicated inside your code, some widgets are being added into containers twice, ...
I cleaned your code and made it do some calculations. As you can see, you can do all of the thing that are possible in plain C using GTK+ too (this should come as no surprise, since GTK+ is merely an add-on library like any other).
| Code: (C) | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| #include <gtk/gtk.h>
typedef struct _Data
{
GtkEntry *e1,
*e2;
GtkLabel *res;
}
Data;
static void
calculate( Data *data )
{
gdouble d1 = g_strtod( gtk_entry_get_text( data->e1 ), NULL );
gdouble d2 = g_strtod( gtk_entry_get_text( data->e2 ), NULL );
gchar *result = g_strdup_printf( "Multiplication: %f\n"
"Division: %f\n"
"Sum: %f\n"
"Difference: %f",
d1 * d2, d1 / d2, d1 + d2, d1 - d2 );
gtk_label_set_label( data->res, result );
g_free( result );
}
int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *hbox1;
GtkWidget *hbox2;
GtkWidget *frame;
GtkWidget *entry;
GtkWidget *button;
GtkWidget *bbox;
GtkWidget *label;
Data *data;
data = g_slice_new0( Data );
gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
g_signal_connect( G_OBJECT( window ), "destroy",
G_CALLBACK( gtk_main_quit ), NULL );
gtk_container_set_border_width (GTK_CONTAINER (window), 20);
vbox = gtk_vbox_new (FALSE, 0);
gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
gtk_container_add (GTK_CONTAINER (window), vbox);
hbox1 = gtk_hbox_new (FALSE, 5);
gtk_box_pack_start (GTK_BOX (vbox), hbox1, FALSE, FALSE, 0);
entry = gtk_entry_new();
gtk_entry_set_max_length( GTK_ENTRY( entry ), 50 );
gtk_entry_set_text (GTK_ENTRY (entry), "3");
gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
gtk_box_pack_start (GTK_BOX (hbox1), entry, FALSE, FALSE, 0);
data->e1 = GTK_ENTRY( entry );
hbox2 = gtk_hbox_new (FALSE, 5);
gtk_box_pack_start (GTK_BOX (vbox), hbox2, FALSE, FALSE, 0);
entry = gtk_entry_new();
gtk_entry_set_max_length( GTK_ENTRY( entry ), 50 );
gtk_entry_set_text (GTK_ENTRY (entry), "2");
gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
gtk_box_pack_start (GTK_BOX (hbox1), entry, FALSE, FALSE, 0);
data->e2 = GTK_ENTRY( entry );
frame = gtk_frame_new (NULL);
gtk_box_pack_start (GTK_BOX (vbox),frame,TRUE,TRUE,0);
bbox = gtk_hbutton_box_new ();
gtk_container_set_border_width (GTK_CONTAINER (bbox), 5);
gtk_container_add (GTK_CONTAINER (frame), bbox);
button = gtk_button_new_with_label ("Compute");
g_signal_connect_swapped (G_OBJECT (button), "clicked",
G_CALLBACK( calculate ), data);
gtk_container_add (GTK_CONTAINER (bbox), button);
frame = gtk_frame_new (NULL);
gtk_frame_set_label_align( GTK_FRAME(frame), 0.0, 0.0);
gtk_box_pack_start (GTK_BOX (vbox),frame,TRUE,TRUE,0);
label = gtk_label_new( "Results of calculation appear here" );
gtk_container_add( GTK_CONTAINER( frame ), label );
data->res = GTK_LABEL( label );
gtk_widget_show_all( window );
gtk_main ();
return(0);
}
/* example-end */ |
Tadej |
|
| Back to top |
|
 |
|
Powered by phpBB © 2001, 2005 phpBB Group CodeBB 1.0 Beta 2
|