0

I am creating a small program that should display an image overlayed the main window based on a button click event. However, whenever I test the code that should be doing this, I get this error: AttributeError: 'int' object has no attribute '_create'

Here is the appropriate code:

def display_image(event):
    fail_image = tk.PhotoImage(file="./RedXMark.png")
    Canvas.create_image(0, 0, image=fail_image, anchor="nw")

# Later in the code:

root.bind('<t>', display_image)

I have been trying to use Tkinter's labels to display the image and, while I wouldn't get an error, it wouldn't display the image. The image is in the same folder as the program and is saved with that exact name.

Any advice would be welcome!

  • Another problem you have: https://stackoverflow.com/questions/16424091/why-does-tkinter-image-not-show-up-if-created-in-a-function – jasonharper Mar 06 '22 at 23:40
  • See possible solution here: https://stackoverflow.com/questions/71245789/how-to-define-an-image-object – Robin Mar 07 '22 at 02:10

1 Answers1

0

You must call create_image on an instance of the Canvas class, but you're calling it on the class itself.

something = tk.Canvas(...)
...
something.create_image(0, 0, image=fail_image, anchor="nw")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685