#! /usr/bin/env python # -*- coding: utf-8 -*- # Petit exercice utilisant la librairie graphique Tkinter from tkinter import * # définition des gestionnaires # d'événements : def move(): "déplacement de la balle" global x1, y1, vx, vy, dt, flag x1, y1 = x1 +vx*dt, y1 + vy*dt if x1 < 0 or x1 > 220: vx=-vx if y1 < 0 or y1 > 220: vy = -vy can1.coords(oval1,x1,y1,x1+30,y1+30) if flag >0: fen1.after(2,move) # boucler après 50 millisecondes def stop_it(): "arret de l'animation" global flag flag =0 def start_it(): "démarrage de l'animation" global flag if flag ==0: # pour éviter que le bouton ne puisse lancer plusieurs boucles flag =1 move() #========== Programme principal ============= # les variables suivantes seront utilisées de manière globale : x1, y1 = 40, 115 # coordonnées initiales vx, vy = 10, 5 # vitesse du déplacement dt=0.1 # pas temporel flag =0 # commutateur # Création du widget principal ("parent") : fen1 = Tk() fen1.title("Exercice d'animation avec Tkinter") # création des widgets "enfants" : can1 = Canvas(fen1,bg='dark grey',height=250, width=250) can1.pack(side=LEFT, padx =5, pady =5) oval1 = can1.create_oval(x1, y1, x1+30, y1+30, width=2, fill='red') bou1 = Button(fen1,text='Quitter', width =8, command=fen1.quit) bou1.pack(side=BOTTOM) bou2 = Button(fen1, text='Démarrer', width =8, command=start_it) bou2.pack() bou3 = Button(fen1, text='Arrêter', width =8, command=stop_it) bou3.pack() # démarrage du réceptionnaire d'évènements (boucle principale) : fen1.mainloop()