0

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.

  • I assume this worked since you accepted the answer, but it is exactly the same info I gave you in a comment to your previous question. You should also delete your other question as it is a duplicate and this one has an accepted answer. – Steve Robillard Jul 18 '17 at 21:34
  • Had to reinstall OS for this to work. System won't let me delete the previous post. Thx. – Glenn Jul 19 '17 at 12:41
1

For Python 2.x try this:

sudo apt-get install python-pil python-imaging python-imaging-tk

then open the Python console (type python on the command line) and type this:

import ImageTk

if it returns nothing everything is working.

If you intend to run this in Python2.x you probably need to change:

from tkinter import *

to

from Tkinter import *

The module name is capitalized differently for Python 2 and Python 3.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.