title: Tkinter - bouncing icon script
body: <p>I'm writing a simple bouncing icon program (Python 3.7, Windows 10 x64) to get the feel for Tkinter and canvases. I've posted my code below. My problem with the program is that it clips the edges of the icon (in the direction of motion).  If I slow the motion down a bit (by increasing the value in the after method) it no longer clips, but the motion is choppy. Maybe I'm overthinking this, it basically does what I've aimed for. But if this were a game or other project that mattered, how would this be prevented?</p>
  
  <pre><code>from tkinter import *
  import os
  from PIL import Image, ImageTk
  
  xinc, yinc = 5, 5
  
  def load_image(width, height, imgpath):
      loadimg = Image.open(imgpath)
      pwid, phi = loadimg.size
      pf1, pf2 = 1.0*width/pwid, 1.0*height/phi
      pfactor = min([pf1, pf2])
      pwidth, pheight = int(pwid*pfactor), int(phi*pfactor)
      loaded = loadimg.resize((pwidth, pheight), Image.ANTIALIAS)
      loaded = ImageTk.PhotoImage(loaded)
      return loaded
  
  def bounce():
      global xinc
      global yinc
      cwid = int(dash.cget('width'))
      chi = int(dash.cget('height'))
      x = dash.coords(dashposition)[0]
      y = dash.coords(dashposition)[1]
      if x &gt; cwid-10 or x &lt; 10:
          xinc = -xinc
      if y &gt; chi-10 or y &lt; 10:
          yinc = -yinc
      dash.move(dashposition, xinc, yinc)
      dash.after(15, bounce)
  
  root = Tk()
  root.configure(bg='black')
  
  dash = Canvas(root, bg='black', highlightthickness=0, width=400, height=300)
  dash.grid(row=0, column=0, padx=2, pady=2)
  
  imagepath = os.getcwd() + '/img/cloudy.png'
  image = load_image(20, 20, imagepath)
  
  x, y = 10, 10
  dashposition = dash.create_image(x, y, anchor=CENTER, image=image, tags=('current'))
  
  bounce()
  
  root.mainloop()
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/oZZRz.png" rel="nofollow noreferrer">cloudy.png</a></p>