To animate a rotating image in a Tkinter canvas in Python, we can use the create_image()
method of the canvas to create an image object and then use the after()
method to repeatedly call a function that rotates the image by a certain angle and updates its position on the canvas [3].
Here's an example code snippet that animates a rotating image in a Tkinter canvas without using a class:
import tkinter as tk
from PIL import Image, ImageTk
# Create a Tkinter window
window = tk.Tk()
window.title("Rotating Image Animation")
# Create a canvas
canvas = tk.Canvas(window, width=400, height=400)
canvas.pack()
# Load an image
image = Image.open("your_image.png")
image = image.resize((100, 100)) # Resize the image as needed
photo = ImageTk.PhotoImage(image)
# Create an image item on the canvas
image_item = canvas.create_image(200, 200, image=photo)
# Function to update the rotation angle
def rotate_image(angle=0):
canvas.delete("all")
rotated_image = ImageTk.PhotoImage(image.rotate(angle))
canvas.create_image(200, 200, image=rotated_image)
canvas.after(100, lambda: rotate_image((angle + 10) % 360)) # Rotate by 10 degrees
# Start the rotation animation
rotate_image()
# Run the Tkinter main loop
window.mainloop()
In this example:
rotate_image
that takes an angle as an argument, deletes the existing image, rotates the image by the specified angle, and creates a new image on the canvas. It then schedules itself to be called after a delay (100 milliseconds in this case) to animate the rotation.rotate_image()
.window.mainloop()
to display the animated rotating image.Make sure to replace your_image.png
with the actual path to your image file, and adjust the canvas size, image size, and rotation speed as needed.
However, there are also alternate methods for same functionality give below:
ββββdef rotate_image_lambda(canvas, image_item, angle):
ββββ rotated_image = ImageTk.PhotoImage(image.rotate(angle))
ββββ canvas.itemconfig(image_item, image=rotated_image)
ββββ canvas.image = rotated_image # Store the reference to prevent garbage collection
ββββ canvas.after(50, lambda: rotate_image_lambda(canvas, image_item, (angle + 5) % 360))
after
method call.canvas
, image_item
, and updated angle
as arguments to rotate_image_lambda
.ββββdef rotate_image_external():
ββββ angle = 0
ββββ def rotate():
ββββ nonlocal angle
ββββ rotated_image = ImageTk.PhotoImage(image.rotate(angle))
ββββ canvas.itemconfig(image_item, image=rotated_image)
ββββ canvas.image = rotated_image
ββββ angle = (angle + 5) % 360
ββββ canvas.after(50, rotate)
ββββ rotate()
rotate_image_external
that manages the animation.rotate_image_external
, we define a local variable angle
to keep track of the current rotation angle.rotate
that performs the rotation and updates the canvas.nonlocal
keyword is used to modify the angle
variable from the outer function within the inner function.rotate
, we perform the rotation, update the image on the canvas, update the angle, and use after
to schedule the rotate
function to be called repeatedly with a delay of 50 milliseconds.rotate()
to start the animation.