gpt4 book ai didi

c - 获取 GTK 标签以在 C 中显示一个 int

转载 作者:太空宇宙 更新时间:2023-11-04 03:43:55 24 4
gpt4 key购买 nike

我正在尝试根据我按下的是减法还是加法按钮来更新带有整数值的标签。但是它不起作用,我很确定它与我如何传递我的 GTK_LABEL(sum) 以及我尝试更新到它的 int 有关。

之前的工作是将一个地址传递给 int 到按钮函数,然后通过对 this question 的最佳答案将其g_print输入终端。 .

这是完整的 error message ,这是我目前的代码。

#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 指南似乎在 Internet 上也不存在。如果有人可以告诉我我做错了什么或者展示一个更好的方式,那就太棒了!谢谢!

最佳答案

我修改了您的代码以按照您的描述工作:

#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 转换为 string,因为每个 char 都有自己的值,并与它们对应的字符相关联代表 ( http://www.asciitable.com/ )。字符串(char * 或 char[])由字符表组成。

  • Button 的信号函数 buttonAdd()buttonSub 包含两个参数而不是三个:第一个是导致信号的 gtk 小部件的指针,第二个参数是从 g_signal_connect() 函数传入最后一个参数的指针。通常我们会有很多数据要通过指针传递,所以在那种情况下我们会创建一个结构变量,填充它并传递它,但在你的情况下它只传递 gtk 标签就足够了,因为它已经包含我们可以读取和增加/减少它的字符串。

  • 我使用 atoi() 将字符串转换为 int,但为了获得更强大的解决方案,最好使用 strtol,但对于您的示例来说,atoi() 已经足够了。

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

关于c - 获取 GTK 标签以在 C 中显示一个 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26429173/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com