首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >树视图中的第二个图像不刷新tkinter

树视图中的第二个图像不刷新tkinter
EN

Stack Overflow用户
提问于 2019-07-02 04:26:08
回答 2查看 78关注 0票数 0

当从def向treeview添加一个图像时,我遇到了问题

案例1-它是好的

代码语言:javascript
复制
import tkinter
import PIL.Image, PIL.ImageTk
from tkinter import PhotoImage
from tkinter import ttk    
window = tkinter.Tk()
tree = ttk.Treeview(window)
tree["columns"]="one"
tree.heading("#0",text="Item",anchor=tkinter.W)
tree.heading("one", text="Detections",anchor=tkinter.W)
style = ttk.Style(window)
style.configure('Treeview', rowheight=50)
tree.grid(row=0,column=0,sticky=tkinter.N)
img = PIL.Image.open("1.jpg")
img = img.resize((10, 10))                
img = PIL.ImageTk.PhotoImage(img)
tree.insert('', 'end', text="predict", image=img, value=("title"))

案例二-不工作

代码语言:javascript
复制
window = tkinter.Tk()
tree = ttk.Treeview(window)
tree["columns"]="one"
tree.heading("#0",text="Item",anchor=tkinter.W)
tree.heading("one", text="Detections",anchor=tkinter.W)
style = ttk.Style(window)
style.configure('Treeview', rowheight=50)
tree.grid(row=0,column=0,sticky=tkinter.N)
#img = PIL.Image.open("2.jpg")
#img = img.resize((10, 10))                
#img = PIL.ImageTk.PhotoImage(img)
#tree.insert('', 'end', text="predict", image=img, value=("title"))

def snapshot():
    img = PIL.Image.open("2.jpg")
    img = img.resize((10, 10))
    img = PIL.ImageTk.PhotoImage(img)
    tree.insert('', 'end', text="predict2", image=img, value=("title2"))

btn_snapshot=tkinter.Button(window, text="Snapshot", width=50, command=snapshot)    
btn_snapshot.grid(row=1,column=0)

那么,问题是当从def.添加图像时.我可以添加项,但是图像不可见

知道吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-07-03 11:14:07

谢谢,伙计,把解决方案复制给其他人。@henry,你是对的,重要的是把所有的图像保存在函数之外的列表中

代码语言:javascript
复制
list_img = []
def snapshot():
    img = PIL.Image.open("frame-01-07-2019-22-02-38.jpg")
    img = img.resize((10, 10))
    img = PIL.ImageTk.PhotoImage(img)
    list_img.append(img)    
    tree.insert('', 'end', text="predict2", image=list_img[len(list_img)-1], value=("title2"))
票数 0
EN

Stack Overflow用户

发布于 2019-07-02 04:36:42

很常见的问题。你必须保存一个图像参考。

代码语言:javascript
复制
def snapshot():
    img = PIL.Image.open("2.jpg")
    img = img.resize((10, 10))
    img = PIL.ImageTk.PhotoImage(img)
    tree.img2dotjpg = img # store the reference
    tree.insert('', 'end', text="predict2", image=img, value=("title2"))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56845314

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档