【问题标题】:Creating and storing images with gtk2?使用 gtk2 创建和存储图像?
【发布时间】:2019-02-23 19:55:43
【问题描述】:

我在使用 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+ 2。 GTK+ 3 已经发布 8 年了,GTK+ 4 指日可待,很有可能在 2019 年发布。此外,绘图发生了变化,cairo 库是 GTK+ 3 中的一等公民。

标签: c gtk gtk2


【解决方案1】:

我建议你看看 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 开始

【讨论】:

  • 它不起作用。我收到大量警告。一次有两个版本的gtk可以吗
  • 在带有 gcc 的 linux 上,我没有任何问题,只要确保你的编译标志。对于这个例子,我使用了 gtk2 和以下命令行:gcc `pkg-config --cflags gtk+-2.0` -o example example.c `pkg-config --libs gtk+-2.0`
猜你喜欢
  • 2021-09-24
  • 1970-01-01
  • 1970-01-01
  • 2013-02-16
  • 1970-01-01
  • 2017-01-09
  • 2011-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多