如何用图像替换ttk.Treeview父节点箭头?

时间:2016-06-02 08:19:58

标签: image python-3.x tkinter ttk treeviewitem

问题:如何在tkinter.ttk.Treeview中创建一个节点,其中节点的切换箭头被定义的图像替换?也就是说,如何从第二张照片到第一张照片,如下所示。

问题:“新墨西哥州技术指南”显示tkinter.ttk.Treeview可以创建文件夹目录,如下所示:

To Achieve

使用tkinter.ttk.Treeview .insert()方法和"图像"关键字,我只能实现以下目标。图像确实出现在节点文本的左侧,但图像不会替换箭头切换节点的打开和关闭以显示其后代。我假设了图像定义的图像"关键字将替换切换箭头。但这并没有发生。

Achieved so far

测试代码:

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'
        self.root_pic = tk.PhotoImage(file=i)
        root_node = self.tree.insert('', 'end', text='  Work Folder', open=True, image=self.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

0 个答案:

没有答案