0

I'm trying to add a background to a tkinter window, and it is not working. Here is my code:

from tkinter import *

from tkinter import messagebox

top = Tk()

C = Canvas(top, bg="blue", height=250, width=300)

filename = PhotoImage(file = "C:\\Users\\Karthik\\OneDrive\\Pictures\\bank.png")

background_label = Label(top, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

C.pack()
top.mainloop()

I tried this, but I'm getting _tkinter.TclError: couldn't recognize data in image file "C:\Users\Karthik\OneDrive\Pictures\bank.png"

Any suggestions?

1
  • Welcome to SO! Please see my edit to better understand how to format questions. Commented Jun 27, 2020 at 16:27

1 Answer 1

1

PhotoImage doesn't read .png files, but PIL.Image() does. Try using this:

from PIL import Image, ImageTk

image = Image.open("C:\\Users\\Karthik\\OneDrive\\Pictures\\bank.png")
photo = ImageTk.PhotoImage(image)

Check this page for more info.

Sign up to request clarification or add additional context in comments.

2 Comments

But i am still getting the same result---ERROR
Have you read the link I provided? Can you provide the full error you're getting?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.