今天继续做的是昨天那个界面对的优化,直接贴下代码,
1 void click_radio(GtkWidget *widget,gpointer *data) 2 { 3 GtkWidget *dialog; 4 GtkWidget *box; 5 GtkWidget *pbox; 6 GtkWidget *bbox; 7 GtkWidget *password_label,*password_entry; 8 GtkWidget *button1; 9 GtkWidget *button2;10 11 dialog=gtk_dialog_new();12 gtk_window_set_modal((GtkWindow*)dialog,GTK_DIALOG_MODAL); //设置窗口模式,这里设置的是除该窗口之外的其他窗口无法进行操作13 gtk_window_set_title(GTK_WINDOW(dialog),"管理员登陆");14 g_signal_connect(G_OBJECT(dialog),"destroy",G_CALLBACK(gtk_widget_destroy),NULL);15 box=GTK_DIALOG(dialog)->vbox;16 ubox=gtk_hbox_new(TRUE,5);17 pbox=gtk_hbox_new(TRUE,5);18 bbox=gtk_hbox_new(TRUE,5);19 20 password_label=gtk_label_new("登陆口令");21 password_entry=gtk_entry_new();22 gtk_entry_set_visibility(GTK_ENTRY(password_entry),FALSE);23 24 button1=gtk_button_new_with_label("确定");25 button2=gtk_button_new_with_label("取消");26 g_signal_connect(G_OBJECT(button2),"clicked",G_CALLBACK(destroy),dialog);27 g_signal_connect(G_OBJECT(button1),"clicked",G_CALLBACK(login),(gpointer*)password_entry); 28 29 gtk_box_pack_start(GTK_BOX(pbox),password_label,TRUE,FALSE,5);30 gtk_box_pack_start(GTK_BOX(pbox),password_entry,TRUE,FALSE,5);31 gtk_box_pack_start(GTK_BOX(bbox),button1,TRUE,FALSE,5);32 gtk_box_pack_start(GTK_BOX(bbox),button2,TRUE,FALSE,5);33 34 gtk_box_pack_start(GTK_BOX(box),ubox,FALSE,FALSE,5);35 gtk_box_pack_start(GTK_BOX(box),pbox,FALSE,FALSE,5);36 gtk_box_pack_start(GTK_BOX(box),bbox,FALSE,FALSE,5);37 38 gtk_widget_show_all(dialog); //gtk_dialog_run(GTK_DIALOG(dialog)); //gtk_widget_destroy(dialog);
第26和27行调用的函数为
1 #define pwd "123456" 2 void destroy(GtkWidget *widget,gpointer *data) //点击取消按钮的事件 3 { 4 gtk_widget_destroy(GTK_WIDGET(data)); 5 } 6 void login(GtkWidget *widget,gpointer *data) //点击确定按钮的事件 7 { 8 GtkWidget *dialog; 9 GtkEntry *entry = (GtkEntry*) data;10 const gchar* password = gtk_entry_get_text( entry );11 if(strcmp(password,pwd)==0)12 {13 g_print("yes");14 dialog = gtk_message_dialog_new(NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_INFO,GTK_BUTTONS_OK,"登陆成功");15 }16 else17 {g_print("no");18 dialog = gtk_message_dialog_new(NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_INFO,GTK_BUTTONS_CLOSE,"用户口令错误");19 }20 printf("%s\n",password);21 gtk_dialog_run(GTK_DIALOG(dialog)); 22 gtk_widget_destroy(dialog); 23 }
这里其实还有一个问题,就是点击确定按钮之后,判断比较如果成功,怎样才能关掉上一个窗口然后打开一个新窗口。其他的有点问题就是调用的这两个函数传递的参数了,还有就是如果click_radio这个函数中加上最后注释掉的那两句运行的时候就会出现一个错误。
(login:9959): Gtk-CRITICAL **: IA__gtk_widget_destroy: assertion 'GTK_IS_WIDGET (widget)' failed
错误信息就是这样,还是要好好看看gtk的函数内容。继续加油!