kanna karthik reddy
kanna karthik reddy

Reputation: 1

Adding background in Tkinter

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?

Upvotes: 0

Views: 41

Answers (1)

Nic Thibodeaux
Nic Thibodeaux

Reputation: 165

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.

Upvotes: 1

Related Questions