Home > Enterprise >  Displaying Images Using Python Events
Displaying Images Using Python Events

Time:03-08

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!

CodePudding user response:

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")
  • Related