如何发送多个entrys到功能 - GTK [英] How to send multiple entrys to function - GTK

查看:132
本文介绍了如何发送多个entrys到功能 - GTK的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经开发了一个在C金融计算器 - 黑色&安培;斯科尔斯。我的老师希望我实现一个GUI来此,他建议GTK。由于(从一开始java程序员c)中我可怜的知识,这几乎是太多了,我的那一刻,因为截止时间为4小时。但经过艰苦的工作和良好的帮助,在这里我计算器觉得我有chanche使它!但现在有一个刺激性的障碍在此改变 -
这是通过多个条目的方法。简而言之 - 有五个inputfields和我要的是发送所有这些通过函数g_signal_connect_swapped到被调用的字符串我提取的方法。事情是,我能够做到这一点(感谢帮助),只要有只有一个字符串(一条)。但我怎么所有这些entrys发送到功能?

I have developed a financial calculator in c - Black & Scholes. My teacher wants me to implement a GUI to this and he suggested GTK. Due to my poor knowledge in c (java programmer from the start) this is almost too much for me at the moment because the deadline is in 4 hours. But after hard work and good help here on stackoverflow I think I have a chanche to make it!. But right now there is one irritating obstacle to overide - That is to pass more than one entry to a method. In short - there are five inputfields and what I want is to send all these via g_signal_connect_swapped to a method where the invoked string i extracted. The thing is that I am able to do this (thanks to help) as long as there are only one string (one entry). But how do I send all these entrys to the function?

我曾尝试申报矢量没有成功。它甚至不会编译由于错误。

I have tried to declare a vector without success. It wont even compile due to errors.

 static GtkWidget entry[5]

由于数组是一个指针(第一个元素),它应该工作 - 但不是

Since an array is a pointer (to the first element) it should work - but not!

和更奇怪 - 我有另一个想法 - 而不是发送entrys我试图提取主法的字符串值 - exaxtly相同的语法在功能 - 但没有更迭。该值为null。

And more strange - I had another idea - instead of sending the entrys I tried to extract the string value in the main-method - exaxtly the same syntax as in the function - but without succes. The value is null.

  const gchar *text;
  text = gtk_entry_get_text(GTK_ENTRY (entry_a));
  printf ("Result: %s\n", text);

上面的片段可以作为expexted在功能 - 但不是在主功能。为什么???

希望你明白我的问题 - 在这里沿用了code产生的GUI。在code是没有错误 - 这是我发送一个入门价值的回调函数

Hope you understand my question - here follows the code that generates the GUI. The code is without error - that is where I send ONE entry-value to the callback-function.

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


 static GtkWidget *asset_label;
 static GtkWidget *frame;
 static GtkWidget *entry_a, *entry_s, *entry_v, *entry_t, *entry_r;
 static GtkWidget *label_a, *label_s, *label_v, *label_t, *label_r;
 static GtkWidget *window, *result_label, *button;
 static GtkWidget *table;

 static void entry_Submit(GtkWidget *entry, GtkWidget *widget) {

 const gchar *text;
 text = gtk_entry_get_text(GTK_ENTRY (entry_a));
 printf ("Result: %s\n", text);

 gtk_widget_destroy(GTK_WIDGET(label_a));

 label_a = gtk_label_new (text);
 gtk_grid_attach (GTK_GRID (table), label_a, 1, 0, 1, 1);

 gtk_widget_show(label_a);

 }


 static void destroy(GtkWidget *widget, gpointer data) {
     gtk_main_quit ();
 }



 static void initialize_window(GtkWidget* window) {
   gtk_window_set_title(GTK_WINDOW(window),"My Window"); //Set window title
   gtk_window_set_default_size (GTK_WINDOW (window), 400, 200); //Set default size for the window
   g_signal_connect (window, "destroy", G_CALLBACK (destroy), NULL); //End application when close button clicked

  }

 int main (int argc, char *argv[]) {
   //GtkWidget *window,*table,*label, *button;
   gtk_init(&argc, &argv);


   //Create the main window
   //window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   //initialize_window(window);
     window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
      gtk_widget_set_size_request (GTK_WIDGET (window), 300, 300);
      gtk_window_set_title (GTK_WINDOW (window), "FINANCIAL CALCULATOR");
      g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
      g_signal_connect_swapped (window, "delete-event", G_CALLBACK (gtk_widget_destroy),    window);



    /* Create a 1x2 table */
    table = gtk_grid_new ();
    gtk_container_add (GTK_CONTAINER (window), table);

    //create a text box (asset price)
    entry_a = gtk_entry_new ();
    gtk_grid_attach (GTK_GRID (table), entry_a, 0, 0, 1, 1);

    // create a new label.
    label_a = gtk_label_new (" ASSET PRICE" );
    gtk_grid_attach (GTK_GRID (table), label_a, 1, 0, 1, 1);



    //create a text box (strike price)
    entry_s = gtk_entry_new ();
    gtk_grid_attach (GTK_GRID (table), entry_s, 0, 1, 1, 1);

    // create a new label.
    label_s = gtk_label_new (" STRIKE PRICE" );
    gtk_grid_attach (GTK_GRID (table), label_s, 1, 1, 1, 1);



    //create a text box (time to maturity)
    entry_t = gtk_entry_new ();
    gtk_grid_attach (GTK_GRID (table), entry_t, 0, 2, 1, 1);

    // create a new label.
    label_t = gtk_label_new (" TIME TO MATURITY" );
    gtk_grid_attach (GTK_GRID (table), label_t, 1, 2, 1, 1);



    //create a text box (volatility)
    entry_v = gtk_entry_new ();
    gtk_grid_attach (GTK_GRID (table), entry_v, 0, 3, 1, 1);

    // create a new label.
    label_v = gtk_label_new (" VOLATILITY" );
    gtk_grid_attach (GTK_GRID (table), label_v, 1, 3, 1, 1);



    //create a text box (interest rate)
    entry_r = gtk_entry_new ();
    gtk_grid_attach (GTK_GRID (table), entry_r, 0, 4, 1, 1);

    // create a new label.
    label_r = gtk_label_new (" INTEREST RATE" );
    gtk_grid_attach (GTK_GRID (table), label_r, 1, 4, 1, 1);



    button = gtk_button_new_with_label("Calculate");
    g_signal_connect_swapped (button, "clicked", G_CALLBACK (entry_Submit), entry_a);
    gtk_grid_attach (GTK_GRID (table), button, 0, 5, 2, 1);

    gtk_widget_show_all(window);

   gtk_main ();
   return 0;
 }

这些都是错误,当我宣布的GtkWidget阵列

these are the errors when I declare an array of GtkWidget

1)在entry_submit回调函数(),我声明如下:

1) In the entry_submit callback-function () where I declare the following:

  text = gtk_entry_get_text(GTK_ENTRY (entry[0]));

编译错误:错误:下标值既不是数组,也不指针也不载体

2)在我宣布以下主要功能:

2) In the main function where I declare the following:

  entry[0] = gtk_entry_new ();

错误:不兼容的类型分配给从类型类型的GtkWidget''结构的GtkWidget *'时

推荐答案

一个指针,指针的答案,在因为我在主函数声明的GtkWidget同样要使用malloc。

A pointer to pointer is the answer and at the same to use malloc since I declare the GtkWidget in the main-function.

    GtkWidget **entry;
entry = malloc(5 * sizeof(GtkWidget));
entry[0] = entry_a;
entry[1] = entry_s;
entry[2] = entry_t;
entry[3] = entry_v;
entry[4] = entry_r;




g_signal_connect_swapped (button, "clicked", G_CALLBACK (entry_Submit), entry);

和功能

 static void entry_Submit(GtkWidget **entry, GtkWidget *widget) {

  GtkWidget *entry_ptr_a = entry[0];
  GtkWidget *entry_ptr_s = entry[1];
  GtkWidget *entry_ptr_t = entry[2];
  GtkWidget *entry_ptr_v = entry[3];
  GtkWidget *entry_ptr_r = entry[4];

  const gchar *a, *s, *t, *v, *r;


    a = gtk_entry_get_text(GTK_ENTRY (entry_ptr_a));
    s = gtk_entry_get_text(GTK_ENTRY (entry_ptr_s));
    t = gtk_entry_get_text(GTK_ENTRY (entry_ptr_t));
    v = gtk_entry_get_text(GTK_ENTRY (entry_ptr_v));
    r = gtk_entry_get_text(GTK_ENTRY (entry_ptr_r));


    printf ("Result: %s , %s, %s, %s, %s\n", a, s, t, v, r);

     }

这篇关于如何发送多个entrys到功能 - GTK的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆