gtk_entry_get_text无效

时间:2013-02-25 13:14:53

标签: c windows gtk

我在c中使用GTK 2.2。我可以从其中一个条目中获取文本并使用类型转换将其转换为整数,但它不适用于第二个条目。我设置为从第二个条目获取文本的变量由于某种原因保持为0。这是我的代码。 button2_clicked部分无法正常工作,但问题也可能出在button2 g_signal connect中。

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

int prod,prod_amt,amt,amt_holder,tot_amt;

static void button3_clicked(GtkWidget *widget,gpointer window){
    GtkWidget *dialog;

    dialog = gtk_message_dialog_new (GTK_WINDOW (window), GTK_DIALOG_MODAL,GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "The Total amount is %d",tot_amt);
    gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);
}

static void button2_clicked(GtkWidget *widget,gpointer data){
    amt = (int)gtk_entry_get_text(GTK_ENTRY(data));
    prod_amt = prod;
    int products[10];
    products[0] = 15;
    products[1] = 200;
    products[2] = 55;
    products[3] = 30000;
    products[4] = 15000;
    products[5] = 20000;
    products[6] = 20;
    products[7] = 30;
    products[8] = 2500;
    products[9] = 1000;
    if(amt>0 && amt<11)
    amt_holder = products[amt-1];
    tot_amt += amt_holder;
    g_print("%d",tot_amt);
    gtk_entry_set_text(GTK_ENTRY(data),"");
}

static void prod_list (GtkWidget *widget, GtkWidget *window)
{
    GtkWidget *dialog;
    dialog = gtk_message_dialog_new (GTK_WINDOW (window), GTK_DIALOG_MODAL,     GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Kurkure 15,Chocolate Pack 200\n Dove Shampoo 55     Laptop 30,000\n Television 15,000 Netbook 20,000\n Oreo 20 Dark Fantasy 30\n Toaster 2500 Clothes 1000");
    gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
    gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);
}

static void button_clicked(GtkWidget *widget,gpointer data){
    prod = (int)gtk_entry_get_text(GTK_ENTRY(data));
    g_print(prod);
    gtk_entry_set_text(GTK_ENTRY(data),"");
}

static void clear_clicked(GtkWidget *widget,gpointer data){
    gtk_entry_set_text(GTK_ENTRY(data),"");
}

int main(int argc,char* argv) {
    GtkWidget *window,*button,*entry,*table,*dialog,*clearb;
    GtkWidget *entry2,*button2,*listofprod,*button3;
    gtk_init(&argc,&argv);
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    float product[5],no_of_products,amount,total_amount=0,product_holder,amount_holder=0;
    button = gtk_button_new_with_mnemonic("Enter number of products");
    entry = gtk_entry_new();
    button2 = gtk_button_new_with_mnemonic("Enter Product Numbers");
    entry2 = gtk_entry_new();
    table = gtk_table_new(3,3,0);
    clearb = gtk_button_new_with_label("Clear");
    button3 = gtk_button_new_with_mnemonic("Total amount");
    listofprod = gtk_button_new_with_mnemonic("List of Products");
    g_signal_connect(window,"delete-event",G_CALLBACK(gtk_main_quit),NULL);
    g_signal_connect(button,"clicked",G_CALLBACK(button_clicked),entry);
    g_signal_connect(entry,"activate",G_CALLBACK(button_clicked),entry);
    g_signal_connect(listofprod,"clicked",G_CALLBACK(prod_list),window);
    g_signal_connect(clearb,"clicked",G_CALLBACK(clear_clicked),(gpointer)entry);
    g_signal_connect(button2,"clicked",G_CALLBACK(button2_clicked),(gpointer)entry2);
    g_signal_connect(button3,"clicked",G_CALLBACK(button3_clicked),(gpointer)window);
    g_signal_connect(entry2,"activate",G_CALLBACK(button2_clicked),(gpointer)entry2);
    gtk_table_attach(GTK_TABLE(table),button,1,2,0,1,GTK_FILL,GTK_FILL,0,0);
    gtk_table_attach(GTK_TABLE(table),entry,0,1,0,1,GTK_FILL,GTK_FILL,0,0);
    gtk_table_attach(GTK_TABLE(table),entry2,0,1,1,2,GTK_FILL,GTK_FILL,0,0);
    gtk_table_attach(GTK_TABLE(table),button2,1,2,1,2,GTK_FILL,GTK_FILL,0,0);
    gtk_table_attach(GTK_TABLE(table),clearb,2,3,0,1,GTK_FILL,GTK_FILL,0,0);
    gtk_table_attach(GTK_TABLE(table),listofprod,2,3,1,2,GTK_FILL,GTK_FILL,0,0);
    gtk_table_attach(GTK_TABLE(table),button3,0,1,2,3,GTK_FILL,GTK_FILL,0,0);
    gtk_container_add(GTK_CONTAINER(window),table);
    gtk_container_set_border_width(GTK_CONTAINER(window),100);
    gtk_widget_show_all(window);
    gtk_widget_set_size_request(window,310,310);
    gtk_main();
    return 0;
}

我多次检查过这段代码。怎么了?

1 个答案:

答案 0 :(得分:5)

您不能将字符串强制转换为整数。这不是“类型转换”在C中的意思。

您必须使用实际代码进行转换。例如,查找strtoul()函数。