from tkinter import * window = Tk() window.title("Canvas Animation") x = 10 y = 10 a = 50 b = 50 x2 = 10 y2 = 10 c = 50 d = 50 x_vel = 8 y_vel = 8 x2_vel = 10 y2_vel = 10 def move(): global x global y global a global b global x_vel global y_vel if x < 10: x_vel = 8 if x > 350: x_vel = -8 if y < 10: y_vel = 8 if y > 150: y_vel = -8 canvas.move(circle, x_vel, y_vel) coordinates = canvas.coords(circle) x = coordinates[0] y = coordinates[1] window.after(33, move) def move2(): global x2 global y2 global c global d global x2_vel global y2_vel if x2 < 50: x2_vel = 10 if x2 > 300: x2_vel = -10 if y2 < 50: y2_vel = 10 if y2 > 100: y2_vel = -10 canvas.move(circle2, x2_vel, y2_vel) coordinates = canvas.coords(circle2) x2 = coordinates[0] y2 = coordinates[1] window.after(33, move2) canvas = Canvas(window, height=200, width=400, bg="#ffc929") canvas.grid(row=0, column=0) coord = [x, y, a, b] circle = canvas.create_oval(coord, outline="#ffe100", fill="#ffe100") coord2 = [x2, y2, c, d] circle2 = canvas.create_oval(coord2, outline="#ffa200", fill="#ffa200") move() move2() window.mainloop()