1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
| from tkinter import *
def clic(event): # La fonction correspondant à un clic (ou touche clavier)
global x,y
x=100
y=100
def bouge(): #La fonction principale qui anime notre objet
global x,y,dx,dy
A=0
w=0
t=0
dt=1
sin=0
cos=0
x+=A*sin*w*t
y=A*cos*w*t
t=t+dt
can.bind("<f>",clic) #Relie lévènement du clic à la fonction clic
can.coords(balle,x,y,x+10,y+10) #Affiche lobjet aux nouvelles coordonnées
fenetre.after(1,bouge) #Rafraîchit la fenêtre
# Création de la fenêtre
fenetre=Tk()
fenetre.title(" Mon animation ")
can=Canvas(fenetre,bg="yellow",height="600",width="600")
can.pack()
can.focus_set()
# Création et initialisation des variables
x,y=100,100
dx,dy=1,1
balle=can.create_oval(x,y,x+10,y+10,fill="red")
#Programme principal
bouge()
fenetre.mainloop() |
Partager