2013-11-04 45 views
2

pic如何动态调整树视图中pixbuf cellrenderer的大小?

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

  • Gtk.TreeStore(STR,GdkPixbuf.Pixbuf)

对于我可以通过添加到模型的正确大小的图像的图片:

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

但是我还使用该模型的其他地方示出了在不同的曼pixbufs呃和pixbufs也可以是各种尺寸。

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

我试图迫使GtkCellRendererPixbuf为固定尺寸,但是这只是显示正确的尺寸的图像 - 但仅对应于固定大小

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

所述图像的所述部分我想使用set_cell_data_func的在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) 

这确实做在运行时,图像的动态调整 - 但在终端,我得到数百个错误,比如这个:

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 

我也尝试了通过调整treemodel pixbuf而不是cell.props.pixbuf的方法来改变大小,但这也给出了与上面相同的错误。

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

所以显然这不是这样做的正确方法 - 所以任何想法如何办法呢?任何链接到示例代码基于Gtk3的C++/Python将是最受欢迎的。

我使用GTK + 3.6/2.7蟒

回答

1

老问题,但遗憾的是仍然具有现实意义 - GTK中这个bug依然存在。幸运的是,有一个非常简单的解决方法。所有你需要做的就是保持对缩放的pixbuf的引用。

我已经改变了_pixbuf_func函数,以便它接受一个存储小pixbufs的字典。这消除了恼人的警告消息,并且还防止每调用一次_pixbuf_func就调用pixbuf。

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的内容,否则他们将永远留下来,你的程序会占用更多的内存的字典中删除存储Pixbufs。