gtk窗口和标签控件范例代码

缥帙各舒散,前后互相逾。这篇文章主要讲述gtk窗口和标签控件范例代码相关的知识,希望能为你提供帮助。


其实已有此类博文。标题不够清楚,编译脚本也没找到。

  • 安装依赖库
sudo apt install -y libgtk2.0-dev

  • 源码
#include < gtk/gtk.h>

#defineFIRST_LABEL "first label"

int main(int argc, char *argv[])

gtk_init(NULL, NULL);

GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_default_size(GTK_WINDOW(window), 300, 200);

GtkWidget *vbox = gtk_vbox_new(TRUE, 10);
gtk_container_add(GTK_CONTAINER(window), vbox);

GtkWidget *label_one = gtk_label_new(FIRST_LABEL);
gtk_container_add(GTK_CONTAINER(vbox), label_one);

gtk_widget_show_all(window);

gtk_main();

return 0;

  • 编译脚本
#!/bin/bash

COMPILE_INCLUDE=$COMPILE_INCLUDE" -I/usr/include/gtk-2.0"
COMPILE_INCLUDE=$COMPILE_INCLUDE" -I/usr/include/glib-2.0"
COMPILE_INCLUDE=$COMPILE_INCLUDE" -I/usr/include/pango-1.0"
COMPILE_INCLUDE=$COMPILE_INCLUDE" -I/usr/include/cairo"
COMPILE_INCLUDE=$COMPILE_INCLUDE" -I/usr/include/gdk-pixbuf-2.0"
COMPILE_INCLUDE=$COMPILE_INCLUDE" -I/usr/include/atk-1.0"
COMPILE_INCLUDE=$COMPILE_INCLUDE" -I/usr/include/harfbuzz"

COMPILE_INCLUDE=$COMPILE_INCLUDE" -I/usr/lib/x86_64-linux-gnu/glib-2.0/include"
COMPILE_INCLUDE=$COMPILE_INCLUDE" -I/usr/lib/x86_64-linux-gnu/gtk-2.0/include"

COMPILE_LINK=$COMPILE_LINK" -lgobject-2.0 -lgtk-x11-2.0"

OUTPUT_FILE=test
if [ -f $OUTPUT_FILE ]; then
rm $OUTPUT_FILE
fi

echo $COMPILE_INCLUDE_LINK

gcc \\
-o $OUTPUT_FILE \\
gtk.cpp \\
$COMPILE_INCLUDE \\
$COMPILE_LINK

【gtk窗口和标签控件范例代码】


    推荐阅读