我正在尝试使用 icrawler 中的图像来填充 tkinter 上的按钮

I'm trying to use images from icrawler to populate buttons on tkinter

总的来说,我是编码方面的新手。我正在尝试创建一个程序来下载前 20 google 个图像并使用 tkinter 将它们放在按钮上。我设法创建了网格和按钮。如果我将 'create_grid' 代码放在函数之外,则会出现最后一张图像,但不会出现其余图像。我认为它与垃圾收集有关,但我整个星期都在为此苦苦思索。任何有关它如何工作的想法都将不胜感激。

    from tkinter import *
    from PIL import ImageTk,Image
    from icrawler.builtin import GoogleImageCrawler
    import time
    import os
    
    fileLocation = "C:\Python projects\tkinter\images\"
    fileList = os.listdir(fileLocation)
    root = Tk()
    root.title("Product image search")
    root.iconbitmap("favicon.ico")
    e = Entry(root, borderwidth=5)
    e.grid(row=0, column=0, columnspan=5)
    
    
    length  = len(fileList)
    i = 0
    rows = 0
    cols = 1    
    button_list = []
    
    
    def create_grid():
        global i, rows, cols 
        while i < length:
        
            # Define image boxes
    
            img_path = (f"{fileLocation}{fileList[i]}")
            img_raw= Image.open(img_path)   
            print(img_path)
            img_resize = img_raw.resize((200, 200), Image.ANTIALIAS)    
            img_1 = ImageTk.PhotoImage(img_resize)  
            img_btn = Button(image=img_1)   
            img_btn.grid(row= rows, column= cols)
            button_list.append(img_btn)
            
            rows += 1
            
            if rows == 5:       
                rows = 0
                cols += 1
            i += 1
    
    
    def crawlerAction():
        google_crawler = GoogleImageCrawler(
            parser_threads=2,
            downloader_threads=4,
            storage={'root_dir': 'images'})
    
    
        google_crawler.crawl(keyword=(e.get()), max_num=20)
        time.sleep(5)
        create_grid()
    
    search_btn = Button(text="検査", command=crawlerAction)
    search_btn.grid(row=0, column=3)
    
    root.mainloop()

您需要在函数内部调用os.listdir。如果你之前调用它,你的图片还没有下载,所以文件夹是空的。在函数内移动 fileLocationfileListlength 应该可以修复它。

root = Tk()
root.title("Product image search")
root.iconbitmap("favicon.ico")
e = Entry(root, borderwidth=5)
e.grid(row=0, column=0, columnspan=5)



i = 0
rows = 0
cols = 1    
button_list = []


def create_grid():
    global i, rows, cols
    fileLocation = "C:\Python projects\tkinter\images\"
    fileList = os.listdir(fileLocation)
    length  = len(fileList)
    while i < length:
    
        # Define image boxes

        img_path = (f"{fileLocation}{fileList[i]}")
        img_raw= Image.open(img_path)   
        print(img_path)
        img_resize = img_raw.resize((200, 200), Image.ANTIALIAS)    
        img_1 = ImageTk.PhotoImage(img_resize)  
        img_btn = Button(image=img_1)   
        img_btn.grid(row= rows, column= cols)
        button_list.append(img_btn)
        
        rows += 1
        
        if rows == 5:       
            rows = 0
            cols += 1
        i += 1

如果您的图像仍然没有显示,它们可能正在被垃圾收集。在这种情况下,在 img_btn = Button(... 之后的行中添加 img_btn.image = img_1。通过创建对 PhotoImage 对象的引用,它不会被垃圾回收。