user7043649 user7043649 - 1 year ago 339
Python Question

AttributeError when creating tkinter.PhotoImage object with PIL.ImageTk

I am trying to place an image resized with PIL in a tkinter.PhotoImage object.

import tkinter as tk # I use Python3
from PIL import Image, ImageTk

master = tk.Tk()

However, when I later try to call

photoimg.put( "#000000", (0,0) )

I get an

AttributError: 'PhotoImage' object has no attribute 'put'

While this:

photoimg.put( "#000000", (0,0))

doesn't raise an error.
What am I doing wrong?

Answer Source

ImageTk.PhotoImage as in PIL.ImageTk.PhotoImage is not the same class as tk.PhotoImage (tkinter.PhotoImage) they just have the same name

here is ImageTk.PhotoImage docs: as you can see there is no put method in it.

but ImageTk.PhotoImage do have it:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download