#include <stdio.h>
#include <stdlib.h>
#include <gtk/gtk.h>
int mcd();
static void num1( GtkWidget *entry1)
{
const gchar* entry_text;
entry_text = gtk_entry_get_text(GTK_ENTRY (entry1));
}
static void num2( GtkWidget *entry2)
{
const gchar* entry_text;
entry_text = gtk_entry_get_text(GTK_ENTRY (entry2));
}
//DA QUI...ho collegato il pulsante "Calcola" con la funzione MCD!
static void callback( GtkWidget *widget,
gpointer data)
{
int a,b,c;
a=22;
b=12;
c=mcd(a,b);
g_print("%d\n", c);
}
int mcd(int x, int y){
if(y==0){
return x;
}
else return mcd(y, x%y);
}
//A QUI
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *table;
GtkWidget *label1;
GtkWidget *label2;
GtkWidget *entry1;
GtkWidget *entry2;
GtkWidget *button;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(window), "MCD Calculator");
gtk_container_set_border_width(GTK_CONTAINER(window), 10);
table = gtk_table_new(3, 2, FALSE);
gtk_container_add(GTK_CONTAINER(window), table);
label1 = gtk_label_new("Num1:");
label2 = gtk_label_new("Num2:");
gtk_table_attach(GTK_TABLE(table), label1, 0, 1, 0, 1, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
gtk_table_attach(GTK_TABLE(table), label2, 0, 1, 1, 2, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
entry1 = gtk_entry_new();
g_signal_connect(G_OBJECT(entry1), "activate", G_CALLBACK(num1), &entry1);
entry2 = gtk_entry_new();
g_signal_connect(G_OBJECT(entry2), "activate", G_CALLBACK(num2), &entry2);
gtk_table_attach(GTK_TABLE(table), entry1, 1, 2, 0, 1, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
gtk_table_attach(GTK_TABLE(table), entry2, 1, 2, 1, 2, GTK_FILL | GTK_SHRINK, GTK_FILL | GTK_SHRINK, 5, 5);
/*Pulsante calcola*/
button = gtk_button_new_with_label ("Calcola");
gtk_container_add (GTK_CONTAINER (window), button);
gtk_table_attach (GTK_TABLE (table), button, 1, 5, 7, 8, GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 0, 0);
g_signal_connect (button, "clicked", G_CALLBACK (callback), (gpointer) "Calcola");
/*Fine pulsante calcola*/
gtk_widget_show_all(window);
g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);
gtk_main();
return EXIT_SUCCESS;
}
我不知道如何从“entry_text”和“entry_text_1”中捕获数字并将它们用于回调函数。 (因此它将成为:a = entry_text; b = entry_text_1)
答案 0 :(得分:0)
您是否正在尝试访问按钮的GtkEntry
功能中的两个callback
的文本?如果是这样,您需要将两者的引用传递给按钮的信号处理程序。
首先,改变这一行:
g_signal_connect (button, "clicked", G_CALLBACK (callback), (gpointer) "Calcola");
对此:
GtkWidget *entries[2] = { entry1, entry 2};
g_signal_connect (button, "clicked", G_CALLBACK (callback), entries);
然后,在callback
函数中:
static void callback( GtkWidget *widget,
gpointer data)
{
GtkWidget *entries = data;
int a,b,c;
a=atoi(gtk_entry_get_text(GTK_ENTRY(entries[0])));
b=atoi(gtk_entry_get_text(GTK_ENTRY(entries[1])));
c=mcd(a,b);
g_print("%d\n", c);
}