1

I've had this problem before and I solved it here, the thing is that for another program I used the same method canvas.create_image() and I couldn't see my image. My idea is, that the garbage collector destroy it before I see it on my canvas, but I don't know how to stop this. I saw this, but I didn't know how to apply to my program.

But if I write canvas.create_rectangle(50, 50, 70, 70), I works very well, so I don't understand. I also tried to add my image to a Label instead of putting it in my canvas, and I had the same problem of seeing anything. But I've never got an execution error.

this is my code :

from tkinter import *

root = Tk()
canvas = Canvas(root, bg="yellow")

dino = canvas.create_image(600, 200, image=PhotoImage(file='running_1.png'))

canvas.pack(fill="both", expand=True)

root.mainloop()
Sticonike
  • 47
  • 6
  • You need to assign the output of `PhotoImage(...)` to a variable, then use the variable in `image=...`. – acw1668 Jun 28 '21 at 12:05

1 Answers1

1

Try this -

from tkinter import *

root = Tk()
canvas = Canvas(root, bg="yellow")
running =PhotoImage(file='running_1.png')

dino = canvas.create_image(600, 200, image=running)

canvas.pack(fill="both", expand=True)

root.mainloop()

Your code doesn't work because you are referencing the PhotoImage inside the canvas.create_image. You should assign a variable for it and then use image=variable

PCM
  • 2,881
  • 2
  • 8
  • 30