【问题标题】:Show icon or color in Gtk TreeView tree在 Gtk TreeView 树中显示图标或颜色
【发布时间】:2015-01-02 16:53:39
【问题描述】:

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

我的代码看起来像这样,但没有在同一目录中加载 icon.png 文件:

#!/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 的文档在这里:

这里有较早的 PyGTK 示例,但处理方式确实发生了变化:

【问题讨论】:

    标签: python treeview gtk3 pygobject gdkpixbuf


    【解决方案1】:

    这个问题可以像PyGTK2.0一样解决,需要在TreeViewColumn的一个对象上附加CellRendererTextCellRendererPixbuf两个对象,然后调用set_cell_data_func方法列设置单元格的数据返回功能。 这是很小的复杂(见下面的代码):

    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()
    

    您可以使用 pygtk2.0 的有关 TreeViewColumn.set_cell_data_func 方法的文档,并阅读有关 CellRenderers 和 PyGTK2.0 的特殊属性的重要页面,这些属性可用于 PyGTK 版本 3 :)

    【讨论】:

    • 感谢这个例子。我现在发现了我的错误。
    【解决方案2】:

    这里是一个完整的例子,展示了如何创建一个带有图标的树形商店(感谢 Ryan Paul):

    import gi
    gi.require_version('Gtk', '3.0')
    from gi.repository import Gtk as gtk
    from gi.repository import Gdk as gdk
    from gi.repository import GdkPixbuf
    import os, stat
    
    # Instantiate the tree store and specify the data types
    store = gtk.TreeStore(str, GdkPixbuf.Pixbuf, int, bool)
    
    def dirwalk(path, parent=None):
      # Iterate over the contents of the specified path
      for f in os.listdir(path):
        # Get the absolute path of the item
        fullname = os.path.join(path, f)
        # Extract metadata from the item
        fdata = os.stat(fullname)
        # Determine if the item is a folder
        is_folder = stat.S_ISDIR(fdata.st_mode)
        # Generate an icon from the default icon theme
        img = gtk.IconTheme.get_default().load_icon(
            "folder" if is_folder else "document",
            gtk.IconSize.MENU, 0)
        # Append the item to the TreeStore
        li = store.append(parent, [f, img, fdata.st_size, is_folder])
        # If the item is a folder, descend into it
        if is_folder: dirwalk(fullname, li)
    
    dirwalk("/path/to/folder")
    
    # Create a TreeViewColumn
    col = gtk.TreeViewColumn("File")
    # Create a column cell to display text
    col_cell_text = gtk.CellRendererText()
    # Create a column cell to display an image
    col_cell_img = gtk.CellRendererPixbuf()
    # Add the cells to the column
    col.pack_start(col_cell_img, False)
    col.pack_start(col_cell_text, True)
    # Bind the text cell to column 0 of the tree's model
    col.add_attribute(col_cell_text, "text", 0)
    # Bind the image cell to column 1 of the tree's model
    col.add_attribute(col_cell_img, "pixbuf", 1)
    
    # Create the TreeView and set our data store as the model
    tree = gtk.TreeView(store)
    # Append the columns to the TreeView
    tree.append_column(col)
    
    scroll = gtk.ScrolledWindow()
    scroll.add(tree)
    
    window = gtk.Window()
    window.connect("destroy", gtk.main_quit)
    window.add(scroll)
    window.set_default_size(400,400)
    window.show_all()
    gtk.main()
    

    【讨论】:

      【解决方案3】:

      显然,我所缺少的只是“pixbuf = 0”,表明 pixbuf 在第 0 列中。示例的这一行需要修改:

          px_column = Gtk.TreeViewColumn("Icon", px_renderer, pixbuf=0)
      

      【讨论】:

        猜你喜欢
        • 2012-12-10
        • 1970-01-01
        • 1970-01-01
        • 2016-09-10
        • 2011-05-11
        • 2020-10-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多