tkinter.ttk.Treeview根节点图标/图像未出现 [英] tkinter.ttk.Treeview root node icon/image does not appear

查看:743
本文介绍了tkinter.ttk.Treeview根节点图标/图像未出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:我无法在tkinter.ttk.Treeview中的根节点旁边显示图标图像.以下是我使用的测试代码.它执行了w/o错误,但图像未出现在根节点的左侧.我尝试使用图像文件的完整路径名,但是没有用.另外,我尝试使用PIL.ImageTk.PhotoImage打开图像文件,但是也没有用.而是出现了如下所示的错误.

Problem: I am not able to get an icon image to appear next to the root node in tkinter.ttk.Treeview. Below is a test code I used. It executed w/o errors but image did not appear on the left of root node. I have tried using the full path name of the image file but that did not work. Also, I have tried using PIL.ImageTk.PhotoImage to open the image file but that did not work either. Instead an error as shown below appeared.

问题:如何使图标图像显示在tkinter.ttk.Treeview的根节点(或任何节点)的左侧?

Question: How do I get an icon image to appear on the left of the root node (or any node) of tkinter.ttk.Treeview?

测试代码:

import os
import tkinter as tk
import tkinter.ttk as ttk
from PIL import Image, ImageTk

class App(ttk.Frame):

    def __init__(self, master, path):
    ttk.Frame.__init__(self, master)
    self.tree = ttk.Treeview(self)
    ysb = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview)
    xsb = ttk.Scrollbar(self, orient='horizontal', command=self.tree.xview)
    self.tree.configure(yscroll=ysb.set, xscroll=xsb.set)
    self.tree.heading('#0', text='Directory', anchor='w')

    abspath = os.path.abspath(path)
    i = './icon/Home-icon_16.gif'
    root_pic = tk.PhotoImage(file=i)
    #root_pic = ImageTk.PhotoImage(i)
    root_node = self.tree.insert('', 'end', text=abspath, open=True, image=root_pic)
    l1_node = self.tree.insert(root_node, 'end', text='level 1', open=True)
    l2_node = self.tree.insert(l1_node, 'end', text='level 2', open=True)
    l3_node = self.tree.insert(l2_node, 'end', text='level 3', open=True)
    l2a_node = self.tree.insert(l1_node, 'end', text='level 2a', open=True)
    l3a_node = self.tree.insert(l2a_node, 'end', text='level 3a', open=True)

    self.tree.grid(row=0, column=0)
    ysb.grid(row=0, column=1, sticky='ns')
    xsb.grid(row=1, column=0, sticky='ew')
    self.grid()

root = tk.Tk()
path_to_my_project = os.getcwd()
app = App(root, path=path_to_my_project)
app.mainloop()

使用PIL.ImageTk.PhotoImage导致的错误消息:

root_pic = ImageTk.PhotoImage(i)
  File "/usr/lib/python3/dist-packages/PIL/ImageTk.py", line 108, in __init__
    mode = Image.getmodebase(mode)
  File "/usr/lib/python3/dist-packages/PIL/Image.py", line 296, in getmodebase
    return ImageMode.getmode(mode).basemode
  File "/usr/lib/python3/dist-packages/PIL/ImageMode.py", line 52, in getmode
    return _modes[mode]
KeyError: './icon/Home-icon_16.gif'

Home-icon_16.gif :

应用程序:python3.5 ver3.5.1-10; python3-tk ver3.5.1-1; tk8.6 ver8.6.5-1; python3-pil.imagetk:amd64 ver3.1.2-0ubuntu1

Applications: python3.5 ver3.5.1-10; python3-tk ver3.5.1-1; tk8.6 ver8.6.5-1; python3-pil.imagetk:amd64 ver3.1.2-0ubuntu1

推荐答案

尝试首先使用Image.open('file_path')创建PIL映像,然后再执行Photoimage.另外,您需要保留对PhotoImage的引用,否则它不会在tkinter中显示.

Try creating PIL image first with Image.open('file_path') and then do Photoimage. Also, you need to keep a reference to the PhotoImage or it wont show in tkinter.

import os
import tkinter as tk
import tkinter.ttk as ttk
from PIL import Image, ImageTk

class App(ttk.Frame):

    def __init__(self, master, path):
        ttk.Frame.__init__(self, master)
        self.tree = ttk.Treeview(self)
        ysb = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview)
        xsb = ttk.Scrollbar(self, orient='horizontal', command=self.tree.xview)
        self.tree.configure(yscroll=ysb.set, xscroll=xsb.set)
        self.tree.heading('#0', text='Directory', anchor='w')

        abspath = os.path.abspath(path)
        i = './icon/Home-icon_16.gif'
        root_pic1 = Image.open(i)                           # Open the image like this first
        self.root_pic2 = ImageTk.PhotoImage(root_pic1)      # Then with PhotoImage. NOTE: self.root_pic2 =     and not     root_pic2 =

        root_node = self.tree.insert('', 'end', text=abspath, open=True, image=self.root_pic2)
        l1_node = self.tree.insert(root_node, 'end', text='level 1', open=True)
        l2_node = self.tree.insert(l1_node, 'end', text='level 2', open=True)
        l3_node = self.tree.insert(l2_node, 'end', text='level 3', open=True)
        l2a_node = self.tree.insert(l1_node, 'end', text='level 2a', open=True)
        l3a_node = self.tree.insert(l2a_node, 'end', text='level 3a', open=True)

        self.tree.grid(row=0, column=0)
        ysb.grid(row=0, column=1, sticky='ns')
        xsb.grid(row=1, column=0, sticky='ew')
        self.grid()

root = tk.Tk()
path_to_my_project = os.getcwd()
app = App(root, path=path_to_my_project)
app.mainloop()

这篇关于tkinter.ttk.Treeview根节点图标/图像未出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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