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:

  1. You create a Tkinter window and canvas to display the rotating image.
  2. Load an image using the Pillow (PIL) library and create a PhotoImage object.
  3. Create an image item on the canvas at the initial position.
  4. Define a function 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.
  5. Start the rotation animation by calling rotate_image().
  6. Finally, run the Tkinter main loop with 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:

  • Method 1: Using a Lambda Function with 'after'
    ​​​​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))
    
    Explanation:
    • This method is similar to previous Method but uses a lambda function within the after method call.
    • The lambda function allows us to pass the current canvas, image_item, and updated angle as arguments to rotate_image_lambda.
    • The rest of the process is the same as in previous Method.
  • Method 2: Using an External Function with 'after'
    ​​​​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()
    
    Explanation:
    • In this method, we define an external function rotate_image_external that manages the animation.
    • Inside rotate_image_external, we define a local variable angle to keep track of the current rotation angle.
    • We define an inner function rotate that performs the rotation and updates the canvas.
    • The nonlocal keyword is used to modify the angle variable from the outer function within the inner function.
    • Inside 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.
    • Finally, we call rotate() to start the animation.