gpt4 book ai didi

使用 gtk2 创建和存储图像?

转载 作者:行者123 更新时间:2023-11-30 14:41:40 26 4
gpt4 key购买 nike

我在使用 gtk2 绘制图像时遇到一些问题。我已经尝试过这段代码:

#include <gtk/gtk.h>

static gboolean button_press_callback (GtkWidget *event_box, GdkEventButton *event, gpointer data)
{
g_print ("Event box clicked at coordinates %f,%f\n",
event->x, event->y);

/* Returning TRUE means we handled the event, so the signal
* emission should be stopped (don't call any further
* callbacks that may be connected). Return FALSE
* to continue invoking callbacks.
*/
return TRUE;
}

static GtkWidget*
create_image (void)
{
GtkWidget *image;
GtkWidget *event_box;

image = gtk_image_new_from_file ("image.png");
}

int main(int argc, char const *argv[])
{
create_image();
return 0;
}

它不会在屏幕上绘制任何图像,事实上我根本看不到任何窗口。另外,将图像存储在变量中以供将来使用的最佳方法是什么?

最佳答案

建议你看看gtk教程https://developer.gnome.org/gtk-tutorial/stable/ ,您的代码缺少很多东西,无法在此处显示有关如何在窗口中显示简单图片的示例:

#include <gtk/gtk.h>

GtkWidget* create_gui()
{
GtkWidget *win = gtk_window_new(GTK_WINDOW_TOPLEVEL); // create the application window
GtkWidget *img = gtk_image_new_from_file("image.png"); // image shall be in the same dir
gtk_container_add(GTK_CONTAINER(win), img); // add the image to the window
g_signal_connect(G_OBJECT(win), "destroy", G_CALLBACK(gtk_main_quit), NULL); // end the application if user close the window
return win;
}
int main(int argc, char** argv) {
GtkWidget* win;

gtk_init(&argc, &argv);


win = create_gui();
gtk_widget_show_all(win); // display the window
gtk_main(); // start the event loop
return 0;
}

顺便说一句,gtk 2 不再维护,如果可以的话我建议您从 gtk3 开始

关于使用 gtk2 创建和存储图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54845612/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com