0

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.

Edited by pyguy62: n/a

3
Contributors
4
Replies
7
Views
5 Years
Discussion Span
Last Post by pyguy62
0

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()

Edited by woooee: n/a

1

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()

Edited by pyTony: Better variable name, pack order

Votes + Comments
Best solution
Attachments gmail.gif 14.36 KB
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.