C更新GTK3的GUI元素

时间:2017-08-28 16:53:25

标签: c gtk

#include <gtk/gtk.h>

GtkBuilder *builder;
GtkWidget *window;
GtkImage *image;
GtkButton *but;
char s[1000];

void on_button1_button_press_event(GtkWidget *but, gpointer  data)
{
    strcpy(s, "/home/linux/testing2.png");
    gtk_widget_queue_draw (image);
}

int main(void)
{
    gtk_init(NULL, NULL);
    builder = gtk_builder_new();
    gtk_builder_add_from_file(builder, "/home/linux/gui.xml", NULL);
    window = (GtkWidget *) gtk_builder_get_object(builder, "window1");
    image = (GtkImage *) gtk_builder_get_object(builder, "image1");
    but = (GtkButton *) gtk_builder_get_object(builder, "button1");
    gtk_image_set_from_file(image, strcpy(s, "/home/linux/testing1.png"));
    g_signal_connect(but, "clicked", G_CALLBACK(on_button1_button_press_event), NULL);
    gtk_widget_show(window);
    gtk_main();

    return 0;
}

当我点击“button1”,调用“回调”函数,并且它更新了字符串s然后我想用gtk_widget_queue_draw刷新“image1”。但是“image1”并没有改变。

1 个答案:

答案 0 :(得分:0)

s采用带有图像文件名的字符串。但它不会保留你传递的字符串。相反,小部件存储整个加载的图像。

所以你的main()变量是没用的,改变它什么都不做。你应该在gtk_image_set_from_file(image, "/home/linux/testing1.png");

中做
gtk_image_set_from_file(image, "/home/linux/testing2.png");

并在事件回调中:

gtk_widget_queue_draw()

无需拨打first_list = ['2017-04-30', '2012-09-30', '2016-09-30', '2012-10-31'] second_list = ['2012-04-30', '2012-05-31', '2012-06-30', '2012-07-31', '2017-04-30', '2017-05-31', '2017-06-30', '2017-07-31', '2017-07-31'] for each_date in first_list: if each_date in second_list: index = second_list.index(each_date) print "For the date ", each_date, " next 3 dates are: ", second_list[index+1], second_list[index+2], second_list[index+3] ,因为更改图片会自动执行此操作。