如何使用GTK将图像添加到按钮

Edg*_*mez 2 c++ linux-mint gtk3

我试图将图像添加到带标签的按钮,但图像不显示,破碎的图像也不显示.

stop_button = gtk_button_new_with_label("stop");
image = gtk_image_new_from_file ("/home/cendit/Escritorio/index.jpeg");
gtk_button_set_image (GTK_BUTTON(stop_button),image);
Run Code Online (Sandbox Code Playgroud)

我尝试了另一条路径"file:///home/cendit/Escritorio/index.jpeg",但没有成功.

eba*_*ssi 5

默认情况下,按钮内的图像不可见,因为我们从GTK + 2.x转换为3.x. 遗憾的是,API尚未清理以反映此更改,因此它有点陷阱.

如果要显示包含图像的按钮,可以使用:

GtkWidget *image = gtk_image_new_from_file ("...");
GtkWidget *button = gtk_button_new ();

gtk_button_set_image (GTK_BUTTON (button), image);
Run Code Online (Sandbox Code Playgroud)

另一方面,如果您想要一个包含文本和图像的按钮,您可以使用:

GtkWidget *image = gtk_image_new_from_file ("...");
GtkWidget *button = gtk_button_new_with_label ("...");

gtk_button_set_always_show_image (GTK_BUTTON (button), TRUE);
gtk_button_set_image (GTK_BUTTON (button), image);
Run Code Online (Sandbox Code Playgroud)

有关gtk_button_set_image()详细信息,请参阅文档.