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

    这一讲讲用户输入的处理

    #include <gtk/gtk.h>
    GtkWidget *txt;
    
    void end_program (GtkWidget *wid, gpointer ptr)
    {
        printf("bybye
    ");
        gtk_main_quit ();
    }
    
    void copy_text (GtkWidget *wid, gpointer ptr)
    {
        const char *input = gtk_entry_get_text (GTK_ENTRY (txt));
        gtk_label_set_text (GTK_LABEL (ptr), input);
    }
    
    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);
        gtk_window_set_title (GTK_WINDOW(win1), "QT");
        gtk_window_set_title (GTK_WINDOW(win2), "GTK");
    
        GtkWidget *btn1 = gtk_button_new_with_label ("Close window1");
        GtkWidget *btn2 = gtk_button_new_with_label ("copy right");
        GtkWidget *btn3 = gtk_button_new_with_label ("copy left");
    
        GtkWidget *labelA = gtk_label_new ("LABELA");
        GtkWidget *labelB = gtk_label_new ("LABELB");
        txt = gtk_entry_new ();
        GtkWidget *table1 = gtk_grid_new ();
        GtkWidget *table2 = gtk_grid_new ();
    
        gtk_grid_attach (GTK_GRID (table1), labelA, 0, 0, 10, 10);
        gtk_grid_attach (GTK_GRID (table1), btn1, 20, 20, 10, 10);
        gtk_grid_attach (GTK_GRID (table1), btn3, 40, 40, 10, 10);
        gtk_grid_attach (GTK_GRID (table2), labelB, 0, 0, 10, 10);
        gtk_grid_attach (GTK_GRID (table2), btn2, 10, 10, 10, 10);
        gtk_grid_attach (GTK_GRID (table2), txt, 0, 20, 10, 10);
    
    
        g_signal_connect (btn1, "clicked", G_CALLBACK (end_program), NULL);
        g_signal_connect (btn2, "clicked", G_CALLBACK (copy_text), labelB);
        g_signal_connect (btn2, "clicked", G_CALLBACK (copy_text), labelA);
        g_signal_connect (btn3, "clicked", G_CALLBACK (copy_text), labelB);
        g_signal_connect (btn3, "clicked", G_CALLBACK (copy_text), labelA);
        g_signal_connect (win2, "delete_event", G_CALLBACK (end_program), NULL);
    
        gtk_container_add (GTK_CONTAINER (win1), table1);
        gtk_container_add (GTK_CONTAINER (win2), table2);
    
        gtk_widget_show_all (win1);
        gtk_widget_show_all (win2);
        gtk_main ();
        return 0;
    }

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

    结果

    没输入任何字符时

     输入字符然后点击copy right或者copy left时

     另一种输入方式,有选择的输入

    #include <gtk/gtk.h>
    GtkWidget *txt;
    
    void end_program (GtkWidget *wid, gpointer ptr)
    {
        printf("bybye
    ");
        gtk_main_quit ();
    }
    
    void copy_text (GtkWidget *wid, gpointer ptr)
    {
        const char *input = gtk_entry_get_text (GTK_ENTRY (txt));
        gtk_label_set_text (GTK_LABEL (ptr), input);
    }
    
    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);
        gtk_window_set_title (GTK_WINDOW(win1), "QT");
        gtk_window_set_title (GTK_WINDOW(win2), "GTK");
    
        GtkWidget *btn1 = gtk_button_new_with_label ("Close window1");
        GtkWidget *btn2 = gtk_button_new_with_label ("copy right");
        GtkWidget *btn3 = gtk_button_new_with_label ("copy left");
    
        GtkWidget *labelA = gtk_label_new ("LABELA");
        GtkWidget *labelB = gtk_label_new ("LABELB");
        GtkAdjustment  *adj = gtk_adjustment_new (0, -10, 10, 1, 0, 0);
        txt = gtk_spin_button_new (adj, 0, 0);
    
        GtkWidget *table1 = gtk_grid_new ();
        GtkWidget *table2 = gtk_grid_new ();
    
        gtk_grid_attach (GTK_GRID (table1), labelA, 0, 0, 10, 10);
        gtk_grid_attach (GTK_GRID (table1), btn1, 20, 20, 10, 10);
        gtk_grid_attach (GTK_GRID (table1), btn3, 40, 40, 10, 10);
        gtk_grid_attach (GTK_GRID (table2), labelB, 0, 0, 10, 10);
        gtk_grid_attach (GTK_GRID (table2), btn2, 10, 10, 10, 10);
        gtk_grid_attach (GTK_GRID (table2), txt, 0, 20, 10, 10);
    
    
        g_signal_connect (btn1, "clicked", G_CALLBACK (end_program), NULL);
        g_signal_connect (btn2, "clicked", G_CALLBACK (copy_text), labelB);
        g_signal_connect (btn2, "clicked", G_CALLBACK (copy_text), labelA);
        g_signal_connect (btn3, "clicked", G_CALLBACK (copy_text), labelB);
        g_signal_connect (btn3, "clicked", G_CALLBACK (copy_text), labelA);
        g_signal_connect (win2, "delete_event", G_CALLBACK (end_program), NULL);
    
        gtk_container_add (GTK_CONTAINER (win1), table1);
        gtk_container_add (GTK_CONTAINER (win2), table2);
    
        gtk_widget_show_all (win1);
        gtk_widget_show_all (win2);
        gtk_main ();
        return 0;
    }

    结果:可以从0变到10,或者0变到-10

    gtk_adjustment_new (0, -10, 10, 1, 0, 0);   // 初始值0,最小值-10,最大值10, 鼠标左键每次递增1(最右边2个是the page increment和the page size)

     变到10后在点击copy按键

     另一种单选输入

    #include <gtk/gtk.h>
    GtkWidget *txt;
    
    void end_program (GtkWidget *wid, gpointer ptr)
    {
        printf("bybye
    ");
        gtk_main_quit ();
    }
    
    void copy_text (GtkWidget *wid, gpointer ptr)
    {
        const char *input = gtk_entry_get_text (GTK_ENTRY (txt));
        gtk_label_set_text (GTK_LABEL (ptr), input);
    }
    
    void check_toggle (GtkWidget *wid, gpointer ptr)
    {
        static int i = 0;
        i++;
        char arr[60] = {0};
        sprintf(arr, "The state of the button checked %d times
    ", i);
        printf ("The state of the button is %d
    ", gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (wid)));
        gtk_label_set_text (GTK_LABEL (ptr), arr);
    }
    
    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);
        gtk_window_set_title (GTK_WINDOW(win1), "QT");
        gtk_window_set_title (GTK_WINDOW(win2), "GTK");
    
        GtkWidget *btn1 = gtk_button_new_with_label ("Close window1");
        GtkWidget *btn2 = gtk_button_new_with_label ("copy right");
        GtkWidget *btn3 = gtk_button_new_with_label ("copy left");
    
        GtkWidget *chk1 = gtk_check_button_new_with_label ("My check");
        GtkWidget *chk2 = gtk_check_button_new_with_label ("check");
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chk1), FALSE);   //  设置默认勾选状态
        gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (chk2), TRUE);  //  设置默认勾选状态
    
        GtkWidget *labelA = gtk_label_new ("LABELA");
        GtkWidget *labelB = gtk_label_new ("LABELB");
        GtkAdjustment  *adj = gtk_adjustment_new (0, -100, 100, 1, 50, 0);
        txt = gtk_spin_button_new (adj, 0, 0);
    
        GtkWidget *table1 = gtk_grid_new ();
        GtkWidget *table2 = gtk_grid_new ();
    
        gtk_grid_attach (GTK_GRID (table1), labelA, 0, 0, 10, 10);
        gtk_grid_attach (GTK_GRID (table1), btn1, 20, 20, 10, 10);
        gtk_grid_attach (GTK_GRID (table1), btn3, 40, 40, 10, 10);
        gtk_grid_attach (GTK_GRID (table1), chk1, 60, 60, 10, 10);
        gtk_grid_attach (GTK_GRID (table2), labelB, 0, 0, 10, 10);
        gtk_grid_attach (GTK_GRID (table2), btn2, 10, 10, 10, 10);
        gtk_grid_attach (GTK_GRID (table2), txt, 0, 20, 10, 10);
        gtk_grid_attach (GTK_GRID (table2), chk2, 20, 20, 10, 10);
    
    
        g_signal_connect (btn1, "clicked", G_CALLBACK (end_program), NULL);
        g_signal_connect (btn2, "clicked", G_CALLBACK (copy_text), labelB);
        g_signal_connect (btn2, "clicked", G_CALLBACK (copy_text), labelA);
        g_signal_connect (btn3, "clicked", G_CALLBACK (copy_text), labelB);
        g_signal_connect (btn3, "clicked", G_CALLBACK (copy_text), labelA);
        g_signal_connect (win2, "delete_event", G_CALLBACK (end_program), NULL);
        g_signal_connect (chk1, "toggled", G_CALLBACK (check_toggle),labelA);
    
        gtk_container_add (GTK_CONTAINER (win1), table1);
        gtk_container_add (GTK_CONTAINER (win2), table2);
    
        gtk_widget_show_all (win1);
        gtk_widget_show_all (win2);
        gtk_main ();
        return 0;
    }

    结果

  • 相关阅读:
    js 中读取JSON的方法探讨
    git 教程 git.oschina.net
    JS实现页面跳转重定向的几种方式
    AngularJs 学习
    firebug js版
    emmt html生成
    idea sass scss配置
    关于idea激活
    sublime text There are no packages 解决!
    js监听滚动条事件
  • 原文地址:https://www.cnblogs.com/goodluck14/p/13236894.html
Copyright © 2011-2022 走看看