在Gtk TreeView树中显示图标或颜色 [英] Show icon or color in Gtk TreeView tree

查看:290
本文介绍了在Gtk TreeView树中显示图标或颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Gtk TreeView(GTK3的Python绑定)的其中一列中加载文件或显示颜色时遇到困难.从QGIS中获取的示例在第一行中显示一个图标,在第二行中显示一个蓝色圆圈.颜色来自图层属性:

I am having difficulty loading a file or displaying a colour in one of the columns of a Gtk TreeView (Python binding of GTK3). An example taken from QGIS shows a icon in the first row and a blue circle in the second row. The colour is taken from the layer properties:

我的代码如下所示,但是没有将icon.png文件加载到同一目录中:

My code looks like this but does not load the icon.png file in the same directory:

#!/usr/bin/python3
from gi.repository import Gtk, Gdk, GdkPixbuf

class MyWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_default_size(200, 200)

        self.liststore = Gtk.ListStore(GdkPixbuf.Pixbuf, str)
        self.treeview = Gtk.TreeView(model=self.liststore)

        symbol1 = GdkPixbuf.Pixbuf.new_from_file("icon.png")
        self.liststore.append([symbol1, "This is a symbol1"])

        symbol2 = Gtk.IconTheme.get_default().load_icon("gtk-cut", 64, 0)
        self.liststore.append([symbol2, "This is symbol2"])

        px_renderer = Gtk.CellRendererPixbuf()
        px_column = Gtk.TreeViewColumn("Icon", px_renderer)
        self.treeview.append_column(px_column) 

        str_renderer = Gtk.CellRendererText()
        str_column = Gtk.TreeViewColumn("Name", str_renderer, text=1)
        self.treeview.append_column(str_column)

        self.add(self.treeview)

win = MyWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()

GTK3 pixbuf的文档在这里:

The documentation for GTK3 pixbuf is here:

PyGTK的旧示例在这里,但是在处理方式上确实发生了一些变化:

Older examples for PyGTK are here, but something has really changed in how this is handled:

  • http://faq.pygtk.org/index.py?file=faq13.006.htp&req=show
  • http://www.daa.com.au/pipermail/pygtk/2003-August/005644.html

推荐答案

此问题可以与PyGTK2.0相似地解决,您需要在TreeViewColumn的一个对象上附加CellRendererTextCellRendererPixbuf的两个对象,然后然后调用列的 set_cell_data_func 方法来设置单元格的数据返回器功能. 这是非常小的复杂(请参见下面的代码):

This problem can resolve similar as PyGTK2.0, you need to attach two objects of CellRendererText and CellRendererPixbuf on the one object of TreeViewColumn and then call set_cell_data_func method of column to set data returner function of cells. this is tiny complex(see below code):

from gi.repository import Gtk, Gdk, GdkPixbuf

class MyWindow(Gtk.Window):
    def __init__(self):
        Gtk.Window.__init__(self)
        self.set_default_size(200, 200)

        self.liststore = Gtk.ListStore(str, str)
        self.treeview = Gtk.TreeView(model=self.liststore)

        self.liststore.append(["icon.png", "This is a symbol1"])

        px_renderer = Gtk.CellRendererPixbuf()
        px_column = Gtk.TreeViewColumn('')
        px_column.pack_start(px_renderer, False)
        str_renderer = Gtk.CellRendererText()
        px_column.pack_start(str_renderer, False)
        # set data connector function/method
        px_column.set_cell_data_func(px_renderer, self.get_tree_cell_pixbuf)
        px_column.set_cell_data_func(str_renderer, self.get_tree_cell_text)
        self.treeview.append_column(px_column)

        self.add(self.treeview)

    def get_tree_cell_text(self, col, cell, model, iter, user_data):
        cell.set_property('text', model.get_value(iter, 1))

    def get_tree_cell_pixbuf(self, col, cell, model, iter, user_data):
        cell.set_property('pixbuf', GdkPixbuf.Pixbuf.new_from_file(model.get_value(iter, 0)))

if __name__ == '__main__':
    win = MyWindow()
    win.connect("delete-event", Gtk.main_quit)
    win.show_all()
    Gtk.main()

您可以使用有关 TreeViewColumn.set_cell_data_func 方法并阅读有关 和PyGTK2.0的特殊属性,这些属性可以在PyGTK的第3版中使用:)

you can use documents of pygtk2.0 about TreeViewColumn.set_cell_data_func method and read important page about CellRenderers and special properties of PyGTK2.0 that can useful on version 3 of PyGTK :)

这篇关于在Gtk TreeView树中显示图标或颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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