Python Forum
Having trouble using photo for background - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: Having trouble using photo for background (/thread-32194.html)



Having trouble using photo for background - blakefindlay - Jan-27-2021

Just wondering what I am doing wrong. I have a photo named overlander_sports.jpg in the newb folder which is the name of the project. I can't seem to get it as a background.

from tkinter import *
import datetime
from tkinter import messagebox

root = Tk()
#Date and Time

C = Canvas(root, bg="blue", height=250, width=300)
filename = PhotoImage(file="C:\\Users\\blake\PycharmProjects\\newb\\overlander_sports.jpg")

background_label = Label(root, image=overlander_sports.jpg)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

C.pack()



RE: Having trouble using photo for background - steve_shambles - Jan-27-2021

change the background_label line to reference the "filename" variable
that you set in previous line.

background_label = Label(root, image=filename)

Also you can't read a jpg file with this method
you would need to use the Pillow add-on,
But you can still read in a gif or png with this code.


RE: Having trouble using photo for background - blakefindlay - Jan-27-2021

Great thank you!