【问题标题】:Icons not displayed while using Gtk+3 ListStore使用 Gtk+3 ListStore 时图标不显示
【发布时间】:2018-04-08 01:43:10
【问题描述】:

我做了以下列表存储:

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

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

【问题讨论】:

    标签: python python-3.x gtk gtk3


    【解决方案1】:

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

    store = Gtk.ListStore(str, str, bool)
    
    treeviewcolumn.add_attribute(cellrendererpixbuf, "icon-name", 0)
    
    store.append('folder', file_name, True])
    

    文档是here

    【讨论】:

      【解决方案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.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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-04
        • 2020-07-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-02
        相关资源
        最近更新 更多