tkinter.ttk.Treeview根节点图标/图像不会出现

时间:2016-05-31 15:35:24

标签: image python-3.x treeview python-imaging-library ttk

问题:我无法在tkinter.ttk.Treeview中的根节点旁边显示图标图像。下面是我使用的测试代码。它执行没有错误但图像没有出现在根节点的左侧。我已经尝试使用图像文件的完整路径名,但这不起作用。此外,我已尝试使用PIL.ImageTk.PhotoImage打开图像文件,但也无法正常工作。而是出现如下所示的错误。

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

测试代码:

behalf = false;
private validateName(){
    if (this.behalf && this.nameB.text != '') {
        return {
            invalidName : true
        };
    }
    else{
        return null;
    }
}    
constructor (private builder: FormBuilder){
    this.title = new Control('', Validators.required);
    this.name = new Control('', this.validateName);
    this.type = new Control('', Validators.required);
    this.desc = new Control('');
    this.hideTitle = new Control('');
    this.end = new Control('', Validators.required);
    this.formBook = builder.group({
        title: this.title,
        name: this.name,
        type: this.type,
        desc: this.desc,
        hideTitle: this.hideTitle,
        end: this.end
    });
}

使用PIL.ImageTk.PhotoImage错误消息:

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

主-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

1 个答案:

答案 0 :(得分:3)

首先尝试使用Image.open('file_path')创建PIL图像,然后执行Photoimage。此外,您需要保留对PhotoImage的引用,否则它将不会显示在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()