import tkinter as tk root = tk.Tk() root.protocol("WM_DELETE_WINDOW", root.destroy) root.geometry("600x600") # Canvas canvas = tk.Canvas(root, width=600, height=600) canvas.pack() # Create circle circle = canvas.create_oval(10, 10, 60, 60, fill="red") # Animation control variables count = 0 running = True loop_count = 0 # Animate function def animate(): global count global running global loop_count # Move circle canvas.coords(circle, 10, 10, 60, 60) root.after(250, lambda: canvas.coords(circle, 540, 540, 590, 590)) root.after(500, lambda: canvas.coords(circle, 490, 10, 540, 60)) root.after(750, lambda: canvas.coords(circle, 10, 490, 60, 540)) count += 1 loop_count += 1 if loop_count < 20: root.after(1000, animate) else: stop() # Stop function def stop(): global loop_count loop_count = 0 root.quit() root.destroy() # Create stop button stop_button = tk.Button(root, text="Stop", command=stop) stop_button.pack() # Start animation animate() root.mainloop()