chriswallis
chriswallis

Reputation: 9

Tkinter image does not show

I've been trying to set up a GUI using python and the Tkinter package. I am having a problem where the image does not show. Here is my code.

import Tkinter as tk
from PIL import Image, ImageTk


class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid()
        self.createWidgets()

    def createWidgets(self):
        self.image = Image.open("my_image.png")

        self.photo = ImageTk.PhotoImage(self.image)

        self.label = tk.Label(self, image=self.photo)
        self.label.image = self.photo # keep a reference!
        self.label.grid(row=0,column=1)



app = Application()
app.master.title("Sample application")
app.mainloop()   

I've included the keep refernce line suggested by others however it does not seem to be working. I'm using OS X 10.10.4 and Python 2.7.12 :: Anaconda custom (x86_64)

Thanks!

Upvotes: 0

Views: 124

Answers (2)

chriswallis
chriswallis

Reputation: 9

I solved this problem by updating my anaconda using conda update --prefix /Users/cwallis/anaconda anaconda. I did have a problem with there then being two versions of Tkinter which was solved with TK Framework double implementation issue

Upvotes: 0

Ray Donnelly
Ray Donnelly

Reputation: 4106

I tested this on Miniconda 2 on Linux and your sample worked just fine.

What do you mean by Python 2.7.12 :: Anaconda custom? Is it a self-built Python?

Upvotes: 1

Related Questions