You are using root
to Attribute PhotoImage
this is impossible!
root
is your window Tk()
class so you cant Attribute it for PhotoImage
because it's dosen't have it so you see AttributeError
tkinter.Tk()
and tkinter.PhotoImage
is a different classes. and the same with tkinter.Label
.
your code will not working with root.PhotoImage
and root.Label
.
try to PhotoImage
and Label
directly.
to create a Label
:
backgroundlabel = Label(parent, image=img)
if use any types of png
or jpg
and jpeg
you can't draw it with just PhotoImage
you will need PIL library
pip3 install PIL
when you have it use it like:
from PIL import Image, ImageTk # import image so you can append the path and imagetk so you can convert it as PhotoImage
now get your full path image like:
C:/.../img.png
Now use it :
path = "C:/.../img.png" # Get the image full path
load = Image.open(path) # load that path
img = ImageTk.PhotoImage(load) # convert the load to PhotoImage
now you have your code work.
full code:
from Tkinter import *
from PIL import ImageTk,Image
... other stuffs
root = Tk()
canvasWidth = 600
canvasHeight = 400
self.canvas = Canvas(root,width=canvasWidth,height=canvasHeight)
path = "D:\Documents\Background.png" # Get the image full path
load = Image.open(path) # load that path
img = ImageTk.PhotoImage(load)
backgroundLabel = Label(parent,image=img)
backgroundLabel .place(x=0,y=0,relWidth=1,relHeight=1)
self.canvas .pack()
root .mainloop()
Hope this will helpfull.