title: How can I achieve SMOOTH movement of canvas item in Tkinter?
body: <p>I am trying to play around with keyboard driven events in Tkinter so that I can move an object around a canvas widget. Up, Down, Left and Right work OK but when I try to program two keys together the movement is not a smooth diagonal motion. Furthermore, whenever a key is hit, the object moves, then there is a slight delay, and then it moves smoothly.
  How can I achieve smooth movement THE MOMENT a key is pressed and how can I achieve a smooth diagonal motion?</p>
  
  <p>This is the code so far:</p>
  
  <pre><code>from tkinter import *
  
  x = 10
  y = 10
  a = 100
  b = 100
  
  def change_coord(event):
          if event.keysym == 'Up':
              canvas1.move(rect, 0,-5)
          if event.keysym == 'Up' and 'Right':
              canvas1.move(rect, 5,-5)
          if event.keysym == 'Up' and 'Left':
              canvas1.move(rect, -5,-5)
          if event.keysym == 'Down':
              canvas1.move(rect, 0,5)
          if event.keysym == 'Down' and 'Right':
              canvas1.move(rect, 5,5)
          if event.keysym == 'Down' and 'Left':
              canvas1.move(rect, -5,5)
          if event.keysym == 'Right':
              canvas1.move(rect, 5,0)
          if event.keysym == 'Left':
              canvas1.move(rect, -5,0)
  
  window = Tk()
  window.geometry("400x200")
  
  #canvas and drawing
  canvas1=Canvas(window, height = 200, width = 400)
  canvas1.grid(row=0, column=0, sticky=W)
  coord = [x, y, a, b]
  rect = canvas1.create_rectangle(*coord, outline="#fb0", fill="#fb0")
  
  #capturing keyboard inputs and assigning to function
  window.bind_all('&lt;Up&gt;', change_coord)
  window.bind_all('&lt;Down&gt;', change_coord)
  window.bind_all('&lt;Left&gt;', change_coord)
  window.bind_all('&lt;Right&gt;', change_coord)
  window.mainloop()
  </code></pre>
  
  <p>Many thanks!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Thanks for all your suggestions. I have been trying to implement an animation loop to work around the slight delay after hitting an arrow key but am now stuck. My NEW code is shown below but running the program doesn't result in the canvas item moving. Firstly, is the idea for the animation loop correct and secondly where do I call the 'move' function to get the item moving. Please help - thanks!</p>
  
  <pre><code>from tkinter import *
  
  x = 10
  y = 10
  a = 100
  b = 100
  direction = None
  
  def move():
      global x_vel
      global y_vel
      global direction
      if direction is not None:
          canvas1.move(rect, x_vel,y_vel)
          after(33,move)
  
  def on_keypress(event):
      global direction
      global x_vel
      global y_vel
      if event.keysym == "Left":
          direction == "left"
          x_vel = -5
          y_vel = 0
      if event.keysym == "Right":
          direction == "right"
          x_vel = 5
          y_vel = 0
      if event.keysym == "Down":
          direction == "down"
          x_vel = 0
          y_vel = 5
      if event.keysym == "Up":
          direction == "up"
          x_vel = 0
          y_vel = -5
  
  def on_keyrelease(event):
      global direction
      direction = None
  
  
  window = Tk()
  window.geometry("400x200")
  
  #canvas and drawing
  canvas1=Canvas(window, height = 200, width = 400)
  canvas1.grid(row=0, column=0, sticky=W)
  coord = [x, y, a, b]
  rect = canvas1.create_rectangle(*coord, outline="#fb0", fill="#fb0")
  
  #capturing keyboard inputs and assigning to function
  window.bind_all('&lt;KeyPress&gt;', on_keypress)
  window.bind_all('&lt;KeyRelease&gt;', on_keyrelease)
  window.mainloop()
  </code></pre>