[Tutor] canvas -> make an object 'seem' to move

Teresa Stanton tms43 at clearwire.net
Fri May 4 20:09:58 CEST 2007


Here is how I've changed it, based on your message and the errors I received 
while putting it together:


# load and display several images using Tkinter
# Tkinter reads only GIF and PGM/PPM images
# (for additional formats use Image, ImageTk from PIL)

from Tkinter import *
import time

root = Tk()
root.title("Click me!")

def next_image(event):
    x0 = 0
    y0 = 0
    x1 = 1
    y1 = 0
    photoId = canvas1.create_image(x0, y0, image=photo)
    step = 10.0
    dur = .3

    for i in range(1, int(step)+3):
        xi = x0 + i*(x1-x0)/step
        yi = y0 + i*(y1-y0)/step
        canvas1.coords(photoId, xi, yi)
        time.sleep(3)
        canvas1.update_idletasks()

image = "DustY1.GIF"
photo = PhotoImage(file=image)

# make canvas the size of image1/photo1
width1 = photo.width()
height1 = photo.height()
canvas1 = Canvas(width=width1, height=height1)
canvas1.pack()

# display photo1, x, y is center (anchor=CENTER is default)
x = (width1)/2.0
y = (height1)/2.0
canvas1.create_image(x, y, image=photo)

canvas1.bind('<Button-1>', next_image)  # bind left mouse click

root.mainloop()

its not really working at all now.  I'm sure this is the right direction to 
go, but I'm still not getting anywhere.  I tried using the update, but it 
didn't seem to do anything.  At this point, I simply want movement from left 
to right.

help... ?



> On 04/05/07, Teresa Stanton <tms43 at clearwire.net> wrote:
>> the image and moves it.  I should mention that I've tried 'move() and
>> coord()' to get the object to move, but I am not getting the effect I 
>> want.
>> When I use move in successive steps it just appears at the last move
>> coordinates.
>
> My Tkinter is quite rusty, but I think this is basically the approach
> you need to take.  However, you will need to put a delay in between
> each call to coords, otherwise python will "optimise" by moving the
> image to its final destination before drawing.
>
> Something like:
>
> # given initial coords (x0,y0), final coords (x1, y1)
> img = canvas1.create_image(x0, y0, image=photo)
>
> # step: number of steps to move in
> step = 10.0
> # duration: number of seconds to move the image
> dur = 2
>
> for i in range(1, int(step)+1):
>    xi = x0 + i*(x1-x0)/step
>    yi = y0 + i*(y1-y0)/step
>    canvas1.coords(img, xi, yi)
>    time.sleep(dur/step)
>    # you may need to add a call to canvas1.update_idletasks() here
>
> # untested
>
> HTH!
>
> -- 
> John.
>
> 




More information about the Tutor mailing list