954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Images not displaying in tkinter

I'm having an issue displaying images in tkinter, I've tried the methods suggested by effbot and a couple other sources to no avail. Here are a couple ways I've tried so far:

gmail=PhotoImage('gmail.gif')
        self.glab=Label(self,image='gmail.gif')
        self.glab.image=gmail
        self.glab.grid(row=3,column=4)

        gmail=PhotoImage('gmail.gif')
        self.glab=Label(self,image=gmail)
        self.glab.image=gmail
        self.glab.grid(row=3,column=4)

        self.glabim=PhotoImage('gmail.gif')
        self.glab=Label(self,image=self.glabim)
        self.glab.grid(row=3,column=4)


        gmail=PhotoImage('gmail.gif')
        self.lab=Label(self,image=gmail)
        self.lab.image=gmail
        self.lab.pack()


the last one actually froze the program completely, it wouldn't run. I'm sure the images are in the correct directory.

pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
 

What type self has? Maybe you should use self.grid?

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 


The Tkinter keyword is "image" not "self.image"; see the example here . Also, a label is usually in a frame or some other container. Finally, you can not mix grid() and pack() so choose one or the other.

gmail=PhotoImage(file='gmail.gif')
self.lab=Label(image=gmail)
self.lab.photo=gmail
self.lab.pack()
woooee
Nearly a Posting Maven
2,454 posts since Dec 2006
Reputation Points: 777
Solved Threads: 714
 

Here one example with tk.Tk class of vegaseat:

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk
    
class MyApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.root = tk.Frame()
        self.root.pack()
        
        self.image = tk.PhotoImage(file='gmail.gif')
        self.gmail = tk.Label(self.root, image=self.image)
        self.gmail.pack()
        
app = MyApp()
app.mainloop()


Same but inherit from tk.Frame:

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk
    

class MyApp(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.pack()

        self.image = tk.PhotoImage(file='gmail.gif')
        self.gmail = tk.Label(self, image=self.image)
        self.gmail.pack()
        
root= tk.Tk()
app = MyApp()
app.mainloop()
Attachments gmail.gif 14.36KB
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

It looks like my issue was the image, which is strange, but I'm going to just have to figure that one out I guess

pyguy62
Posting Whiz
353 posts since Aug 2011
Reputation Points: 34
Solved Threads: 19
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: