简体   繁体   English

如何在GTK3中向按钮添加图标?

[英]How to add an icon to a button in GTK3?

I am using Glade to generate C-based GUI's. 我正在使用Glade生成基于C的GUI。 I have a GtkButton in my UI and would like to display an icon on it. 我的UI中有一个GtkButton,想在其上显示一个图标。

In glade there seems to be an option on the General properties of the GtkButton called 'Label with optional image'. 在林间空地中,GtkButton的常规属性上似乎有一个名为“带有可选图像的标签”的选项。 However, I can't get this to work. 但是,我无法使它正常工作。

On the main() I have: 在main()上,我有:

button = gtk_builder_get_object (builder, "start_button");
button = gtk_button_new_from_icon_name ("document-new-symbolic", GTK_ICON_SIZE_BUTTON);

In the same folder where i run the binary file, I have a small document-new-symbolic.png. 在运行二进制文件的同一文件夹中,我有一个小的document-new-symbolic.png。 On the docs it says that "If the icon name isn't known, a “broken image” icon will be displayed instead." 文档上说“如果不知道图标名称,则会显示“残破的图像”图标。” However, in my case, just the normal button appears without any icon. 但是,就我而言,只显示普通按钮而没有任何图标。

What is going on? 到底是怎么回事? Why is the icon not displayed? 为什么不显示图标?

Try 尝试

GtkWidget *image = gtk_image_new_from_file("document-new-symbolic.png");
gtk_button_set_image(button, image);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM