在一个固定的布局GTK2放置图片 [英] Placing images in a fixed layout GTK2

查看:622
本文介绍了在一个固定的布局GTK2放置图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有尝试做以下的应用程序:


  • 创建一个 GTK2 顶级主窗口

  • 添加固定框架到主窗口小部件的绝对定位

  • 创建的矩阵 GtkImages 将被用于显示动画 GIFS 和静态 JPEG文件

  • 在启动时的静态 JPEGS 从列表中随机挑选将填补矩阵

  • 当一个事件发生的矩阵将充满动画 GIFS

  • 当动画结束可能不同的 JPEGS 将再次显示矩阵中的

当两个或两个以上随机选择 JPEG文件放置在矩阵的行

运行时错误仅发生。

下面是这样一个运行时间错误的例子:

 (错误:3909):GTK的WARNING **:无法设置父控件上有一个父

如果该行的每个图像都是独一无二的发生没有运行时错误。

code段和运行时输出如下:

  / *
 *编译我:
 * GCC -Wall -o错误wrong.c $(pkg配置--cflags --libs GTK + -2.0 gmodule-2.0)
 * // *头*包括// **** ****原型/
/ ****** /typedef结构
{
    unsigned int类型pixel_width,pixel_height;
    gchar文件名[20];
    * GtkWidget的形象;
} symbol_t;symbol_t符号[] =
{
    {118,107,图像/ LO.jpg,NULL},
    {118,107,图像/ L1.jpg,NULL},
    {118,107,图像/ L2.jpg,NULL},
    {118,107,图像/ L3.jpg,NULL},
    {118,107,图像/ H1.jpg,NULL},
    {118,107,图像/ H2.jpg,NULL},
    {118,107,图像/ H3.jpg,NULL},
    {118,107,图像/ H4.jpg,NULL},
    {118,107,图像/ H5.jpg,NULL}
};* GtkWidget的框架; / *窗口小部件的绝对定位* /
* GtkWidget的窗口;
INT初始化(无效)
{
    / *初始化随机数发生器* /
}静态无效销毁(* GtkWidget的窗口,gpointer数据)
{
  gtk_main_quit();
}GtkWidget的* SetupWindow(gchar *数据常量gchar *文件名)
{
    / *设置顶层窗口的背景设置为图像载
       在*文件名和返回窗口小部件
    * /    返回(窗口);
}INT主(INT ARGC,CHAR *的argv [])
{
    unsigned int类型Y,I,POS_X,POS_Y;    gtk_init(安培; ARGC,&安培; argv的);    在里面(); //初始化随机数发生器
    窗口= SetupWindow(破,图像/ background.jpg);
    g_signal_connect(G_OBJECT(窗口),消灭,G_CALLBACK(破坏),NULL);    帧= gtk_fixed_new();
    gtk_container_add(GTK_CONTAINER(窗口),帧);    / *设置标志JPG格式* /
    对于(i = 0; I< 9;我++)
    {
        / *每个符号图像加载到内存中* /
    符号由[i] =图像配gtk_image_new_from_file(符号[I] .fileName);
    }    / *显示一些符号* /
    POS_Y = 150;
    POS_X = 187;
    为(γ= 0; Y小于5; Y ++)/ *第一行 - 第5符号* /
    {
    I =(无符号整数)(随机()%9);
    的printf(符号[%D]。[%S] \\ n,我,符号[I] .fileName);
        gtk_fixed_put(GTK_FIXED(帧),符号[I]在图像配,POS_X,POS_Y);
    POS_X + =符号[I] .pixel_width;
    }    gtk_widget_show_all(窗口);
    进入主循环();
    返回0;
}

运行时错误当两个或多个匹配的符号(图像)被放置在该行:


  [渐]〜/来源/矩阵GT; 。/错误
符号[1] [图片/ L1.jpg]
符号[7] [图片/ H4.jpg]
符号[0] [图片/ LO.jpg]
符号[7] [图片/ H4.jpg](错误:3909):GTK的WARNING **:无法设置控件父有母符号[5] [图片/ H2.jpg](错误:3909):GTK的CRITICAL **:gtk_widget_destroy:断言`GTK_IS_WIDGET(部件)'失败(错误:3909):GTK的CRITICAL **:gtk_widget_destroy:断言`GTK_IS_WIDGET(部件)'失败


当这种情况发生时的一些行中的图像是空的(白色)。

当没有匹配的符号(图像)被放置在该行输出:


  [渐]〜/来源/矩阵GT; 。/错误
符号[1] [图片/ L1.jpg]
符号[6] [图片/ H3.jpg]
符号[3] [图片/ L3.jpg]
符号[0] [图片/ LO.jpg]
符号[4] [图片/ H1.jpg]


在这种情况下正确显示所有图像。

有关如何解决与任何建议,我可能是做错了?


解决方案
一旦你将图像放置到另一个窗口小部件

,它成为拥有和由(父)部件管理 - 你可以把它添加到一个以上的小部件

最简单的方式得到这个工作是装入图像gtk_image_new_from_file()您想将它添加到窗口各一次。如果你不想这样做,也许你可以将它添加到窗口小部件之前,使用类似 gtk_image_new_from_image()来复制图像。

I have an application that is attempting to do the following:

  • Create a GTK2 top level main window
  • Add a fixed frame into the main window for absolute positioning of widgets
  • Create a matrix of GtkImages that will be used to display animated GIFS and static JPEGS
  • On start-up the static JPEGS picked randomly from a list will fill the matrix
  • When an event happens the matrix will be filled with animated GIFS
  • When the animation is over possibly different JPEGS will again be displayed in the matrix

Run time errors are happening only when two or more of the randomly selected JPEGS are placed in a row of the matrix.

Here is an example of such a run time error:

(wrong:3909): Gtk-WARNING **: Can't set a parent on widget which has a parent

If each image of the row are unique no run time errors occur.

Code snippets and run time output are as follows:

/*
 * Compile me with:
 *   gcc -Wall -o wrong wrong.c $(pkg-config --cflags --libs gtk+-2.0 gmodule-2.0)
 */

/* header includes */

/**** prototypes ****/
/********************/

typedef struct
{
    unsigned int pixel_width, pixel_height;
    gchar fileName[20];
    GtkWidget *image;   
}symbol_t;

symbol_t symbols[] =
{
    { 118, 107, "images/LO.jpg", NULL },
    { 118, 107, "images/L1.jpg", NULL },
    { 118, 107, "images/L2.jpg", NULL },
    { 118, 107, "images/L3.jpg", NULL },
    { 118, 107, "images/H1.jpg", NULL },
    { 118, 107, "images/H2.jpg", NULL },
    { 118, 107, "images/H3.jpg", NULL },
    { 118, 107, "images/H4.jpg", NULL },
    { 118, 107, "images/H5.jpg", NULL }
};

GtkWidget *frame;       /* for absolute positioning of widgets */
GtkWidget *window;


int Init( void )
{
    /* initialize random number generator */
}

static void destroy (GtkWidget *window, gpointer data)
{
  gtk_main_quit ();
}

GtkWidget *SetupWindow(gchar *data, const gchar *filename)
{
    /* setup top-level window setting the background to the image contained
       in *filename and return window widget
    */

    return(window);
}

int main (int argc, char *argv[])
{
    unsigned int y, i, pos_x, pos_y;    

    gtk_init (&argc, &argv);

    Init(); // init random number generator
    window = SetupWindow("Broken", "images/background.jpg");
    g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy), NULL);

    frame = gtk_fixed_new();
    gtk_container_add(GTK_CONTAINER(window), frame);

    /* setup symbol jpgs */
    for( i = 0; i < 9; i++ )
    {
        /* load each symbol image into memory */
    symbols[i].image = gtk_image_new_from_file( symbols[i].fileName ); 
    }

    /* display some symbols */
    pos_y = 150;
    pos_x = 187;
    for( y = 0; y < 5 ; y++ )  /* first row - 5 symbols */
    {
    i = (unsigned int)(random()%9);
    printf("Symbol[%d] [%s]\n", i, symbols[i].fileName);
        gtk_fixed_put(GTK_FIXED(frame), symbols[i].image, pos_x, pos_y);
    pos_x += symbols[i].pixel_width;
    }

    gtk_widget_show_all(window);
    gtk_main ();
    return 0;
}

Run time errors when two or more matching symbols ( images ) are placed on the row:

[chim] ~/source/matrix > ./wrong 
Symbol[1] [images/L1.jpg]
Symbol[7] [images/H4.jpg]
Symbol[0] [images/LO.jpg]
Symbol[7] [images/H4.jpg]

(wrong:3909): Gtk-WARNING **: Can't set a parent on widget which has a parent

Symbol[5] [images/H2.jpg]

(wrong:3909): Gtk-CRITICAL **: gtk_widget_destroy: assertion `GTK_IS_WIDGET (widget)' failed

(wrong:3909): Gtk-CRITICAL **: gtk_widget_destroy: assertion `GTK_IS_WIDGET (widget)' failed

When this occurs some of the images in the row are empty ( white ).

Output when no matching symbols ( images ) are placed on the row:

[chim] ~/source/matrix > ./wrong 
Symbol[1] [images/L1.jpg]
Symbol[6] [images/H3.jpg]
Symbol[3] [images/L3.jpg]
Symbol[0] [images/LO.jpg]
Symbol[4] [images/H1.jpg]

And in this case all images are displayed properly.

Any suggestions about how to fix and what I might be doing wrong?

解决方案

Once you place the image into another widget, it becomes owned and managed by that (parent) widget -- you can't add it to more than one widget.

The simple way to get this to work is to load the image with gtk_image_new_from_file() each time you want to add it to the window. If you don't want to do that, maybe you can use something like gtk_image_new_from_image() to copy the image prior to adding it to the widget.

这篇关于在一个固定的布局GTK2放置图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆