You can define your widgets in a structure and call them from within the function you created -- including organizing your application --, but calling by g_signal_connect()
, and then connect a widget to another. An example of code in which the text of a entry
is put on a label
after a button
be clicked:
#include <gtk/gtk.h>
typedef struct MinhaJanela {
GtkWidget *window;
GtkWidget *botao;
GtkWidget *texto;
GtkWidget *label;
GtkWidget *fixed;
} JanelaPrincipal;
static void botao_clicado(GtkWidget *, gpointer);
int main(int argc, char *argv[])
{
JanelaPrincipal janela;
gtk_init(&argc, &argv);
janela.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(janela.window), "Minha Janela");
gtk_window_set_default_size(GTK_WINDOW(janela.window), 300, 80);
gtk_window_set_position(GTK_WINDOW(janela.window), GTK_WIN_POS_CENTER);
janela.fixed = gtk_fixed_new();
gtk_container_add(GTK_CONTAINER(janela.window), janela.fixed);
janela.texto = gtk_entry_new();
gtk_fixed_put(GTK_FIXED(janela.fixed), janela.texto, 15, 15);
gtk_widget_set_size_request(janela.texto, 90, 30);
janela.botao = gtk_button_new_with_label("Botão");
gtk_fixed_put(GTK_FIXED(janela.fixed), janela.botao, 115, 15);
gtk_widget_set_size_request(janela.botao, 80, 30);
janela.label = gtk_label_new("Label");
gtk_fixed_put(GTK_FIXED(janela.fixed), janela.label, 190, 15);
gtk_widget_set_size_request(janela.label, 100, 30);
/* Sinais */
g_signal_connect(GTK_OBJECT(janela.botao), (gpointer) "clicked",
G_CALLBACK(botao_clicado), &janela);
g_signal_connect_swapped(G_OBJECT(janela.window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(janela.window);
gtk_main();
return 0;
}
/* Função chamada */
static void botao_clicado(GtkWidget *widget, gpointer data)
{
gtk_label_set_text( GTK_LABEL( ((JanelaPrincipal *)data) -> label ),
(gchar *)gtk_entry_get_text(GTK_ENTRY( ((JanelaPrincipal *)data) -> texto)) );
}
Analyze the function botao_clicado()
and the g_signal_connect()
, is among them that occurs the "communication" of widgets.
Hi Lucas, could you prepare a code example demonstrating your difficulty? You can write a function of
callback
that gets the text of entry using gtk_entry_get_text() and set the text in your other widget with its respective September.– Anthony Accioly
To make it clear, you don’t need inputs and returns, just have widgets accessible in callback call scope.
– Anthony Accioly