NaruS
NaruS

Reputation: 168

Unsure of how to set a background image in Tkinter

Before I start I do know there is a post regarding my question found here, however, this didn't exactly help me and I still seem to be running into problems even after following answers on the post.

I seem to get two different "errors"

One error, where it shows that the image file is unreadable. which is given when using this code(This is what is given from the other post as a answer that worked):

self.background_image=tk.PhotoImage…
self.background_label = tk.Label(parent, image=background_image)
self.background_label.place(x=0, y=0, relwidth=1, relheight=1)

And another "Error" Where when using the following code, no image shows up but no real error message is given:

self.background_image = tk.PhotoImage(r'C:/hazuki-gui/resources/background1.png')
self.background_label = tk.Label(image=self.background_image)
self.background_label.place(x=0,y=0)

I have looked around and everything just shows the first way I showed above. I have tried using both png and jpg images but both return the same results in both cases.

Any help regarding this would be appreciated.

Side Note: If there is any difference for how to do this for python 2.7 and python 3.x, please let me know (Currently I am using python 2.7 but will be moving this to 3.x)

Upvotes: 0

Views: 81

Answers (1)

Henry Yik
Henry Yik

Reputation: 22493

Maybe your image is garbage collected since there is no reference to your image?

self.background_image = tk.PhotoImage(file=r'C:/hazuki-gui/resources/background1.png')
self.background_label = tk.Label(image=self.background_image)
self.background_label.place(x=0,y=0)
self.background_label.img = self.background_image #try to add this

Upvotes: 1

Related Questions