使用Gtk + 3 ListStore时不显示图标

时间:2018-04-08 01:43:11

标签: python python-3.x gtk gtk3

我制作了以下列表商店:

    # creating list view
    store = Gtk.ListStore(GdkPixbuf.Pixbuf, str, bool)
    self.get_files(store)
    treeview = Gtk.TreeView(store)
    treeview.connect("button_press_event", self.on_button1_clicked)
    horizontal_box.pack_start(treeview, True, True, 0)

    cellrenderertext = Gtk.CellRendererText()
    cellrendererpixbuf = Gtk.CellRendererPixbuf()

    #column 0 of list view to display icons
    treeviewcolumn = Gtk.TreeViewColumn("Icon")
    treeview.append_column(treeviewcolumn)
    treeviewcolumn.pack_start(cellrendererpixbuf, True)
    treeviewcolumn.add_attribute(cellrendererpixbuf, "pixbuf", 0)

    #column 1 of list view to display names
    treeviewcolumn = Gtk.TreeViewColumn("Name")
    treeview.append_column(treeviewcolumn)
    treeviewcolumn.pack_start(cellrenderertext, True)
    treeviewcolumn.add_attribute(cellrenderertext, "text", 1)

    #column 2 of list view to display if it is directory
    treeviewcolumn = Gtk.TreeViewColumn("Is Dir?")
    treeview.append_column(treeviewcolumn)
    treeviewcolumn.pack_start(cellrenderertext, True)
    treeviewcolumn.add_attribute(cellrenderertext, "text", 2)

我正在使用以下函数来填充商店:

def get_files(self, store):
    for file_name in os.listdir(CURRENT_DIRECTORY):

        # modified_time = os.path.getmtime(file_name)
        if not file_name[0] == '.': 
            if os.path.isdir(os.path.join(CURRENT_DIRECTORY, file_name)):
                store.append([Gtk.Image.new_from_stock(Gtk.STOCK_DIRECTORY, Gtk.IconSize.MENU), file_name, True])
            else:
                store.append([Gtk.Image.new_from_stock(Gtk.STOCK_FILE, Gtk.IconSize.MENU), file_name, False])

输出正确显示列表名称和'是目录'但它没有显示图标。我在这里做错了什么?

2 个答案:

答案 0 :(得分:1)

您正在将图像设置为pixbuf。相反,我会使用它(浓缩显示更改):

store = Gtk.ListStore(str, str, bool)

treeviewcolumn.add_attribute(cellrendererpixbuf, "icon-name", 0)

store.append('folder', file_name, True])

文档为here

答案 1 :(得分:0)

这是一个完整的示例,说明如何创建列表存储,其中图标由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.ListStore(str, GdkPixbuf.Pixbuf, int, bool)

def list_files(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([f, img, fdata.st_size, is_folder])
    # If the item is a folder, descend into it
    
list_files("/path/to/files")

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

enter image description here