Problem with PIL/Tkinter Program Example

W. eWatson notvalid2 at sbcglobal.net
Thu Apr 9 06:22:46 EDT 2009



Something is amiss here. The program produces a canvas in which one can move 
an object around. The input file is hard coded (see open). If you want to 
try it, you'll need to provide a file. Python error below. Name space 
difficulty?

#Mouse movement
from Tkinter import *
import PIL
import Image


class data:
     startx=0
     starty=0

def startmotioncallback(event):
     data.startx=event.x
     data.starty=event.y


def motioncallback(event):
     deltax=event.x-data.startx
     deltay=event.y-data.starty
     data.startx=event.x
     data.starty=event.y
     # should put some limits on where the cirle is moved
     # left as exercise.....
     data.cnv.move(data.ring,deltax,deltay)

def Demo(root):
     # resize window
     root.geometry('400x400+0+0')
     data.root=root
     # make a canvas
     cnv=Canvas(root)
     cnv.pack(expand=1,fill=BOTH)
     data.cnv=cnv
     img=Image.open('jupa9810.jpg')  # some image you have.....
     raw_input("Hello")
     data.img=ImageTk.PhotoImage(img)
     data.photo=cnv.create_image(0,0,image=data.img,anchor='nw')
     data.ring=cnv.create_oval((100,100,300,300))
     cnv.bind("<B1-Motion>",motioncallback)
     cnv.bind("<Button-1>",startmotioncallback)
     root.mainloop()

Traceback (most recent call last):
   File 
"C:\Sandia_Meteors\Sentinel_Development\Development_Sentuser-Utilities\Playground\fun-move_object.py", 
line 45, in <module>
     Demo(root)
   File 
"C:\Sandia_Meteors\Sentinel_Development\Development_Sentuser-Utilities\Playground\fun-move_object.py", 
line 35, in Demo
     data.img=ImageTk.PhotoImage(img)
NameError: global name 'ImageTk' is not defined



More information about the Python-list mailing list