gpt4 book ai didi

c - 在 GTK+ 助手中打印变量

转载 作者:太空宇宙 更新时间:2023-11-04 04:48:21 25 4
gpt4 key购买 nike

我想在 GTK+ 助手中询问用户他的名字,将这个名字保存在一个全局变量中,并在不同站点的同一个助手中使用这个变量。我的问题是助手已完全创建,新站点上的更改无效。

例子:我用“空”初始化全局变量“名称”。现在,助手创建所有站点但仅显示第一个站点。用户在“entryfield”中写下他的名字,变量“name”现在包含他的名字。助手现在显示应包含用户名的第二个站点,但它显示字符串“空”。

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

完整示例代码:

#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. By\n"\
"clicking the forward button, you can continue\n"\
"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));
}

最佳答案

entry_changed 中,您必须再次调用 gtk_entry_set_text(GTK_ENTRY(page[2].widget), name);,因为您只设置了一次输入文本,这是唯一重要的事情。否则,您在设置第 3 页时设置的默认值会保留。

关于c - 在 GTK+ 助手中打印变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18609421/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com