Why does my object move in the wrong direction
<p>I have a made a simple program which is meant to move a ball left and right horizontally within a canvas. The user will use the left and right keys to move the ball accordingly by 5 pixels a time. If the  x coordinate of the ball is less than 40 or more than 240 then it will do nothing.</p>

<pre><code>try:    
    import tkinter as tk
except ImportError:
    import Tkinter as Tk

window = tk.Tk()
game_area = tk.Canvas(width=270, height=400, bd=0, highlightthickness=0,
                      bg="white")

ball = game_area.create_oval(10, 10, 24, 24, fill="red")
game_area.move(ball, 120, 4)
coords = 120

def move_left(event):
    global coords
    if coords &lt; 40:
        pass
    else:
        coords = int(coords)- 5
        game_area.move(ball, coords, 4)
    game_area.update()

def move_right(event):
    global coords
    if coords &gt; 240:
        pass
    else:
        coords = int(coords)+5
        game_area.move(ball, coords, 4)
    game_area.update()

window.bind("&lt;Left&gt;", move_left)
window.bind("&lt;Right&gt;", move_right)
game_area.pack()
window.mainloop()
</code></pre>

<p>However, pressing either key moves the ball towards the right (more than 5 pixels across) and off the screen despite the <code>if</code> function which is meant to prevent this.</p>

<p>According to the <a href="http://effbot.org/tkinterbook/canvas.htm#Tkinter.Canvas.move-method" rel="nofollow">Tkinter Canvas documentation</a>, the second argument to the <code>move</code> method, <code>dx</code>, is an offset. Try calling it like</p>

<pre><code>game_area.move(ball, -5, 4)
</code></pre>

<p>Then you don't need the following line, either.</p>

<pre><code>coords = int(coords)- 5
</code></pre>

python canvas tkinter coordinates