获取GTK标签以在C中放置int [英] Getting a GTK Label to dislay an int in C

查看:76
本文介绍了获取GTK标签以在C中放置int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用整数值更新标签,具体取决于我按下减法还是加法按钮.但是,它不起作用,我很确定这与我传递 GTK_LABEL(sum)的方式以及我要更新为它的int的方式有关.

I'm trying to update a label with an integer value depending on whether I press a subtract or add button. Its not working however and I'm pretty sure it has something to do with how I pass my GTK_LABEL(sum) around, and the int I'm trying to update into it.

以前可行的方法是将地址传递给int到按钮函数,然后通过对 g_print 输入终端.com/questions/22384333/passing-additional-arguments-to-gtk-function>这个问题.

What worked before was passing an address to the int to the button function, and then g_printing it into the terminal, via the best answer to this question.

这是完整的错误消息,这是到目前为止的代码.

Here is the full error message, and here's my code so far.

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


void updateLabel(GTK_LABEL(sum), int num)
{
    gchar *display;
    display = g_strdup_printf("%d", num);         //convert num to str
    gtk_label_set_text (GTK_LABEL(sum), display); //set label to "display"
    g_free(display);                              //free display
}


void buttonAdd (GtkButton *button, GtkLabel *sum, gpointer num)
{
    num += 1;
    g_print("%i\n",num);
    updateLabel(GTK_LABEL(sum),num);
}

void buttonSub (GtkButton *button, GtkLabel *sum, gpointer num)
{ 
    num -= 1;
    g_print("%i\n",num);
    updateLabel(GTK_LABEL(sum),num);
}


int main (int argc, char **argv)
{
    signed int n = 0;
    char display = n;
    GtkWidget *window;
    GtkWidget *addButton;
    GtkWidget *subButton;  
    GtkWidget *grid;
    GtkWidget *sum;

    gtk_init (&argc,&argv);

    //Declarations
    window    = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    sum       = gtk_label_new (&display);
    grid      = gtk_grid_new ();
    addButton = gtk_button_new_with_label ("Add Value");
    subButton = gtk_button_new_with_label ("Sub Value");

    //Set Properties
    gtk_container_set_border_width (GTK_CONTAINER(window), 20);
    gtk_widget_set_size_request    (GTK_CONTAINER(window), 150, 100);
    gtk_label_set_selectable       (GTK_LABEL(sum), TRUE);
    gtk_grid_set_row_spacing       (GTK_GRID(grid), 4);
    gtk_grid_set_column_spacing    (GTK_GRID(grid), 4);
    gtk_container_add              (GTK_CONTAINER(window), grid);

    //Fill the grid with shit                  (x, y, h, v)
    gtk_grid_attach (GTK_GRID(grid), addButton, 0, 0, 1, 1);
    gtk_grid_attach (GTK_GRID(grid), subButton, 1, 0, 1, 1); 
    gtk_grid_attach (GTK_GRID(grid), sum,       0, 2, 2, 1);

    gtk_widget_show_all (window);

    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
    g_signal_connect(G_OBJECT(addButton), "clicked", G_CALLBACK(buttonAdd), &n);
    g_signal_connect(G_OBJECT(subButton), "clicked", G_CALLBACK(buttonSub), &n);
    gtk_main();

    return 0;
}

我认为需要特别注意的一点是,我是C语言的新手,并且我来自python,因此,指针,地址和GTK语法对我来说很神秘.简单的非专业的GTK指南在互联网上似乎也还不存在.如果有人可以告诉我我做错了什么,或者显示一种更好的方式,那就太棒了!!谢谢!

I think its important to note that I'm new to C, and I come from python, so pointers, addresses, and GTK syntax is pretty cryptic to me. Simple non-jargony GTK guides also seem to not exist on the internet yet too. If someone could show me what I'm doing wrong or a show a better way that would be awesome!! Thanks!

推荐答案

我修改了您的代码以使其如您所描述的那样工作:

I modified your code to work as you described:

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

void updateLabel(GtkLabel *sum, int num)
{
    gchar *display;
    display = g_strdup_printf("%d", num);         //convert num to str
    gtk_label_set_text (GTK_LABEL(sum), display); //set label to "display"
    g_free(display);                              //free display
}

void buttonAdd (GtkButton *button, GtkLabel *sum)
{
    int num = atoi(gtk_label_get_text(sum));
    num += 1;
    g_print("%d\n",num);
    updateLabel(GTK_LABEL(sum), num);
}

void buttonSub (GtkButton *button, GtkLabel *sum)
{
    int num = atoi(gtk_label_get_text(sum));
    num -= 1;
    g_print("%i\n",num);
    updateLabel(GTK_LABEL(sum),num);

}

int main(int argc, char **argv)
{
    GtkWidget *window;
    GtkWidget *addButton;
    GtkWidget *subButton;
    GtkWidget *grid;
    GtkWidget *sum;

    gtk_init (&argc,&argv);

    //Declarations
    window    = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    sum       = gtk_label_new ("0");
    grid      = gtk_grid_new ();
    addButton = gtk_button_new_with_label ("Add Value");
    subButton = gtk_button_new_with_label ("Sub Value");

    //Set Properties
    gtk_container_set_border_width (GTK_CONTAINER(window), 20);
    gtk_widget_set_size_request    (GTK_WIDGET(window), 150, 100);
    gtk_label_set_selectable       (GTK_LABEL(sum), TRUE);
    gtk_grid_set_row_spacing       (GTK_GRID(grid), 4);
    gtk_grid_set_column_spacing    (GTK_GRID(grid), 4);
    gtk_container_add              (GTK_CONTAINER(window), grid);

    //Fill the grid with shit                  (x, y, h, v)
    gtk_grid_attach (GTK_GRID(grid), addButton, 0, 0, 1, 1);
    gtk_grid_attach (GTK_GRID(grid), subButton, 1, 0, 1, 1);
    gtk_grid_attach (GTK_GRID(grid), sum,       0, 2, 2, 1);

    gtk_widget_show_all (window);

    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
    g_signal_connect(G_OBJECT(addButton), "clicked", G_CALLBACK(buttonAdd), sum);
    g_signal_connect(G_OBJECT(subButton), "clicked", G_CALLBACK(buttonSub), sum);

    gtk_main();

    return 0;
}

从一开始:

  • 尽管char可以用作int,但是您不能仅通过 char display = n; 将整个int转换为字符串,因为每个char都有与字符关联的自己的值,代表( http://www.asciitable.com/).字符串(char *或char [])由一个char表组成.

  • Although a char can be used as an int, you cannot convert a whole int to string simply by char display = n; as each char has it's own value associated with a character they represent (http://www.asciitable.com/). A string (char * or char[]) is made of a table of chars.

按钮的信号函数 buttonAdd() buttonSub 包含两个参数,但不包含三个参数:第一个是导致信号的gtk小部件的指针,而第二个参数是在 g_signal_connect()函数的最后一个参数中传递的指针.通常,我们会有大量数据通过指针传递,因此在这种情况下,我们将构造一个struct变量,填充并传递给它,但是在您的情况下,只需传递gtk标签就足够了,因为它已经包含了我们可以读取和增加/减少它的字符串.

Button's signal function buttonAdd() and buttonSub contains two parameters and not three: the first is a pointer of the gtk widget that caused the signal and the second parameter is a pointer passed in the last parameter from g_signal_connect() function. Normally we'd have a lot of data to pass trough the pointer so in that case we'd make a struct variable, fill it and pass that through, but in your case it's enough to just pass the gtk label, because it already contains the string which we can read and increase/decrease it.

我使用atoi()将字符串转换为int,但对于更健壮的解决方案,最好使用strtol,但对于您的示例而言,atoi()绰绰有余.

I used atoi() to convert the string to int, but for a more robust solution it's better to use strtol, but for your example atoi() is more than enough.

您不能将GTK_LABEL()用作参数,因为它是宏而不是类型.

You cannot use GTK_LABEL() as a parameter, because it's a macro and not a type.

这篇关于获取GTK标签以在C中放置int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆