废话少说, 当然是先打开glade3 放置一个窗口 在窗口上放置一个固定容器 然后然后在容器上放置一个单行文本框 在文本框下面放置两个按钮 在按钮下面放置一个标签 保存为entryreset.glade 名称之类的如图所示: ![](http://www.bailangcn.tk/content/uploadfile/201203/thum-8266e4bfeda1bd42d8f9794eb4ea0a1320120315131457.png)
在code::blocks下输入以下代码: /* 作者:bailangcn 时间:2012/3/15 作者主页:bailangcn.tk */
#include <gtk/gtk.h>
GtkWidget *entry;
GtkWidget *label;
void get_entry_to_label(GtkWidget *Widget,gpointer data)
{
const gchar *text; text = (gchar *)malloc(sizeof(gchar));
text = gtk_entry_get_text(GTK_ENTRY(entry));//获取entry的内容 gtk_label_set_text(GTK_LABEL(label),text);//设置label的内容
}
void reset_entry(GtkWidget *Widget,gpointer data)
{
gtk_entry_set_text(GTK_ENTRY(entry),"欢迎光临www.bailangcn.tk");//设置entry的内容
}
int main(int argc, char *argv[])
{
GtkBuilder *builder; gtk_init(&argc, &argv);
GtkWidget *window; GtkWidget *button;
GtkWidget *reset; builder = gtk_builder_new();
gtk_builder_add_from_file(builder, "entryreset.glade", NULL); window = GTK_WIDGET(gtk_builder_get_object(builder, "window"));
entry = GTK_WIDGET(gtk_builder_get_object(builder,"entry"));
button = GTK_WIDGET(gtk_builder_get_object(builder,"button"));
reset = GTK_WIDGET(gtk_builder_get_object(builder,"reset"));
label = GTK_WIDGET(gtk_builder_get_object(builder,"label"));
g_signal_connect(G_OBJECT(window),"delete_event",G_CALLBACK(gtk_main_quit),NULL);
g_signal_connect(G_OBJECT(button),"pressed",G_CALLBACK(get_entry_to_label),NULL);
g_signal_connect(G_OBJECT(reset),"pressed",G_CALLBACK(reset_entry),NULL);
// gtk_builder_connect_signals(builder, NULL);//自动关联已在 UI 文件配置好的信号 如果你不在galde文件里面配置信号这行代码可以不用
g_object_unref(G_OBJECT(builder));//释放builder gtk_widget_show_all(window);
gtk_main();
return 0;
} 编译运行可得如下界面: ![](http://www.bailangcn.tk/content/uploadfile/201203/9efe1efa6f059a08f8257b76076b562a20120315132032.png)
输入文字按确定如下: ![](http://www.bailangcn.tk/content/uploadfile/201203/91db2edef695d914308451eb36588d0e20120315132402.png)
按下重置如下: ![](http://www.bailangcn.tk/content/uploadfile/201203/b0bc7a51281455991fb349c8ea1bed5520120315132417.png)
本文始发于:http://www.bailangcn.tk/?post=12转载请注明
|