gpt4 book ai didi

Python Tkinter Treeview 添加图像作为列值

转载 作者:行者123 更新时间:2023-12-05 09:16:56 25 4
gpt4 key购买 nike

我正在尝试将图像添加到 TreeView 上每一行的第一列,但无论我做什么,总是以显示的对象名称“pyimage1”而不是实际图像结束。 As this image shows

我使用的代码是这样的。

    from tkinter import PhotoImage.
self._img = PhotoImage(file="resources\information_picto.gif")
self.tree.insert('', 'end', values= self._image,self.name, self.status, self.cores, self.turn, self.added_time)

我尝试过使用 png,结果相同,我知道我的图像对象已正确创建,因为在调试时我可以看到图像的属性,但我无法让它显示在 TreeView 上行。

编辑:

 def __init__(self, master, **kw):
self.SortDir = True
f = ttk.Frame(master)
f.pack(fill=BOTH, expand=True)
self.dataCols = ('Project Name', 'Status', 'Cores', 'Turn', 'Added date/time')
self.tree = ttk.Treeview(columns=self.dataCols,
show='headings')
self.tree.column("Project Name", anchor="center")

self.tree.grid(in_=f, row=0, column=0, sticky=NSEW)
f.rowconfigure(0, weight=1)
f.columnconfigure(0, weight=1)
style = ttk.Style(master)
style.configure('Treeview', rowheight=38)

self._img = PhotoImage(file="resources\information_picto.gif")
self.tree.insert('', 'end', text="#0's text", image=self._img,
value=("A's value", "B's value"))

我正在尝试上面的代码,它与你的非常相似,但我找不到我的错误,但是我看到“文本”或“图像”字段出现在行中,只是列表我作为“值(value)”传递的值(value)观,有什么想法吗?

最佳答案

您可以使用 w.insert 方法中的 image 参数显示图像。见下文。

from tkinter import PhotoImage.
self._img = PhotoImage(file="resources\information_picto.gif")
self.tree.insert('', 'end', text='Information_picto.gif', open=True, image=self._img,
value=(self.name, self.status, self.cores, self.turn, self.added_time))

编辑:

这是一个示例脚本,显示了 ttk.Treeview 小部件的基本设置以及如何将图像包含到小部件的 #0 列和第一行(标题下方)。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import tkinter as tk
import tkinter.ttk as ttk

class App(ttk.Frame):

def __init__(self, parent=None, *args, **kwargs):
ttk.Frame.__init__(self, parent)
self.parent = parent

# Create Treeview
self.tree = ttk.Treeview(self, column=('A','B'), selectmode='none', height=7)
self.tree.grid(row=0, column=0, sticky='nsew')

# Setup column heading
self.tree.heading('#0', text=' Pic directory', anchor='center')
self.tree.heading('#1', text=' A', anchor='center')
self.tree.heading('#2', text=' B', anchor='center')
# #0, #01, #02 denotes the 0, 1st, 2nd columns

# Setup column
self.tree.column('A', anchor='center', width=100)
self.tree.column('B', anchor='center', width=100)

# Insert image to #0
self._img = tk.PhotoImage(file="imagename.gif") #change to your file path
self.tree.insert('', 'end', text="#0's text", image=self._img,
value=("A's value", "B's value"))


if __name__ == '__main__':
root = tk.Tk()
root.geometry('450x180+300+300')

app = App(root)
app.grid(row=0, column=0, sticky='nsew')

root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)

root.mainloop()

Picture added.

回应您的编辑:请参阅脚本中的注释以获取解释。还建议您试验我提供给您的早期脚本,以帮助您更好地理解如何使用 Treeview 小部件。玩得开心。

#!/usr/bin/python3
# -*- coding: utf-8 -*-


# tkinter modules
import tkinter as tk
import tkinter.ttk as ttk

class App(ttk.Frame):

def __init__(self, master, **kw):
self.SortDir = True
#f = ttk.Frame(master) #1. this widget is self, no need to assign to f. 2. You missed out .__init__().
ttk.Frame.__init__(self, master)
#f.pack(fill=tk.BOTH, expand=True)# redundant. done by app.grid


#self.dataCols = ('Project Name', 'Status', 'Cores', 'Turn', 'Added date/time')
#I have removed 'Project Name' since it is #0. self.dataCols is for #01, #02, .. onwards
self.dataCols = ('Status', 'Cores', 'Turn', 'Added date/time')
#self.tree = ttk.Treeview(self, columns=self.dataCols, show='headings')
# Did not define widget's parent? I have added. Picture not shown because u used option show='headings'
self.tree = ttk.Treeview(self, columns=self.dataCols)
#self.tree.column("Project Name", anchor="center")
#self.tree.grid(in_=f, row=0, column=0, sticky=tk.NSEW)
# I have removed "in_=f" since parent has been defined.
self.tree.grid(row=0, column=0, sticky=tk.NSEW)

# Setup column heading
self.tree.heading('#0', text='Project Name', anchor='center')
self.tree.heading('#1', text='Status', anchor='center')
self.tree.heading('#2', text='Cores', anchor='center')
self.tree.heading('#3', text='Turn', anchor='center')
self.tree.heading('#4', text='Added date/time', anchor='center')


#f.rowconfigure(0, weight=1) # Use with .grid but not for .pack positioning method
#f.columnconfigure(0, weight=1) # same as above
style = ttk.Style(master)
style.configure('Treeview', rowheight=38)

self._img = tk.PhotoImage(file="test50.gif")
self.tree.insert('', 'end', text="#0's text", image=self._img,
value=("A's value", "B's value"))

if __name__ == '__main__':
root = tk.Tk()
root.geometry('450x180+300+300')

app = App(root)
app.grid(row=0, column=0, sticky='nsew')

root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)

root.mainloop()

关于Python Tkinter Treeview 添加图像作为列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49307497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com