How do I fix the “image ”pyimage10“ doesn't exist” error, and why does it happen?

后端 未结 3 1349
遇见更好的自我
遇见更好的自我 2021-01-13 22:59

I am making a tkiner application and that shows a user a page with some basic information and a picture before allowing them to click a button to view live Bitcoin price dat

相关标签:
3条回答
  • 2021-01-13 23:27

    You can not load a .png format with tkinter. You rather need to use the PIL library for that:

    import PIL
    
    image = PIL.Image.open("bitcoin.png")
    BTC_img = PIL.ImageTk.PhotoImage(image)
    BTC_img_label = tk.Label(self, image=BTC_img)
    BTC_img_label.image = BTC_img
    BTC_img_label.grid(row=2, column=0)
    

    EDIT:

    Please, create a test.py file and run this EXACT code:

    import tkinter as tk
    from PIL import Image, ImageTk
    
    root = tk.Tk()    
    image = Image.open("bitcoin.png")
    photo = ImageTk.PhotoImage(image)
    label = tk.Label(root, image=photo)
    label.image = photo
    label.grid(row=2, column=0)
    #Start the program
    root.mainloop()
    

    Tell me if you get an error or not.

    0 讨论(0)
  • 2021-01-13 23:27

    Like @joost-broekhuizen, I've had the same problem using Tkinter together with matplotlib.pyplot functions. Adding a 'master' to the PhotoImage function solved the problem for me.

    Broken code (raises: TclError: image "pyimage10" doesn't exist):

    import matplotlib
    matplotlib.use("TkAgg")
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
    import matplotlib.pyplot as plt
    import Tkinter as tk
    from PIL import Image, ImageTk
    
    fig = plt.figure()
    
    root = tk.Tk()
    image = Image.open("background.png")
    photo = ImageTk.PhotoImage(image)
    label = tk.Label(root, image=photo)
    label.image = image
    label.pack()
    
    root.mainloop()
    

    Adding 'master=root' to PhotoImage solved this error!

    photo = ImageTk.PhotoImage(image, master=root)
    
    0 讨论(0)
  • 2021-01-13 23:46

    I had the same problem. The problem was importing matplotlib.pyplot in the same program or in another py file from which you import definitions. Use Canvas for your plots instead

    0 讨论(0)
提交回复
热议问题