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 Aug 02, 2014 12:24 am

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Working with GTK entry
PostPosted: Fri Aug 16, 2013 10:28 am 
Offline
Familiar Face

Joined: Mon Mar 01, 2010 3:21 pm
Posts: 37
Location: India
What is wrong with that programme it is not commputing nether it is writting anything on the file could anybody help please-------

Code:
#include <glib/gstdio.h>
#include <gtk/gtk.h>

typedef struct _Data
{
GtkEntry *e1,
*e2;
}
Data;

static void
calculate( Data *data )
{ FILE *Purchaselist= g_fopen("Purchaselist","w");
gdouble d2 = g_strtod( gtk_entry_get_text( data->e2 ), NULL );
gdouble  d1 = g_strtod( gtk_entry_get_text( data->e1 ), NULL );
gdouble d3=d1*d2;
g_print("Vegetables puchased for %d\n",d3);
fprintf(Purchaselist,"Vegetables puchased for %d\n",d3);

}

int main( int argc,
char *argv[] )
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *frame;
GtkWidget *entry;
GtkWidget *button;
GtkWidget *bbox;
GtkWidget *label;
Data *data;
GtkWidget *fixed;
data = g_slice_new0( Data );

gtk_init (&argc, &argv);
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Vegetables Purchased");
gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
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);

frame = gtk_frame_new (NULL);
gtk_container_add(GTK_CONTAINER(vbox), frame);

fixed = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(frame), fixed);

label = gtk_label_new ("Tomatoes rate per Kg ");
gtk_fixed_put(GTK_FIXED(fixed), label, 15, 55);
entry = gtk_entry_new();
gtk_entry_set_max_length( GTK_ENTRY( entry ), 50 );
gtk_entry_set_text (GTK_ENTRY (entry), "15 ");
gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
data->e1 = GTK_ENTRY( entry );
gtk_fixed_put(GTK_FIXED(fixed), entry, 250, 50);

label = gtk_label_new ("Tomatoes in kgs");
gtk_fixed_put(GTK_FIXED(fixed), label, 15, 85);
entry = gtk_entry_new();
gtk_entry_set_max_length( GTK_ENTRY( entry ), 50 );
gtk_entry_set_text (GTK_ENTRY (entry), "1000");
gtk_editable_select_region (GTK_EDITABLE (entry), 0, -1);
data->e2 = GTK_ENTRY( entry );
gtk_fixed_put(GTK_FIXED(fixed), entry, 250, 80);

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_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_SPREAD);
gtk_container_add (GTK_CONTAINER (frame), bbox);

button = gtk_button_new_with_label ("Find cost");
g_signal_connect_swapped (G_OBJECT (button), "clicked",
G_CALLBACK( calculate ), data);
gtk_container_add (GTK_CONTAINER (bbox), button);

gtk_widget_show_all( window );
gtk_main ();

return(0);
}


Top
 Profile  
 
 Post subject: Re: Working with GTK entry
PostPosted: Sat Aug 17, 2013 6:12 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 734
Location: UK
Hi,

Which version of GTK+ are you using?

This code is very very similar to some that you posted about 3 years ago.

You are making the code too overly complicated. First you are using containers within containers within containers when it is not needed. You are also using GtkFixed which should only be used in special cases and this is NOT one of those cases.

_________________
E.


Top
 Profile  
 
 Post subject: Re: Working with GTK entry
PostPosted: Sun Aug 18, 2013 10:33 am 
Offline
Familiar Face

Joined: Mon Mar 01, 2010 3:21 pm
Posts: 37
Location: India
yes errol at that time this code was working now it is not..........GTK 2 and GTK+...........


Top
 Profile  
 
 Post subject: Re: Working with GTK entry
PostPosted: Wed Aug 21, 2013 8:21 am 
Offline
Never Seen the Sunlight

Joined: Mon Apr 28, 2008 5:52 am
Posts: 734
Location: UK
Your code has many errors, with the small ones causing you the big problems

Global structure is named as _Data. In ANSI C any variable, structure name, etc. beginning with a underscore is reserved for the standard library and not for application use.

Function calculate() :
- File is opened but never closed. Can cause memory leaks, running out of file handles etc. also means the buffer is never flushed to the file system
- Incorrect conversion type is used. %d is used which is to convert an integer, but a floating point number is passed you should use something like %f instead. This is the cause of original error (one character difference)

Function main() :
- Having the window set as a dialog makes it hard to close the application down without a menu item or button that can also do this
- You have containers within containers within containers which make this simple example very complex

As another note you are doing financial calculations and I would advise you to use fixed point arithmetic and not floating point arithmetic which can have unpredictable rounding errors. If you are only dealing with small to medium quantities and your code is not being used for financial critical stuff then you can remain using floating point.

Below is a correction of your code (works with GTK+ 2.22 and above)
Code:
#include <glib/gstdio.h>
#include <gtk/gtk.h>

typedef struct Data {
    GtkEntry *e1, *e2;
} Data;

static void calculate(Data * data)
{
    FILE *Purchaselist = g_fopen("Purchaselist", "w");
    gdouble d2 = g_strtod(gtk_entry_get_text(data->e2), NULL);
    gdouble d1 = g_strtod(gtk_entry_get_text(data->e1), NULL);
    gdouble d3 = d1 * d2;
    g_print("Vegetables puchased for %f\n", d3);
    fprintf(Purchaselist, "Vegetables puchased for %f\n", d3);
    fclose(Purchaselist);
}

int main(int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *entry;
    GtkWidget *button;
    GtkWidget *bbox;
    GtkWidget *label;
    Data *data;
    GtkWidget *table;

    gtk_init(&argc, &argv);

    data = g_slice_new0(Data);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Vegetables Purchased");

    g_signal_connect(G_OBJECT(window), "destroy",
                     G_CALLBACK(gtk_main_quit), NULL);
    gtk_container_set_border_width(GTK_CONTAINER(window), 20);

    table = gtk_table_new(3, 2, FALSE);
    gtk_container_set_border_width(GTK_CONTAINER(table), 5);
    gtk_container_add(GTK_CONTAINER(window), table);

    label = gtk_label_new("Tomatoes rate per Kg ");
    gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 0, 1);
    entry = gtk_entry_new();
    gtk_entry_set_max_length(GTK_ENTRY(entry), 50);
    gtk_entry_set_text(GTK_ENTRY(entry), "15 ");
    gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
    data->e1 = GTK_ENTRY(entry);
    gtk_table_attach_defaults(GTK_TABLE(table), entry, 1, 2, 0, 1);

    label = gtk_label_new("Tomatoes in kgs");
    gtk_table_attach_defaults(GTK_TABLE(table), label, 0, 1, 1, 2);
    entry = gtk_entry_new();
    gtk_entry_set_max_length(GTK_ENTRY(entry), 50);
    gtk_entry_set_text(GTK_ENTRY(entry), "1000");
    gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
    data->e2 = GTK_ENTRY(entry);
    gtk_table_attach_defaults(GTK_TABLE(table), entry, 1, 2, 1, 2);

    bbox = gtk_hbutton_box_new();
    gtk_container_set_border_width(GTK_CONTAINER(bbox), 5);
    gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_SPREAD);
    gtk_table_attach_defaults(GTK_TABLE(table), bbox, 0, 2, 2, 3);

    button = gtk_button_new_with_label("Find cost");
    g_signal_connect_swapped(G_OBJECT(button), "clicked",
                             G_CALLBACK(calculate), data);
    gtk_container_add(GTK_CONTAINER(bbox), button);

    gtk_widget_show_all(window);
    gtk_main();

    return (0);
}


This is the code for GTK+ 3 and above for those interested and if you want to upgrade to GTK+ v3
Code:
#include <glib/gstdio.h>
#include <gtk/gtk.h>

typedef struct Data {
    GtkEntry *e1, *e2;
} Data;

static void calculate(Data * data)
{
    FILE *Purchaselist = g_fopen("Purchaselist", "w");
    gdouble d2 = g_strtod(gtk_entry_get_text(data->e2), NULL);
    gdouble d1 = g_strtod(gtk_entry_get_text(data->e1), NULL);
    gdouble d3 = d1 * d2;
    g_print("Vegetables puchased for %f\n", d3);
    fprintf(Purchaselist, "Vegetables puchased for %f\n", d3);
    fclose(Purchaselist);
}

int main(int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *entry;
    GtkWidget *button;
    GtkWidget *bbox;
    GtkWidget *label;
    Data *data;
    GtkWidget *grid;

    gtk_init(&argc, &argv);

    data = g_slice_new0(Data);

    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window), "Vegetables Purchased");

    g_signal_connect(G_OBJECT(window), "destroy",
                     G_CALLBACK(gtk_main_quit), NULL);
    gtk_container_set_border_width(GTK_CONTAINER(window), 20);

    grid = gtk_grid_new();
    gtk_container_set_border_width(GTK_CONTAINER(grid), 5);
    gtk_container_add(GTK_CONTAINER(window), grid);

    label = gtk_label_new("Tomatoes rate per Kg ");
    gtk_grid_attach(GTK_GRID(grid), label, 0, 0, 1, 1);
    entry = gtk_entry_new();
    gtk_entry_set_max_length(GTK_ENTRY(entry), 50);
    gtk_entry_set_text(GTK_ENTRY(entry), "15 ");
    gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
    data->e1 = GTK_ENTRY(entry);
    gtk_grid_attach(GTK_GRID(grid), entry, 1, 0, 1, 1);

    label = gtk_label_new("Tomatoes in kgs");
    gtk_grid_attach(GTK_GRID(grid), label, 0, 1, 1, 1);
    entry = gtk_entry_new();
    gtk_entry_set_max_length(GTK_ENTRY(entry), 50);
    gtk_entry_set_text(GTK_ENTRY(entry), "1000");
    gtk_editable_select_region(GTK_EDITABLE(entry), 0, -1);
    data->e2 = GTK_ENTRY(entry);
    gtk_grid_attach(GTK_GRID(grid), entry, 1, 1, 1, 1);

    bbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
    gtk_container_set_border_width(GTK_CONTAINER(bbox), 5);
    gtk_button_box_set_layout(GTK_BUTTON_BOX(bbox), GTK_BUTTONBOX_SPREAD);
    gtk_grid_attach(GTK_GRID(grid), bbox, 0, 2, 2, 2);

    button = gtk_button_new_with_label("Find cost");
    g_signal_connect_swapped(G_OBJECT(button), "clicked",
                             G_CALLBACK(calculate), data);
    gtk_container_add(GTK_CONTAINER(bbox), button);

    gtk_widget_show_all(window);
    gtk_main();

    return (0);
}

_________________
E.


Top
 Profile  
 
 Post subject: Re: Working with GTK entry
PostPosted: Mon Sep 02, 2013 9:13 am 
Offline
Familiar Face

Joined: Mon Mar 01, 2010 3:21 pm
Posts: 37
Location: India
Thanks Errol very much...........


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

All times are UTC


Who is online

Users browsing this forum: No registered users and 8 guests


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