我开发了一个c - Black& amp;斯科尔斯。我的老师要我为此实现GUI,他建议使用GTK。由于我对c(java程序员从一开始)的知识不足,这对我来说几乎是太多了,因为截止日期是4个小时。但经过艰苦的努力和对stackoverflow的良好帮助,我想我有一个想要做到的!但是现在有一个令人恼火的障碍 - 那就是将多个条目传递给方法。简而言之 - 有五个输入字段,我想要的是通过g_signal_connect_swapped将所有这些发送到我提取的调用字符串的方法。问题是我能够做到这一点(感谢帮助),只要只有一个字符串(一个条目)。但是如何将所有这些托管发送给函数?
我试图声明一个向量但没有成功。由于错误,它甚至无法编译。
static GtkWidget entry[5]
由于数组是一个指针(指向第一个元素),它应该可以工作 - 但不是!
更奇怪 - 我有另一个想法 - 而不是发送我试图在main方法中提取字符串值的命令 - 与函数中的语法相同 - 但没有成功。该值为null。
const gchar *text;
text = gtk_entry_get_text(GTK_ENTRY (entry_a));
printf ("Result: %s\n", text);
上面的代码片段在函数中被执行 - 但不在main函数中。为什么???
希望您理解我的问题 - 这里是生成GUI的代码。代码没有错误 - 这是我向回调函数发送一个入口值的地方。
#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
数组时,这些是错误1)在entry_submit callback-function()中,我声明了以下内容:
text = gtk_entry_get_text(GTK_ENTRY (entry[0]));
编译错误:错误:下标值既不是数组也不是指针,也不是矢量
2)在我宣布以下内容的主要功能中:
entry[0] = gtk_entry_new ();
错误:从“struct GtkWidget *”类型指定类型“GtkWidget”时出现不兼容的类型
答案 0 :(得分:0)
指向指针的指针是答案,同时使用malloc,因为我在main函数中声明了GtkWidget。
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);
}