GTK:等待用户输入

时间:2013-08-17 18:46:12

标签: c visual-c++ gtk

我在C中使用GTK,我想制作一个表单以收集用户的输入。我使用GTK_ENTRY来收集用户的输入。

不幸的是,我不知道如何执行“暂停”以等待用户输入其输入并在输入准备就绪后立即恢复。

请帮帮我吗?

由于

1 个答案:

答案 0 :(得分:0)

#include <gtk/gtk.h>

static void on_button_clicked(GtkButton *button,gpointer data);
static void on_entry_activate(GtkEntry *entry,gpointer data);

int main(int argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *entry;
    GtkWidget *button;
    GtkWidget *hbox;

    gtk_init(&argc,&argv);
    window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_title(GTK_WINDOW(window),"entry");
    gtk_window_set_position(GTK_WINDOW(window),GTK_WIN_POS_CENTER);
    gtk_container_set_border_width(GTK_CONTAINER(window),5);

    hbox=gtk_box_new(GTK_ORIENTATION_HORIZONTAL,5);
    gtk_container_add(GTK_CONTAINER(window),hbox);

    entry=gtk_entry_new();
    gtk_box_pack_start(GTK_BOX(hbox),entry,TRUE,TRUE,5);

    button=gtk_button_new_with_label("Ok");
    gtk_box_pack_start(GTK_BOX(hbox),button,FALSE,FALSE,0);

    g_signal_connect(G_OBJECT(window),"destroy",
                G_CALLBACK(gtk_main_quit),NULL);
    g_signal_connect(G_OBJECT(button),"clicked",
                G_CALLBACK(on_button_clicked),entry);
    g_signal_connect(G_OBJECT(entry),"activate",/* when you press ENTER */
                G_CALLBACK(on_entry_activate),NULL);

    gtk_widget_show_all(window);
    gtk_main();

    return 0;
}

static void on_button_clicked(GtkButton *button,gpointer data)
{
    GtkWidget *entry=(GtkWidget *)data;
    GtkWidget *dialog;
    gchar buff[1024];

    g_snprintf(buff,1024,"You input '%s' !",
                gtk_entry_get_text(GTK_ENTRY(entry)));

    dialog=gtk_message_dialog_new(NULL,GTK_DIALOG_MODAL,
                GTK_MESSAGE_INFO,GTK_BUTTONS_OK,"%s",buff);
    gtk_dialog_run(GTK_DIALOG(dialog));

    gtk_widget_destroy(dialog);
    gtk_entry_set_text(GTK_ENTRY(entry),"");
}

static void on_entry_activate(GtkEntry *entry,gpointer data)
{
    g_printf("%s\n",gtk_entry_get_text(GTK_ENTRY(entry)));
}

这个例子非常简单。当用户在输入中按ENTER或用于单击OK按钮时,输入就绪 我认为代码很容易阅读。祝你好运!