Skip to content Skip to sidebar Skip to footer

Move An Image In Python Using Tkinter

Guys I am working on a code which needs to move an image in python using Tkinter(canvas) This is creating problems for me. The image is being displayed but it is not moving. from T

Solution 1:

Canvas provides move method. Arguments are item you want to move, relative x offset from the previous position, y offset.

You need to save the return value of the create_image to pass it to the move method.

Also make sure the canvas is expandable (pack(expand=1, fill=BOTH) in the following code)

from Tkinter import *

root = Tk()

defnext_image(event):
    canvas1.move(item, 10, 0) # <--- Use Canvas.move method.

image1 = r"C:\Python26\Lib\site-packages\pygame\examples\data\ADN_animation.gif"
photo1 = PhotoImage(file=image1)
width1 = photo1.width()
height1 = photo1.height()
canvas1 = Canvas(width=width1, height=height1)
canvas1.pack(expand=1, fill=BOTH) # <--- Make your canvas expandable.
x = (width1)/2.0
y = (height1)/2.0
item = canvas1.create_image(x, y, image=photo1) # <--- Save the return value of the create_* method.
canvas1.bind('<Button-1>', next_image)
root.mainloop() 

UPDATE according to the comment

Using after, you can schedule the function to be called after given time.

defnext_image(event=None):
    canvas1.move(item, 10, 0)
    canvas1.after(100, next_image) # Call this function after 100 ms.

Post a Comment for "Move An Image In Python Using Tkinter"