0

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!

2 Answers2

1

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?

Ray Donnelly
  • 3,920
  • 1
  • 19
  • 20
  • I installed a standard anaconda package, I'm not sure what the custom meant. I'm not sure what the problem is I've looked at many examples of the image not coming up in Tkinter and this doesn't seem to fit any of them. I guess I should maybe re instal anaconda to see if that is the problem. Thanks for testing it out though! At least I now its not the code. – chriswallis Aug 16 '16 at 11:22
0

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

Community
  • 1
  • 1