PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, May 7, 2022

[FIXED] How can I load an image into tkinter?

 May 07, 2022     image, python, tkinter     No comments   

Issue

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.


Solution

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



Answered By - Oliver
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing