gtk3gtk3入门


备注

GTK + 3也称为Gtk3,是一个多平台的GUI工具包,它是用C语言编写的,但它包含许多语言的绑定,包括C ++,Python,Vala和Ruby。 (有关完整列表,请参阅Gtk网站 )。

Gtk +是GNU项目的一部分,属于GNU LGPL许可证,这意味着所有开发人员都可以使用它,包括开发专有软件的开发人员,无需任何许可费或版税。

致谢:松散地基于http://www.gtk.org/

版本

发布日期
3.20 2016年3月1日
3.18 2015年9月1日
3.16 2015年3月1日
3.14 2014年9月1日
3.12 2014年3月1日
3.10 2013年9月1日
3.8 2013-03-01
3.6 2012-09-01
3.4 2012-04-01
3.2 2011-10-01
3.0 2010-02-01

资料来源:

[C] Gtk +中的“Hello World”

#include <gtk/gtk.h>

// callback function which is called when button is clicked
static void on_button_clicked(GtkButton *btn, gpointer data) {
    // change button label when it's clicked
    gtk_button_set_label(btn, "Hello World");
}

// callback function which is called when application is first started
static void on_app_activate(GApplication *app, gpointer data) {
    // create a new application window for the application
    // GtkApplication is sub-class of GApplication
    // downcast GApplication* to GtkApplication* with GTK_APPLICATION() macro
    GtkWidget *window = gtk_application_window_new(GTK_APPLICATION(app));
    // a simple push button
    GtkWidget *btn = gtk_button_new_with_label("Click Me!");
    // connect the event-handler for "clicked" signal of button
    g_signal_connect(btn, "clicked", G_CALLBACK(on_button_clicked), NULL);
    // add the button to the window
    gtk_container_add(GTK_CONTAINER(window), btn);
    // display the window
    gtk_widget_show_all(GTK_WIDGET(window));
}

int main(int argc, char *argv[]) {
    // create new GtkApplication with an unique application ID
    GtkApplication *app = gtk_application_new(
        "org.gtkmm.example.HelloApp", 
        G_APPLICATION_FLAGS_NONE
    );
    // connect the event-handler for "activate" signal of GApplication
    // G_CALLBACK() macro is used to cast the callback function pointer
    // to generic void pointer
    g_signal_connect(app, "activate", G_CALLBACK(on_app_activate), NULL);
    // start the application, terminate by closing the window
    // GtkApplication* is upcast to GApplication* with G_APPLICATION() macro
    int status = g_application_run(G_APPLICATION(app), argc, argv);
    // deallocate the application object
    g_object_unref(app);
    return status;
}
 

[C ++] gtkmm中的“Hello World”

#include <gtkmm/application.h>
#include <gtkmm/applicationwindow.h>
#include <gtkmm/button.h>

// main window of the application
class HelloWorldWindow : public Gtk::ApplicationWindow {
    // a simple push button
    Gtk::Button btn;
public:
    HelloWorldWindow()
    : btn("Click me!") {// initialize button with a text label
        // when user presses the button "clicked" signal is emitted
        // connect an event handler for the signal with connect()
        // which accepts lambda expression, among other things
        btn.signal_clicked().connect(
        [this]() {
            btn.set_label("Hello World");
        });
        // add the push button to the window
        add(btn);
        // make the window visible
        show_all();
    }
};

int main(int argc, char *argv[]) {
     // This creates an Gtk+ application with an unique application ID
     auto app = Gtk::Application::create(argc, argv, "org.gtkmm.example.HelloApp");
     HelloWorldWindow hw;
     // this starts the application with our window
     // close the window to terminate the application
     return app->run(hw);
}
 

安装或设置

蟒蛇

视窗

安装GTK3 for Python的最简单方法是使用PyGObject for Windows 。它提供了一个安装程序,可以安装开发GTK应用程序所需的大部分内容。

PyGObject安装程序提供的选项数量令人生畏,但对于大多数GTK项目,您必须选择的唯一选项是GTK+ 3.xx

C ++

Gtk +的C ++绑定称为gtkmm

视窗

在Microsoft Windows上,gtkmm可以通过MSYS2环境安装。通过安装安装程序并更新软件包列表来设置MSYS2环境后,安装gtkmm with

pacman -S mingw-w64-x86_64-gtkmm3 #64 bit
pacman -S mingw-w64-i686-gtkmm3   #32 bit
 

安装pkg-config以轻松获取编译器和链接器标志以及GNU autotools构建集成

pacman -S pkg-config
 

现在gtkmm应用程序可以在MSYS2环境中编译,链接和运行。

# enable C++ 14 support if needed
# -mwindows flag is to suppress the background command-prompt window 
# for GUI applications
g++ -mwindows -std=c++14 -o app.exe app.cpp `pkg-config --cflags --libs gtkmm-3.0`
./app.exe
 

但是由于缺少.dll查找的标准环境变量,可执行文件不会在MSYS2 shell之外运行。需要<MSYS2 INSTALLATION DIRECTORY>\mingw64\lib\ 将以下.dll从<MSYS2 INSTALLATION DIRECTORY>\mingw64\lib\ (用于64位安装)复制到应用程序目录.exe 所在的位置)。版本号可能会根据安装情况而变化。

libatk-1.0-0.dll
libatkmm-1.6-1.dll
libbz2-1.dll
libcairo-2.dll
libcairo-gobject-2.dll
libcairomm-1.0-1.dll
libepoxy-0.dll
libexpat-1.dll
libffi-6.dll
libfontconfig-1.dll
libfreetype-6.dll
libgcc_s_seh-1.dll
libgdk_pixbuf-2.0-0.dll
libgdk-3-0.dll
libgdkmm-3.0-1.dll
libgio-2.0-0.dll
libgiomm-2.4-1.dll
libglib-2.0-0.dll
libglibmm-2.4-1.dll
libgmodule-2.0-0.dll
libgobject-2.0-0.dll
libgtk-3-0.dll
libgtkmm-3.0-1.dll
libharfbuzz-0.dll
libiconv-2.dll
libintl-8.dll
libpango-1.0-0.dll
libpangocairo-1.0-0.dll
libpangoft2-1.0-0.dll
libpangomm-1.4-1.dll
libpangowin32-1.0-0.dll
libpixman-1-0.dll
libpng16-16.dll
libsigc-2.0-0.dll
libstdc++-6.dll
libwinpthread-1.dll
zlib1.dll
 

在此步骤之后,程序应该运行。但它找不到Gtk +的标准图标集,即Adwaita图标主题 ,因此图标可能无法加载。需要将图标和一些其他文件复制到应用程序目录中,以便应用程序可以加载它们。

<MSYS2 INSTALL DIRECTORY>

mingw64
 |
 +-- lib
      |
      +-- gdk-pixbuf-2.0
share
 |
 +-- icons
       |
       +-- Adwaita
       |
       +-- hicolor (fallback icon theme for Gtk+)
 

到应用程序目录,具有相同的目录结构。

入门套件

#include <gtk/gtk.h>
static void destroy(GtkWidget *widget, gpointer data)
{
gtk_main_quit();
}
int main(int argc, char *argv[])
{
gtk_init(&argc, &argv);
GtkWidget *window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_title(GTK_WINDOW(window), "Window");
g_signal_connect(window, "destroy", G_CALLBACK(destroy), NULL);

GtkWidget *k;
k= gtk_fixed_new();
    gtk_container_add(GTK_CONTAINER(window), k);

       GtkWidget* la,*r;
    la = gtk_button_new_with_label (",mkl");
     gtk_fixed_put (GTK_FIXED (k), la,50,237);
    gtk_widget_set_size_request(la, 98, 90);
//    gtk_container_set_border_width(GTK_CONTAINER (la)    , 5);


    r = gtk_button_new_with_label (",kii");
     gtk_fixed_put (GTK_FIXED (k), r,150,237);
    gtk_widget_set_size_request(r, 98, 90);

    gtk_widget_set_size_request(GTK_WIDGET(window),300,349);
gtk_widget_show_all(GTK_WIDGET(window));

gtk_main();
return 0;
}
 

编译:

c++ starterkit.c `pkg-config --libs --cflags gtk+-3.0` -o p
 

./p