Reputation:
so i was writing this code and when i run it my animated ball is going only right side but not turning back can you help me
import tkinter
canvas = tkinter.Canvas(bg="white",width=(900),height=(900))
canvas.pack()
def ball():
global x
canvas.delete("all")
canvas.create_oval(x-100,y-100,x+100,y+100,fill="orange",outline="black",width=4)
x = x+5
if x <800:
canvas.after(20,ball)
def ball_back():
global x
canvas.delete("all")
canvas.create_oval(x-100,y-100,x+100,y+100,fill="orange",outline="black",width=4)
x = x-5
if x >100:
canvas.after(20,ball_back)
x = 100
y = 300
ball()
ball_back()
Upvotes: 0
Views: 66
Reputation: 1414
The way Tkinter calls those methods is a little different than what you may expect. Both ball
and ball_back
are being called right away one after another. Since ball_back
fails the if
statement right out of the gate (x is greater than 100) it never gets called again. Try changing the last lines of ball
to this:
x = x+5
if x <800:
canvas.after(20,ball)
else:
canvas.after(20, ball_back)
Upvotes: 2