如何在tkinter Treeview小部件中的不同列中插入图像?

时间:2018-12-22 19:35:25

标签: python image tkinter treeview

我只想要该图像位于指定的列(位于“国家/地区”列中)而不是第一列中。谢谢!

from tkinter import ttk
from tkinter import *

root = Tk()

s = ttk.Treeview(columns=('#1', '#2'))

s.heading('#0', text='Ip')
s.heading('#1', text='Port')
s.heading('#2', text='Country')

s.pack()

v = PhotoImage(file='uk.png')

s.insert('', 2, values=('127.0.0.1', '8888'), image=v)

root.mainloop()

enter image description here

1 个答案:

答案 0 :(得分:0)

我已经很简单了:)只是从国家开始

from tkinter import ttk
from tkinter import *

root = Tk()
s = ttk.Treeview(columns=('#1', '#2'))
s.heading('#0', text='Country')
s.heading('#1', text='Ip')
s.heading('#2', text='Port')
v = PhotoImage(file='uk.png')
s.insert('', 2, values = ('127.0.0.1', '8888'), image=v)
s.pack()
root.mainloop()

enter image description here

如果您一开始不喜欢图片,我可以想象没有树状视图,

from tkinter import ttk
from tkinter import *

root = Tk()

v = PhotoImage(file='uk.png')

# header
b = Label(root, text="Ip")
b.grid(row=0, column=0)
b = Label(root, text="port")
b.grid(row=0, column=1)
b = Label(root, text="Country")
b.grid(row=0, column=2)

lista = [["800","127.1.1.",v]]

height = 2
width = 3
for i in range(height-1): #Rows
    b1 = Label(root, text=lista[i][0])
    b1.grid(row=i+1, column=0)
    b2 = Label(root, text=lista[i][1])
    b2.grid(row=i+1, column=1)
    b3 = Label(root, image=lista[i][2])
    b3.grid(row=i+1, column=2)

mainloop()

enter image description here