gtk - 在GTK+助手中,打印变量

  显示原文与译文双语对照的内容
89 5

我想要用户在GTK+助手中的名字,在全局变量中保存这个名字,并在同一个助手中使用这里变量。我的问题是助手已经完全创建并且新站点的更改没有任何影响。

示例:使用将全局变量"姓名"设置为。现在,助手创建所有站点,但只显示第一个站点。用户在"entryfield"中写下他的名字,变量"姓名"现在包含他的名字。助手现在显示第二个站点,它应该包含用户名,但它显示字符串"空白"。

我需要使用新变量创建这个已经创建的条目。我的英语不好,希望能解释一下我的问题。: )

完整的示例代码:

#include <gtk/gtk.h>
#include <string.h>
static void entry_changed (GtkEditable*, GtkAssistant*);
static void assistant_cancel (GtkAssistant*, gpointer);
static void assistant_close (GtkAssistant*, gpointer);
char *name ="empty";
typedef struct {
 GtkWidget *widget;
 gint index;
 const gchar *title;
 GtkAssistantPageType type;
 gboolean complete;
} PageInfo;
int main (int argc,
 char *argv[])
{
 GtkWidget *assistant, *entry, *label;
 guint i;
 PageInfo page[5] = {
 { NULL, -1,"Introduction", GTK_ASSISTANT_PAGE_INTRO, TRUE},
 { NULL, -1, NULL, GTK_ASSISTANT_PAGE_CONTENT, FALSE},
 { NULL, -1, NULL, GTK_ASSISTANT_PAGE_CONTENT, TRUE},
 { NULL, -1,"Confirmation", GTK_ASSISTANT_PAGE_CONFIRM, TRUE},
 };
 gtk_init (&argc, &argv);
/* Create a new assistant widget with no pages. */
 assistant = gtk_assistant_new ();
 gtk_widget_set_size_request (assistant, 450, 300);
 gtk_window_set_title (GTK_WINDOW (assistant),"GtkAssistant Example");
 g_signal_connect (G_OBJECT (assistant),"destroy",
 G_CALLBACK (gtk_main_quit), NULL);
 page[0].widget = gtk_label_new ("This is an example of a GtkAssistant. Byn"
"clicking the forward button, you can continuen"
"to the next section!");
 page[1].widget = gtk_hbox_new (FALSE, 5);
 page[2].widget = gtk_entry_new();
 page[3].widget = gtk_label_new ("finish!");
/* Create the necessary widgets for the second page. */
 label = gtk_label_new ("Your Name:");
 entry = gtk_entry_new ();
 gtk_box_pack_start (GTK_BOX (page[1].widget), label, FALSE, FALSE, 5);
 gtk_box_pack_start (GTK_BOX (page[1].widget), entry, FALSE, FALSE, 5);
 gtk_entry_set_text(GTK_ENTRY(page[2].widget), name);
/* Add four pages to the GtkAssistant dialog. */
 for (i = 0; i <4; i++)
 {
 page[i].index = gtk_assistant_append_page (GTK_ASSISTANT (assistant),
 page[i].widget);
 gtk_assistant_set_page_title (GTK_ASSISTANT (assistant),
 page[i].widget, page[i].title);
 gtk_assistant_set_page_type (GTK_ASSISTANT (assistant),
 page[i].widget, page[i].type);
/* Set the introduction and conclusion pages as complete so they can be
 * incremented or closed. */
 gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant),
 page[i].widget, page[i].complete);
 }
/* Update whether pages 2 through 4 are complete based upon whether there is
 * text in the GtkEntry, the check button is active, or the progress bar
 * is completely filled. */
 g_signal_connect (G_OBJECT (entry),"changed",
 G_CALLBACK (entry_changed), (gpointer) assistant);
 g_signal_connect (G_OBJECT (assistant),"cancel",
 G_CALLBACK (assistant_cancel), NULL);
 g_signal_connect (G_OBJECT (assistant),"close",
 G_CALLBACK (assistant_close), NULL);
 gtk_widget_show_all (assistant);
 gtk_main ();
 return 0;
}
/* If there is text in the GtkEntry, set the page as complete. Otherwise,
 * stop the user from progressing the next page. */
static void
entry_changed (GtkEditable *entry,
 GtkAssistant *assistant)
{
 const gchar *text = gtk_entry_get_text (GTK_ENTRY (entry));
 gint num = gtk_assistant_get_current_page (assistant);
 GtkWidget *page = gtk_assistant_get_nth_page (assistant, num);
 gtk_assistant_set_page_complete (assistant, page, (strlen (text)> 0));
 name = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
}
/* If the dialog is cancelled, delete it from memory and then clean up after
 * the Assistant structure. */
static void
assistant_cancel (GtkAssistant *assistant,
 gpointer data)
{
 gtk_widget_destroy (GTK_WIDGET (assistant));
}
/* This function is where you would apply the changes and destroy the assistant. */
static void
assistant_close (GtkAssistant *assistant,
 gpointer data)
{
 g_print ("You would apply your changes now!n");
 gtk_widget_destroy (GTK_WIDGET (assistant));
}
时间:原作者:3个回答

86 3

entry_changed 中,你必须调用gtk_entry_set_text(GTK_ENTRY(page[2].widget), name);同样,因为只设置了输入文本一次,这是唯一的事情。否则设置 3rd 页sticks时设置的默认值。

原作者:
...