TypeError: __str__ returned non-string (type instance)

tkinter

Solution

Looks like I answered my own question. First of all, I wrote the syntactically incorrect statements:

background = "background.png"
photo = tk.PhotoImage(Image.open(background))

This should be written correctly:

background = "background.png"
photo = tk.PhotoImage(background)

Second, Tkinter does not support .png files. The correct class is ImageTk from module PIL.

from PIL import ImageTk as itk
background = "background.png"
photo = itk.PhotoImage(file = background)

And notice the difference in syntax:

photo = tk.PhotoImage(background)
photo = itk.PhotoImage(file = background)

Problem

I keep getting this really strange error that I cannot solve looking at any other post. I'm applying a background image to a tkinter canvas. ``` import Tkinter as tk ## Python 2.X import Image root = tk.Tk(); background = "background.png" photo = tk.PhotoImage(Image.open(background)) canvas = tk.Canvas(root, width=500, height=500) canvas.pack() canvas.create_image(0, 0, anchor="nw", image=photo) root.mainloop() ``` But because of the last line, I get this error: ``` Traceback (most recent call last): File "main.py", line 40, in <module> canvas.create_image(0, 0, anchor="nw", image=photo) File "c:\Python27\lib\lib-tk\Tkinter.py", line 2279, in create_image return self._create('image', args, kw) File "c:\Python27\lib\lib-tk\Tkinter.py", line 2270, in _create *(args + self._options(cnf, kw)))) TypeError: __str__ returned non-string (type instance) ```

Original source