_tkinter.TclError: image "pyimage1" doesn't exist

做了一个条形码生成器,能保存条形码,但是不能正常读取条形码,
报错行 lab = tkinter.Label(b10_tk, image=photo1, width=100, height=50)
报错信息 _tkinter.TclError: image "pyimage1" doesn't exist
这是为什么

    b10_tk = tkinter.Tk()
    b10_tk.geometry('1200x900')
    b10_tk.title('条形码生成')
    # os.mkdir('txm')     # 创建一个文件夹,将生成的图片放入文件夹里

    def b():
        zhi = ent.get('1.0', 'end-1c').split('\n')  # 获取text中所有的值
        for i in zhi:
            global run5
            if not run5:
                break   # 可以再添加一个entry框,即可实现多种格式条形码生成
            b10_a = barcode.get('code128', i, writer=ImageWriter())
            b10_a.save(i)     # 保存条形码图片到本地
            b10_image = Image.open(i + '.png')
            photo1 = ImageTk.PhotoImage(b10_image)  # 读取文件夹下图片,写入到label中
            lab = tkinter.Label(b10_tk, image=photo1, width=100, height=50)
            lab.place(x=0, y=150)
            b10_tk.after(int(ent1.get()))
            lab.destroy()

    def c():
        global run5
        run5 = True
        yunxin = Thread(target=b, args=())
        yunxin.start()
        if not run:
            yunxin.join()
    com = tkinter.Button(b10_tk, command=c, text='开始')
    com.place(x=0, y=0)
    ent1 = tkinter.Entry(b10_tk)
    ent1.place(x=50, y=0)
    ent1.insert('1', '3')   # 默认条形码停顿时间3秒
    lab_tingzhi = tkinter.Label(b10_tk, text='条形码停顿时间', fg='#f00')
    lab_tingzhi.place(x=120, y=0)
    ent = tkinter.Text(b10_tk)
    ent.place(x=50, y=20)
    b10_tk.mainloop()
  • 看下这篇博客,也许你就懂了,链接:解决_tkinter.TclError: image “ ..... “ doesn‘t exist
  • 以下回答由chatgpt基于相关博客总结生成:

    我会尝试回答这个问题。

    根据你提供的错误信息,我猜测你的代码使用了Tkinter作为GUI库来生成条形码,并且在生成条形码后尝试将其显示在一个Label上。而报错信息"_tkinter.TclError: image "pyimage1" doesn't exist"表明你的代码在生成一个名为"pyimage1"的图像时失败了,导致后续Label显示出错。

    这个问题可能有以下几个原因:

    1. 生成的图像对象没有被正确地传递给Label的image属性。

    2. Tkinter没有正确地管理图像对象,导致图像对象被提前销毁或无法访问。

    3. 图像生成的代码有误,导致图像无法生成。

    对于这个问题,我建议你可以采取以下步骤来尝试解决。

    1. 确认图像对象已被正确地传递给Label的image属性。

    可以通过打印图像对象的方法来确认图像对象是否被正确传递。

    1. 确认图像对象已经被Tkinter正确地管理。

    你可以在生成图像对象后,使用一个全局变量来持有这个对象的引用,确保图像对象不会被提前销毁。

    1. 确认生成图像的代码是否正确。

    你可以使用一些在线条形码生成器来生成测试用的条形码图片,以此来确认生成图片的代码是否正确。

    最后,我提供一个参考代码,以供参考。

    import tkinter
    from PIL import Image, ImageTk
    
    # 生成条形码图片的方法
    def generate_barcode():
        # 这里使用第三方库生成条形码图片,这里仅作示例
        barcode = Image.new('RGB', (100, 50), color = (255, 255, 255))
        barcode_draw = ImageDraw.Draw(barcode)
        barcode_draw.text((10, 10), "123456789", fill=(0, 0, 0))
        return barcode
    
    # 显示条形码的方法
    def show_barcode():
        barcode_image = generate_barcode()
        photo = ImageTk.PhotoImage(barcode_image)
        label = tkinter.Label(text="barcode", image=photo)
        label.image = photo # 引用计数加一,防止变量提前销毁
        label.pack()
    
    if __name__ == '__main__':
        root = tkinter.Tk()
        show_barcode()
        root.mainloop()
    

    这份代码示例使用了Pillow库来生成条形码图片,同时使用了ImageTk.PhotoImage将Pillow生成的图片对象转换为Tkinter可用的图片对象,并将其附加到Label上。