Python Tkinter Treeview将图像添加为列值 [英] Python Tkinter Treeview add an image as a column value

查看:278
本文介绍了Python Tkinter Treeview将图像添加为列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将图像添加到树视图上每一行的第一列,但是无论我做什么,总是以显示的对象"pyimage1"的名称而不是实际图像结尾. 此图像显示

i'm trying to add an Image to the first column of every row on a treeview, but no matter what I do, always end up with the name of the object "pyimage1" showed instead of the actual image. As this image shows

我正在使用的代码是这样的.

The code that i'm using is something like this.

    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,但结果相同,我知道我的图像对象已正确创建,因为在调试时我可以看到图像的属性,但无法使其显示在树视图上行.

I've tried with png, with the same result, I know that my image object has been correctly created, because when debugging I can see the properties of the image, but I can't make it to show on the treeview row.

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

我正在尝试使用上面的代码,它与您的代码非常相似,但是我找不到我的错误,但是我看到"text"或"image"字段仅出现在该行的列表中我作为价值"传递的价值,有什么想法吗?

I'm trying with the above code, it's pretty similar to yours, but i can't find my error, however I've watch that "text" nor "image" field appear on the row, just the list of values that i passed as "value", any thoughts?

推荐答案

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

You can display your image using the image argument in the w.insert method. See below.

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列和第一行(标题下方).

Here is an example script showing the basic setup of a ttk.Treeview widget and how you can include an image to the #0 column and 1st row (below heading) of the widget.

#!/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()

回复您的修改: 请参阅脚本中的注释以获取解释.还建议您尝试一下我提供给您的早期脚本,以帮助您更好地了解如何使用Treeview小部件.玩得开心.

Respond to your See comments in the script for explanation. Also suggest you experiment the earlier script I had provided you to help you better understand how to use Treeview widget. Have fun.

#!/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将图像添加为列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆