Python 当你点击一个缩略图来显示图像时?

Python 当你点击一个缩略图来显示图像时?,python,python-3.x,tkinter,python-imaging-library,Python,Python 3.x,Tkinter,Python Imaging Library,请帮助修复脚本 import os, sys import tkinter from PIL import ImageTk, Image DIR_IMGS = 'imgs' DIR_THUMBS = 'thumbs' imgfiles = os.listdir(DIR_IMGS) thumbfiles = os.listdir(DIR_THUMBS) root = tkinter.Tk() root.geometry('900x700') links = [] def showItem(

请帮助修复脚本

import os, sys
import tkinter
from PIL import ImageTk, Image

DIR_IMGS = 'imgs'
DIR_THUMBS = 'thumbs'
imgfiles = os.listdir(DIR_IMGS)
thumbfiles = os.listdir(DIR_THUMBS)

root = tkinter.Tk()
root.geometry('900x700')
links = []


def showItem(imgfile):
    print(imgfile)
    pathImg = os.path.join(DIR_IMGS, imgfile)
    print(pathImg)
    renderImg = ImageTk.PhotoImage(file=pathImg)
    popup = tkinter.Toplevel()
    tkinter.Button(popup, image=renderImg).pack()   


def createThumbs():
    for imgfile in imgfiles:
        pathImg1 = os.path.join(DIR_IMGS, imgfile)
        pathImg2 = os.path.join(DIR_THUMBS, imgfile)

        openImg = Image.open(pathImg1)
        openImg.thumbnail((100, 100))
        openImg.save('thumbs/' + imgfile)


def outputButtons():
    for thumbfile in thumbfiles:
        pathImg = os.path.join(DIR_THUMBS, thumbfile)
        renderImg = ImageTk.PhotoImage(file=pathImg)
        but = tkinter.Button(root, image=renderImg)
        but.pack(side='left')
        but.bind('<Button-1>', lambda event, thumbfile=thumbfile: showItem(thumbfile))
        links.append(renderImg)


createThumbs()
outputButtons()

root.mainloop()
导入操作系统,系统 进口tkinter 从PIL导入ImageTk,图像 DIR_IMGS='IMGS' DIR_THUMBS='THUMBS' imgfiles=os.listdir(DIR\u IMGS) thumbfiles=os.listdir(DIR\u THUMBS) root=tkinter.Tk() 根几何体('900x700') 链接=[] def显示项(imgfile): 打印(imgfile) pathImg=os.path.join(DIR\u IMGS,imgfile) 打印(路径) renderImg=ImageTk.PhotoImage(文件=pathImg) popup=tkinter.Toplevel() tkinter.Button(弹出窗口,image=renderImg.pack()) def createThumbs(): 对于imgfiles中的imgfile: pathImg1=os.path.join(DIR\u IMGS,imgfile) pathImg2=os.path.join(DIR\u THUMBS,imgfile) openImg=Image.open(路径img1) openImg.缩略图((100100)) openImg.save('thumbs/'+imgfile) def输出按钮(): 对于thumbfiles中的thumbfile: pathImg=os.path.join(DIR\u THUMBS,thumbfile) renderImg=ImageTk.PhotoImage(文件=pathImg) but=tkinter.Button(根,image=renderImg) 但是.pack(side='left') 但是.bind(“”,lambda事件,thumbfile=thumbfile:showItem(thumbfile)) links.append(渲染) createThumbs() 输出按钮() root.mainloop() 我写了这个脚本,只是流行书籍“MarkLutz.ProgrammingPython”的一个例子。但出于某种奇怪的原因,我的脚本不起作用

没有清单错误,因为屏幕不是错误消息。但是在弹出窗口中不显示(显示空白弹出窗口)

在窗口有机会显示之前,会对(稍大的)图像进行垃圾收集。 您需要在图像周围保留一个引用来显示它。我从中获取了解决方案,您的
showItem
函数可以如下所示:

def showItem(imgfile):
    print(imgfile)
    pathImg = os.path.join(DIR_IMGS, imgfile)
    print(pathImg)
    renderImg = ImageTk.PhotoImage(file=pathImg)
    popup = tkinter.Toplevel()
    button = tkinter.Button(popup, image=renderImg)
    button.image = renderImg
    button.pack()   
在窗口有机会显示(稍大的)图像之前,该图像已被垃圾收集。 您需要在图像周围保留一个引用来显示它。我从中获取了解决方案,您的
showItem
函数可以如下所示:

def showItem(imgfile):
    print(imgfile)
    pathImg = os.path.join(DIR_IMGS, imgfile)
    print(pathImg)
    renderImg = ImageTk.PhotoImage(file=pathImg)
    popup = tkinter.Toplevel()
    button = tkinter.Button(popup, image=renderImg)
    button.image = renderImg
    button.pack()