I'm trying to add a background image to a canvas in Python. So far the code looks like this:
from Tkinter import *
from PIL import ImageTk,Image
... other stuffs
root=Tk()
canvasWidth=600
canvasHeight=400
self.canvas=Canvas(root,width=canvasWidth,height=canvasHeight)
backgroundImage=root.PhotoImage("D:\Documents\Background.png")
backgroundLabel=root.Label(parent,image=backgroundImage)
backgroundLabel.place(x=0,y=0,relWidth=1,relHeight=1)
self.canvas.pack()
root.mainloop()
It's returning an AttributeError: PhotoImage
just change to :
believe me this will 100% work
PhotoImage
is not an attribute of theTk()
instances (root
). It is a class fromTkinter
.So, you must use:
Beware also
Label
is a class fromTkinter
...Edit:
Unfortunately,
Tkinter.PhotoImage
only works with gif files (and PPM). If you need to read png files you can use thePhotoImage
(yes, same name) class in theImageTk
module fromPIL
.So that, this will put your png image in the canvas: