程序代码:
#include <gtk/gtk.h>
void button_event(GtkWidget *,gpointer *);
GtkWidget *name;
GtkWidget *passwd;
int main(int argc,char *argv[])
{
GtkWidget *window;
GtkWidget *button;
GtkWidget *box;
GtkWidget *label;
gtk_init(&argc,&argv);
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_signal_connect(GTK_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
box=gtk_vbox_new(FALSE,0);
label=gtk_label_new("name");
gtk_box_pack_start(GTK_BOX(box),label,FALSE,FALSE,5);
gtk_widget_show(label);
name=gtk_entry_new();
gtk_box_pack_start(GTK_BOX(box),name,FALSE,FALSE,5);
gtk_widget_show(name);
label=gtk_label_new("passwd");
gtk_box_pack_start(GTK_BOX(box),label,FALSE,FALSE,5);
gtk_widget_show(label);
passwd=gtk_entry_new();
gtk_entry_set_visibility(GTK_ENTRY(passwd),FALSE);//录入内容不可见,如密码等
gtk_box_pack_start(GTK_BOX(box),passwd,FALSE,FALSE,5);
gtk_widget_show(passwd);
button=gtk_button_new_with_label("enter");
gtk_box_pack_start(GTK_BOX(box),button,FALSE,FALSE,5);
gtk_signal_connect(GTK_OBJECT(button),"pressed",GTK_SIGNAL_FUNC(button_event),"enter");
gtk_widget_show(button);
gtk_container_add(GTK_CONTAINER(window),box);
gtk_widget_show(box);
gtk_widget_show(window);
gtk_main();
return 0;
}
void button_event(GtkWidget *widget,gpointer *data)
{
const gchar *uname;
const gchar *upasswd;
uname=gtk_entry_get_text(GTK_ENTRY(name));
upasswd=gtk_entry_get_text(GTK_ENTRY(passwd));
g_print("Name:%s\n",uname);
g_print("Passwd:%s\n",upasswd);
}