我想在我的gui中有一个图片元素,上面有文字。我的目标是加载图片(例如水滴),并在其上放置代表所测量的湿度(值来自MQTT)的文本。
什么是最好的方法?我不在乎它是标签还是其他任何元素(尽管我不满意为此使用按钮)。文本需要更改。我对这个框架非常陌生,所以我还没有掌握。
谢谢!
答案 0 :(得分:1)
这是示例代码。它使用叠加层堆叠两个小部件,即图像和标签:
#include <gtk/gtk.h>
int main(int argc, char *argv[]) {
GtkWidget *window;
GtkWidget *image;
GtkWidget *label;
GtkWidget *overlay;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_title(GTK_WINDOW(window), "Sandbox");
image = gtk_image_new_from_file("image.png");
label = gtk_label_new("I've always been too lame\n\
To see what's before me\n\
And I know nothing sweeter than\n\
Champaign from last New Years\n\
Sweet music in my ears\n\
And a night full of no fears\n\
\n\
But if I had one wish fulfilled tonight\n\
I'd ask for the sun to never rise\n\
If God passed a mic to me to speak\n\
I'd say \"Stay in bed, world,\n\
Sleep in peace");
overlay = gtk_overlay_new ();
gtk_container_add(GTK_CONTAINER(window), overlay);
gtk_overlay_add_overlay(GTK_OVERLAY(overlay), image);
gtk_overlay_add_overlay(GTK_OVERLAY(overlay), label);
g_signal_connect(G_OBJECT(window), "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all(window);
gtk_main();
return 0;
}