zoukankan      html  css  js  c++  java
  • An Introduction to C & GUI Programming -----Simon Long 学习笔记 3

    这一讲讲的是按键button

    #include <gtk/gtk.h>
    
    int main (int argc, char *argv[])
    {
        gtk_init (&argc, &argv);
        GtkWidget *win1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        GtkWidget *win2 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        GtkWidget *btn = gtk_button_new_with_label ("Close window");
    
        gtk_window_set_title (GTK_WINDOW(win1), "QT is better than GTK");
        gtk_window_set_title (GTK_WINDOW(win2), "GTK is better than QT");
        gtk_window_set_default_size(GTK_WINDOW(win1), 120, 240);
        gtk_window_set_default_size(GTK_WINDOW(win2), 240, 120);
        gtk_container_add (GTK_CONTAINER (win1), btn);
        gtk_widget_show (win2);
        gtk_widget_show_all (win1);
        gtk_main ();
        return 0;
    }

    编译 gcc `pkg-config --cflags gtk+-3.0` test.c -o hello `pkg-config --libs gtk+-3.0`

    结果

      出现了一个新的函数 gtk_widget_show_all,把gtk_widget_show_all  (win1)改为gtk_widget_show(win1)看看会出现什么情况

    #include <gtk/gtk.h>
    
    int main (int argc, char *argv[])
    {
        gtk_init (&argc, &argv);
        GtkWidget *win1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        GtkWidget *win2 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        GtkWidget *btn = gtk_button_new_with_label ("Close window");
    
        gtk_window_set_title (GTK_WINDOW(win1), "QT is better than GTK");
        gtk_window_set_title (GTK_WINDOW(win2), "GTK is better than QT");
        gtk_window_set_default_size(GTK_WINDOW(win1), 120, 240);
        gtk_window_set_default_size(GTK_WINDOW(win2), 240, 120  );
        gtk_container_add (GTK_CONTAINER (win1), btn);
        gtk_widget_show (win1);
        gtk_widget_show (win2);
        gtk_main ();
        return 0;
    }

    结果

     发现按键不见了,猜测 gtk_widget_show_all 会显示1个窗口中所有的内容,多加几个按键试试看

    #include <gtk/gtk.h>
    
    int main (int argc, char *argv[])
    {
        gtk_init (&argc, &argv);
        GtkWidget *win1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        GtkWidget *win2 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    
        GtkWidget *btn1 = gtk_button_new_with_label ("Close window1");
        GtkWidget *btn2 = gtk_button_new_with_label ("Close window2");
        GtkWidget *btn3 = gtk_button_new_with_label ("Close window3");
    
        gtk_window_set_title (GTK_WINDOW(win1), "QT is better than GTK");
        gtk_window_set_title (GTK_WINDOW(win2), "GTK is better than QT");
        gtk_window_set_default_size(GTK_WINDOW(win1), 120, 240);
        gtk_window_set_default_size(GTK_WINDOW(win2), 240, 120  );
        gtk_container_add (GTK_CONTAINER (win1), btn1);
        gtk_container_add (GTK_CONTAINER (win1), btn2);
        gtk_container_add (GTK_CONTAINER (win2), btn3);
        gtk_widget_show_all (win1);
        gtk_widget_show_all (win2);
        gtk_main ();
        return 0;
    }

    结果

     编译告警但是能运行,告警窗口win1 有1个widget,再加一个不行,也不知道为啥,先归类到待处理类别中。(可以用box解决这个问题,见学习笔记4)

    按键button的信号回调函数

    #include <gtk/gtk.h>
    // 点击按键的时候会触发调用这个函数 void end_program (GtkWidget *wid, gpointer ptr) { printf("bybye "); gtk_main_quit (); } int main (int argc, char *argv[]) { gtk_init (&argc, &argv); GtkWidget *win1 = gtk_window_new (GTK_WINDOW_TOPLEVEL); GtkWidget *win2 = gtk_window_new (GTK_WINDOW_TOPLEVEL); GtkWidget *btn1 = gtk_button_new_with_label ("Close window1"); GtkWidget *btn2 = gtk_button_new_with_label ("Close window2"); gtk_window_set_title (GTK_WINDOW(win1), "QT"); gtk_window_set_title (GTK_WINDOW(win2), "GTK"); gtk_window_set_default_size(GTK_WINDOW(win1), 120, 240); gtk_window_set_default_size(GTK_WINDOW(win2), 240, 120 ); g_signal_connect (btn1, "clicked", G_CALLBACK (end_program), NULL); g_signal_connect (btn2, "clicked", G_CALLBACK (end_program), NULL); gtk_container_add (GTK_CONTAINER (win1), btn1); gtk_container_add (GTK_CONTAINER (win2), btn2); gtk_widget_show_all (win1); gtk_widget_show_all (win2); gtk_main (); return 0; }

    结果 : 无论点击窗口1还是2的close window,均会关闭2个窗口,并在shell下打印bye bye。

     点击窗口的x可以关闭程序(

    if we click the X at the top right, the window closes but the
    program doesn’t exit. Let’s fix that.
    The way we do this is to connect another handler to the signal generated when that X is
    clicked. The name of this signal is delete_event , and it is generated from the window
    widget)

    #include <gtk/gtk.h>
    void end_program (GtkWidget *wid, gpointer ptr)
    {
        printf("bybye
    ");
        gtk_main_quit ();
    }
    
    int main (int argc, char *argv[])
    {
        gtk_init (&argc, &argv);
        GtkWidget *win1 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
        GtkWidget *win2 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    
        GtkWidget *btn1 = gtk_button_new_with_label ("Close window1");
        GtkWidget *btn2 = gtk_button_new_with_label ("Close window2");
    
        gtk_window_set_title (GTK_WINDOW(win1), "QT");
        gtk_window_set_title (GTK_WINDOW(win2), "GTK");
        gtk_window_set_default_size(GTK_WINDOW(win1), 120, 240);
        gtk_window_set_default_size(GTK_WINDOW(win2), 240, 120  );
    
        g_signal_connect (btn1, "clicked", G_CALLBACK (end_program), NULL);
        g_signal_connect (win2, "delete_event", G_CALLBACK (end_program), NULL);  // 窗口点击X会触发delete_event信号
        gtk_container_add (GTK_CONTAINER (win1), btn1);
        gtk_container_add (GTK_CONTAINER (win2), btn2);
        gtk_widget_show_all (win1);
        gtk_widget_show_all (win2);
        gtk_main ();
        return 0;
    }

    结果:点击 Close window2没反应了,点击X关闭所有窗口了

  • 相关阅读:
    SQL 左外连接,右外连接,全连接,内连接
    CentOS 7.0默认使用的是firewall作为防火墙
    Python-WEB自动化-三种等待方式
    Python + Selenium的web自动化框架
    BS架构和CS架构的区别
    ERROR 1062 (23000): Duplicate entry '%-root' for key 'PRIMARY'
    CentOS 7镜像下载
    CentOS7各个版本镜像下载地址
    CentOS7的Minimal版本安装
    Python 文字转语音
  • 原文地址:https://www.cnblogs.com/goodluck14/p/13235243.html
Copyright © 2011-2022 走看看