#! /usr/bin/env python
# -*- coding:Utf8 -*-

# Animation manuelle d'une balle dans un canevas

from tkinter import *

class Application(Tk):

    def __init__(self):
        # Création du widget principal
        Tk.__init__(self)
        self.title("Exercice d'animation avec tkinter")

        # Coordonnées initiales de position de la balle:
        self.x, self.y = 10, 10

        # Création des widgets "esclaves":
        self.can = Canvas(self, bg = "dark grey", height=300, width=300)
        self.balle = self.can.create_oval(self.x, self.y, self.x+30, self.y+30,
                                    width=2, fill='red')
        self.can.pack(side=LEFT)
        Button(self,text='Quitter',command=self.destroy).pack(side=BOTTOM)
        Button(self,text='Gauche',
               command=lambda gd=-10, hb=0 : self.avance(gd, hb)).pack()
        Button(self,text='Droite',
               command=lambda gd=10, hb=0 : self.avance(gd, hb)).pack()
        Button(self,text='Haut',
               command=lambda gd=0, hb=-10 : self.avance(gd, hb)).pack()
        Button(self,text='Bas',
               command=lambda gd=0, hb=10 : self.avance(gd, hb)).pack()


    def avance(self, gd, hb):
        "Méthode de déplacement de la balle"
        self.x, self.y = self.x+gd, self.y+hb
        self.can.coords(self.balle, self.x, self.y, self.x+30, self.y+30)
           

#------ Programme principal -------

Application().mainloop()
