8

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

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
user1689935
  • 319
  • 2
  • 4
  • 6

4 Answers4

10

PhotoImage is not an attribute of the Tk() instances (root). It is a class from Tkinter.

So, you must use:

backgroundImage = PhotoImage("D:\Documents\Background.gif")

Beware also Label is a class from Tkinter...

Edit:

Unfortunately, Tkinter.PhotoImage only works with gif files (and PPM). If you need to read png files you can use the PhotoImage (yes, same name) class in the ImageTk module from PIL.

So that, this will put your png image in the canvas:

from Tkinter import *
from PIL import ImageTk

canvas = Canvas(width = 200, height = 200, bg = 'blue')
canvas.pack(expand = YES, fill = BOTH)

image = ImageTk.PhotoImage(file = "C:/Python27/programas/zimages/gato.png")
canvas.create_image(10, 10, image = image, anchor = NW)

mainloop()

enter image description here

joaquin
  • 82,968
  • 29
  • 138
  • 152
0

just change to :

    image = Image.open("~~~path.png")
    backgroundImage=ImageTk.PhotoImage(image)

believe me this will 100% work

infiNity9819
  • 536
  • 2
  • 7
  • 19
0
from Tkinter import *
from PIL import ImageTk

canvas = Canvas(width = 200, height = 200, bg = 'blue')
canvas.pack(expand = YES, fill = BOTH)

image = ImageTk.PhotoImage(file = "C:/Python27/programas/zimages/gato.png")
canvas.create_image(10, 10, image = image, anchor = NW)

mainloop()
maxbachmann
  • 2,862
  • 1
  • 11
  • 35
skee
  • 15
  • 1
  • 6
  • Welcome to StackOverflow. While this code may solve the question, [including an explanation](https://meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit](https://stackoverflow.com/posts/65252059/edit) your answer to add explanations and give an indication of what limitations and assumptions apply. – Ruli Dec 11 '20 at 15:34
0

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.

Shihab
  • 94
  • 10