I want to put my own image in the background of the canvas. I made this code:
size = (800,600)
im = Image.new('RGB',size)
pix = im.load()
for i in range(size[0]):
for j in range(size[1]):
pix[i,j] = (100,100,100)
im.save('ima5.png')
im2 = ImageTk.PhotoImage(im)
canvas = Tkinter.Canvas(width=Width, height=Height, bg=im2)
I've this error: _tkinter.TclError: unknown color name "pyimage1"
, I know it's why the image haven't the format expected by the canvas. How to put im2 as background of the canvas?
The bg/background attribute requires a color, not an image. To use an image as the background, use the create_image
method of the canvas:
import Image, ImageTk, Tkinter
im = ImageTk.PhotoImage(Image.open("<path\\to\\image.jpg-ect>").resize((800,600)))
canvas = Tkinter.Canvas(Parent, width=WIDTH, height=HEIGHT)
canvas.pack() #place(), etc.
Canvas_Image = canvas.create_image(0,0 image=im, anchor="nw")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.