如何在树视图中动态调整pixbuf cellrenderer的大小? [英] how do I dynamically resize a pixbuf cellrenderer in a treeview?

查看:44
本文介绍了如何在树视图中动态调整pixbuf cellrenderer的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用类似上面的Gtk3 TreeView.该模型是Gtk.TreeStore

I'm using a Gtk3 TreeView that looks like above. The model is a Gtk.TreeStore

  • Gtk.TreeStore(str,GdkPixbuf.Pixbuf)

对于图片,我可以通过以下方式将正确尺寸的图像添加到模型中:

For the picture I could add to the model the correctly sized images via:

  • pixbuf.scale_simple(48,48,GdkPixbuf.InterpType.BILINEAR)

但是我还在其他地方使用该模型以不同的方式显示像素缓冲区,并且像素缓冲区也可以是各种大小.

However I'm also using the model elsewhere showing the pixbufs in a different manner and the pixbufs can also be a variety of sizes.

我想做的是强制在运行时显示图片的大小.问题是-我该怎么做?

What I would like to do is force the size of the picture displayed at run-time. The question is - how do I do that?

我尝试强制将GtkCellRendererPixbuf设置为固定大小,但这只会显示正确大小的图像-但只显示图像中与固定大小对应的部分

I tried forcing the GtkCellRendererPixbuf to be a fixed size, but this just displays the correct size image - but only the portion of the image corresponding to the fixed-size

pixbuf = Gtk.CellRendererPixbuf()
pixbuf.set_fixed_size(48,48)

我想到了使用TreeViewColumn的 set_cell_data_func :

I thought of using the set_cell_data_func of the TreeViewColumn:

col = Gtk.TreeViewColumn('', pixbuf, pixbuf=1)
col.set_cell_data_func(pixbuf, self._pixbuf_func, None)

def _pixbuf_func(self, col, cell, tree_model, tree_iter, data):
    cell.props.pixbuf = cell.props.pixbuf.scale_simple(48,48,GdkPixbuf.InterpType.BILINEAR)

这确实在运行时对图像进行了动态调整大小-但在终端中,我却遇到了数百种错误,例如:

This does do the dynamic resizing of the image at runtime - BUT in the terminal I get hundreds of errors such as this:

sys:1: RuntimeWarning: Expecting to marshal a borrowed reference for <Pixbuf object at 0x80acbe0 (GdkPixbuf at 0x87926d0)>, but nothing in Python is holding a reference to this object. See: https://bugzilla.gnome.org/show_bug.cgi?id=687522

我还尝试了调整树模型pixbuf的大小,而不是 cell.props.pixbuf 的大小,但这也产生了与上述相同的错误.

I also tried the alternative by resizing treemodel pixbuf instead of the cell.props.pixbuf but this also gives the same error as above.

cell.props.pixbuf = tree_model.get_value(tree_iter,1).scale_simple(48,48,GdkPixbuf.InterpType.BILINEAR)

很明显,这不是正确的方法-因此,您有什么其他想法吗?到示例代码基于Gtk3的C ++/Python的任何链接都将受到欢迎.

So obviously this is not the correct way of doing this - so any thoughts how else to approach this? Any links to example code Gtk3 based C++/Python would be most welcome.

我正在使用Gtk + 3.6/python 2.7

I'm using Gtk+3.6 / python 2.7

推荐答案

旧问题,但遗憾的是仍然存在-Gtk中的此错误仍然存​​在.幸运的是,有一个非常简单的解决方法.您需要做的只是保持对缩放后的pixbuf的引用.

Old question, but sadly still relevant - this bug in Gtk still exists. Fortunately there's a pretty easy workaround. All you need to do is to keep a reference to the scaled pixbuf.

我已经更改了 _pixbuf_func 函数,以便它接受存储小pixbuf的字典.这样就消除了烦人的警告消息,并且还防止了每次调用 _pixbuf_func 时pixbuf的尺寸缩小.

I've altered the _pixbuf_func function so that it accepts a dictionary in which it stores the small pixbufs. This gets rid of the annoying warning message and also prevents the pixbuf from being downscaled every single time _pixbuf_func is called.

def pixbuf_func(col, cell, tree_model, tree_iter, data):
    pixbuf= tree_model[tree_iter][1] # get the original pixbuf from the TreeStore. [1] is
                                     # the index of the pixbuf in the TreeStore.
    try:
        new_pixbuf= data[pixbuf] # if a downscaled pixbuf already exists, use it
    except KeyError:
        new_pixbuf= pixbuf.scale_simple(48,48,GdkPixbuf.InterpType.BILINEAR)
        data[pixbuf]= new_pixbuf # keep a reference to this pixbuf to prevent Gtk warning
                                 # messages
    cell.set_property('pixbuf', new_pixbuf)

renderer = Gtk.CellRendererPixbuf()
col = Gtk.TreeViewColumn('', renderer)
col.set_cell_data_func(renderer, pixbuf_func, {}) # pass a dict to keep references in

此解决方案的一个问题是,只要TreeStore的内容发生更改,就必须从dict中删除存储的Pixbuf,否则它们将永远存在,并且程序将消耗比必要更多的内存.

A problem with this solution is that you have to remove the stored Pixbufs from the dict whenever the contents of your TreeStore change, otherwise they'll stick around forever and your program will consume more memory than necessary.

这篇关于如何在树视图中动态调整pixbuf cellrenderer的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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