Glade 3和C,按钮和文本框的信号处理

时间:2013-03-11 10:10:12

标签: c gtk glade

我是Glade的初学者,但有C编程经验。

我正在尝试构建一个接受两个值并显示总和的项目。

我使用了以下内容:

textbox1 - (entry1) ---- for first value,
textbox2 - (entry2) ---- for 2nd value,
textbox3 - (entry3) ---- to display result,
button - (button1) ---- to calculate sum,
signal - (on_button1_clicked) ---- to handle sum function.

请帮我处理信号代码。

#include <gtk/gtk.h>
#include <stdio.h>

    GtkBuilder *builder;
    GtkWidget  *window;
    GError     *error = NULL;

void on_button1_clicked
{

// Need Help in this section

}

void on_window1_destroy (GtkObject *object, gpointer user_data)
{
gtk_main_quit();
}

int main( int argc, char **argv )
{

    /* Init GTK+ */
    gtk_init( &argc, &argv );

    /* Create new GtkBuilder object */
    builder = gtk_builder_new();
    /* Load UI from file. If error occurs, report it and quit application.
     * Replace "tut.glade" with your saved project. */
    if( ! gtk_builder_add_from_file( builder, "sum.glade", &error ) )
    {
        g_warning( "%s", error->message );
        g_free( error );
        return( 1 );
    }

    /* Get main window pointer from UI */
    window = GTK_WIDGET( gtk_builder_get_object( builder, "window1" ) );

    /* Connect signals */
    gtk_builder_connect_signals( builder, NULL );

    /* Destroy builder, since we don't need it anymore */
    g_object_unref( G_OBJECT( builder ) );

    /* Show window. All other widgets are automatically shown by GtkBuilder */
    gtk_widget_show( window );

    /* Start main loop */
    gtk_main();

    return( 0 );
}

Sample code image

3 个答案:

答案 0 :(得分:1)

我尝试了这些东西,并在一些帮助下可以使它工作。您需要参考Click

答案 1 :(得分:1)

#include <gtk/gtk.h>
#include<stdio.h>
#include<stdlib.h>
#include <string.h>

/*i have grouped all the widgets in one structure. this technique will 
help if you have huge number of  windows in your project. 
as the number of window will increase the variable we declare will increase
 and the declared variables can have same name of different windows */
struct login_window
{
   GtkEntry *e1;//textbox1
   GtkEntry *e2;//textbox2
   GtkEntry *e3;//textbox3
};
typedef struct login_window widgets;


G_MODULE_EXPORT void on_window1_destroy ()
{
    gtk_main_quit ();
}


G_MODULE_EXPORT void on_button1_clicked ( GtkButton *button,widgets *widg )
{
    char ch1[5],ch2[5],ch[5];
    int a;
//getting text from the text boxes
 const gchar *text1 = gtk_entry_get_text( widg->e2 );
 const gchar *text2 = gtk_entry_get_text( widg->e1 );

 //copying the text of textbox into an array
 strcpy(ch1,text1);
 strcpy(ch2,text2);

 //converting characters to integer and adding them
 a=(atoi(ch1))+(atoi(ch2));

itoa(a,ch,5);//converting integer to character
 gtk_entry_set_text (widg->e3,ch);//setting or displaying text(output) to the third text box
}

int main(int argc, char *argv[])
{
    GtkBuilder *gtkBuilder;
    widgets   widg;
    GtkWidget  *window;

    gtk_init(&argc, &argv);
    gtkBuilder = gtk_builder_new();

    gtk_builder_add_from_file(gtkBuilder, "window.glade", NULL);


    window = GTK_WIDGET( gtk_builder_get_object( gtkBuilder, "window1") );

//connecting each structure member to it appropriate gtkwidget
    widg.e1 = GTK_ENTRY( gtk_builder_get_object( gtkBuilder, "entry1" ) ); 
    widg.e2 = GTK_ENTRY( gtk_builder_get_object( gtkBuilder, "entry2" ) );
    widg.e3 = GTK_ENTRY( gtk_builder_get_object( gtkBuilder, "entry3" ) );

/*connecting signals so that appropriate handlers can be called when button is clicked*/
    gtk_builder_connect_signals( gtkBuilder, &widg );

    g_object_unref(G_OBJECT(gtkBuilder));
    gtk_widget_show(window);//showing the window
    gtk_main();

    return 0;
}

答案 2 :(得分:0)

我不会告诉你到底该怎么做,但是从逻辑上思考你的问题:你需要

  1. entry1entry2
  2. 中获取文字
  3. 将字符串转换为整数。
  4. 将它们添加到一起
  5. 将答案转换为字符串
  6. entry3
  7. 上设置文字

    我确信您可以在文档中找到一些有用的功能来执行此操作的Gtk +部分:GtkEntry documentation