标签归档:gtk

ubuntu11.10安装GTK/GNOME开发环境

安装GTK环境只要安装一个libgtk2.0-dev就可以了,而安装gnome开发环境的话,需要装gnome-core-devel,它包含GTK开发包。
但在一般情况下,我们需要有一个ide开发环境,需要帮助文件,于是我们安装
sudo apt-get install gnome-devel gnome-devel-docs
sudo apt-get instal glade libglade2-dev
安装完成后我们也同样做个测试程序

#include <gtk/gtk.h>
void hello(GtkWidget *widget,gpointer data)
{
	g_print("Hello Ubuntu!\n");
}
gint delete_event(GtkWidget *widget,GdkEvent *event,gpointer data)
{
	g_print ("delete event occurred\n");
	return(TRUE);
}
void destroy(GtkWidget *widget,gpointer data)
{
	gtk_main_quit();
}
int main( int argc, char *argv[] )
{
	GtkWidget *window;
	GtkWidget *button;
	gtk_init (&argc, &argv);
	window=gtk_window_new (GTK_WINDOW_TOPLEVEL);
	gtk_signal_connect (GTK_OBJECT(window),"delete_event",GTK_SIGNAL_FUNC(delete_event),NULL);
	gtk_signal_connect (GTK_OBJECT (window), "destroy",GTK_SIGNAL_FUNC (destroy), NULL);
	gtk_container_set_border_width (GTK_CONTAINER (window), 10);
	button = gtk_button_new_with_label ("Hello Ubuntu!");
	gtk_signal_connect (GTK_OBJECT (button), "clicked",GTK_SIGNAL_FUNC (hello), NULL);
	gtk_signal_connect_object (GTK_OBJECT (button), "clicked",GTK_SIGNAL_FUNC (gtk_widget_destroy),GTK_OBJECT (window));
	gtk_container_add (GTK_CONTAINER (window), button);
	gtk_widget_show (button);
	gtk_widget_show (window);   /*显示一个窗口*/
	gtk_main();   /*进入主循环*/
	return(0);
}

用下面命令编译运行

$ gcc `pkg-config --cflags --libs gtk+-2.0` gtktest.c -o gtktest
$ ./gtktest

会显示一个带有一个按钮的窗口,点击按钮以后窗口关闭,命令行显示Hello Ubuntu!

有时候运行可能会提示警告:
Gtk-WARNING **: 无法在模块路径中找到主题引擎:“pixmap”。运行下边的apt代码安装就可以了。

sudo apt-get install gtk2-engines-pixbuf

继续阅读

ubuntu gtk 配置与hello world

配置相当简单,只需要在前边配置c和c++的基础上,安装一个gnome-core-devel就可以了,里面集成了很多其他的包。除此之外还要转一些其他的东西,如libglib2.0-doc、libgtk2.0-doc帮助文档,devhelp帮助文档查看,glade-gnome、glade-common、glade-doc图形界面设计等。

直接运行:

sudo apt-get install gnome-core-devel
sudo apt-get install libglib2.0-doc libgtk2.0-doc
sudo apt-get install devhelp
sudo apt-get install glade-gnome glade-common glade-doc

然后写下gtkhello.c

#include<gtk/gtk.h>
void hello(GtkWidget *widget,gpointer data)
{
g_print("Hello Ubuntu!\n");
}
gint delete_event(GtkWidget *widget,GdkEvent *event,gpointer data)
{
g_print ("delete event occurred\n");
return(TRUE);
}
void destroy(GtkWidget *widget,gpointer data)
{
gtk_main_quit();
}
int main( int argc, char *argv[] )
{
GtkWidget *window;
GtkWidget *button;
gtk_init (&argc, &argv);
window=gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_signal_connect (GTK_OBJECT(window),"delete_event",GTK_SIGNAL_FUNC(delete_event),NULL);
gtk_signal_connect (GTK_OBJECT (window), "destroy",GTK_SIGNAL_FUNC (destroy), NULL);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
button = gtk_button_new_with_label ("Hello Ubuntu!");
gtk_signal_connect (GTK_OBJECT (button), "clicked",GTK_SIGNAL_FUNC (hello), NULL);
gtk_signal_connect_object (GTK_OBJECT (button), "clicked",GTK_SIGNAL_FUNC (gtk_widget_destroy),GTK_OBJECT (window));
gtk_container_add (GTK_CONTAINER (window), button);
gtk_widget_show (button);
gtk_widget_show (window);   /*显示一个窗口*/
gtk_main();   /*进入主循环*/
return(0);
}

用gcc编译,
gcc gtkhello.c -o gtktest `pkg-config –cflags –libs gtk+-2.0`
然后运行./gtktest,看到窗口了吧?