-1

I'm trying to load an image into tkinter, to use as a background image. The image is a GIF (originally JPG, but I heard that tkinter doesn't support that format) with the same dimensions as the window. Anyway, when I ran my code, it ran, but the tkinter window was empty! Here's my code for the window:

class Window(Tk):
    def __init__(self):
        super().__init__()
        self.geometry("700x600")
        self.resizable(False, False)
    
    def background_img(self, img_path):
        background_img = PhotoImage(file=img_path)
        background_img_label = Label(self, image=background_img)
        background_img_label.place(x=0, y=0)

window = Window()
img_path = "background.gif"
window.background_img(img_path)
window.mainloop()

Can you please tell me what I'm doing wrong? Thanks in advance.

  • You are using the name `background_img` both for the function name and the image itself, so the definitions override each other. Change one of them. – TDG Feb 26 '22 at 15:33
  • Thanks @TDG but I tried that and it's still giving me a blank screen. – manologgon Feb 26 '22 at 16:50

1 Answers1

-1

As TDG said, the names background_img (function) and background_img (tkinter.PhotoImage) override each other. You should rename one.

Oliver
  • 94
  • 8
  • Ok, I tried that and it's still giving me a blank screen. – manologgon Feb 26 '22 at 16:50
  • @manologgon Have you tried renaming the background image to `self.background_img`? See [Why don't tkinter images show up if created in a function?](https://stackoverflow.com/q/16424091/16775594). – Sylvester Kruin Feb 26 '22 at 17:08