Problem with PIL/Tkinter Program Example

W

W. eWatson

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
 
P

Peter Otten

W. eWatson said:
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?
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

If you want to use the ImageTk module you have to import it first:
#Mouse movement
from Tkinter import *
import PIL
import Image

import ImageTk

[rest of your code here]

Peter
 
W

W. eWatson

Peter said:
W. eWatson said:
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?
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

If you want to use the ImageTk module you have to import it first:
#Mouse movement
from Tkinter import *
import PIL
import Image

import ImageTk

[rest of your code here]

Peter
Very good. Thanks.

--
W. eWatson

(121.015 Deg. W, 39.262 Deg. N) GMT-8 hr std. time)
Obz Site: 39° 15' 7" N, 121° 2' 32" W, 2700 feet

Web Page: <www.speckledwithstars.net/>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top