Tkinter Treeview 插入图像
在insert( )方法内若是增加image参数可以添加图像,在添加图像时需要考虑的是可能row的高度不足,所以必须增加高度。这时可以用下列Style( )方法处理。
Style().configure("TreeView", rowHeight=xx) # xx 是高度设置
示例1
设计一个含有图像的Treeview。
from tkinter import *
from tkinter.ttk import *
from PIL import Image, ImageTk
root = Tk()
root.title("apidemos.com")
Style().configure("Treeview",rowheight=35) # 格式化扩充row高度
info = ["The Phoenix News App provides access to the latest news from around China",
"Swiss National Rail App provides all Swiss train schedules",
"Coca-Cola App is a software for entertainment"]
tree = Treeview(root,columns=("Description"))
tree.heading("#0",text="App") # 图标栏
tree.heading("#1",text="Function Description")
tree.column("#1",width=300) # 格式化栏标题
img1 = Image.open("apidemos.png") # Insert apidemos.png
imgObj1 = ImageTk.PhotoImage(img1)
tree.insert("",index=END,text="Phoenix News",image=imgObj1,values=info[0])
img2 = Image.open("apidemos.png") # Insert apidemos.png
imgObj2 = ImageTk.PhotoImage(img2)
tree.insert("",index=END,text="Swiss Railways",image=imgObj2,values=info[1])
img3 = Image.open("apidemos.png") # Insert apidemos.png
imgObj3 = ImageTk.PhotoImage(img3)
tree.insert("",index=END,text="Coca-Cola",image=imgObj3,values=info[2])
tree.pack()
root.mainloop()
输出: