After having issues tracking down an error on RPI3 - No Module Named ImageTk - I decided to do a complete reinstall of the operating system and start with a clean system. After doing so, the same error occurred. After reading several posts I make sure to install python-pil.imagetk, python-imaging-tk, python3-pil.imagetk, python3-pil-imagetk-dbg. After doing so, I still have the same error.
Python --version returns 2.7.9 - yet with my IDE I can also run 3 and 3.4.2. The error seems to occur in all cases.
In /usr/bin I have executables for python, python2, python2.7,python3,python3.4 - which I think is standard.
In /usr/lib I have an interesting mix of folders. None of them contains ImageTk. The python2.6 folder has only dist-packages and no mention of Image. The python 2.7 folder has many files and folders, including a comprehensive dist-packages, but no Image. Python3 folder has just dist-packages and no Image. The Python3.4 folder has many files and folders like 2.7.
My code is as follows - but it crashes on the Import ImageTk.
#!/usr/bin/env python
from tkinter import *
from PIL import Image #, ImageTk
import ImageTk
class Application(Frame):
""" build a basic window frame """
def __init__(self, master):
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
self.label1 = Label(self, text='My Label')
self.label1.grid(row=0, column=0, sticky=W)
self.button1 = Button(self, text="Submit", command=self.display)
self.button1.grid(row=0, column=1, sticky = W)
# create larger button
# use lambda: to prevent event function from executing when defined
self.largeButton1 = Button(self, text="Push Me", command=lambda:
self.displayWithParameter("One"))
self.largeButton1.config(width=4, height=3)
self.largeButton1.grid(row=1, column=1, sticky =W)
self.largeButton2 = Button(self, text="Push Me", command=lambda:
self.displayWithParameter("Two"))
self.largeButton2.config(width=4, height=3)
self.largeButton2.grid(row=1, column=6, sticky =W)
my_image = Image.open("Play30.png")
filename= ImageTk.PhotoImage(my_image)
# put image on label
label = Label(image=filename)
label.image = filename # keep a reference!
label.grid()
#put image on button
self.imageButton1 = Button(self, text="", image=filename,
command=self.display)
self.imageButton1.config(width=90, height=90)
self.imageButton1.grid(row=4, column=10, sticky =W)
def display(self):
# event handler for button
print("The button was clicked")
def displayWithParameter(self, varString):
print(varString)
root = Tk()
root.title('Application Window')
w, h = root.winfo_screenwidth(), root.winfo_screenheight()
root.geometry("%dx%d+0+0" % (w, h))
app = Application(root)
app.mainloop()
So when I install these various python imaging packages I am not sure where they are going - that is, to which version.
I have seen many posts for this ImageTk problem on the net, with many answers, but none of them seems to work. Perhaps I am just looking in the wrong place.