我尝试使用整数值更新标签,具体取决于我是按减号还是添加按钮。但是它没有工作,我很确定它与我如何通过我的 GTK_LABEL(总和)有关,而我正试图更新它。< / p>
之前的工作是将地址传递给按钮功能的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语法对我来说非常神秘。简单的非jargony GTK指南似乎也不存在于互联网上。如果有人可以告诉我我做错了什么,或者展示一个更好的方式,那就太棒了!! 谢谢!
答案 0 :(得分:1)
我将您的代码修改为按照您的描述工作:
#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 [])由字符表组成。
按钮的信号函数buttonAdd()
和buttonSub
包含两个参数而不是三个:第一个是引发信号的gtk小部件的指针,第二个参数是在来自g_signal_connect()
函数的最后一个参数。通常我们会通过指针传递大量数据,所以在这种情况下我们会创建一个struct变量,填充它并传递它,但在你的情况下,只需传递gtk标签,因为它已经包含我们可以阅读并增加/减少它的字符串。
我使用atoi()将字符串转换为int,但对于更强大的解决方案,最好使用strtol,但对于你的例子atoi()绰绰有余。
您不能将GTK_LABEL()用作参数,因为它是宏而不是类型。