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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| #!/usr/bin/env python3
# -*- coding: utf-8 -*-
from tkinter import *
from math import sin, cos, sqrt
def bouge():
"La fonction principale qui anime notre objet"
global x, y, t
x = A*cos(w*t)
y = sqrt(A**2 - x**2)
t += dt
# déplace les objets graphiques
canvas.coords(ligne, x0, y0, x0+x, y0+y)
canvas.coords(balle, x0+x-rayon, y0+y-rayon, x0+x+rayon, y0+y+rayon)
# prochaine boucle dans xxx millisecondes
fenetre.after(50, bouge)
# end def
# création de la fenêtre
fenetre = Tk()
fenetre.title("Animation Pendule")
fenetre.resizable(width=False, height=False)
# création canevas graphique
canvas = Canvas(fenetre, bg="sky blue", width=400, height=400)
canvas.pack()
# bouton quitter
Button(
fenetre, text="Quitter", command=fenetre.destroy
).pack(side=RIGHT, padx=5, pady=5)
# initialisation variables globales
A = 100
w = 0.2
t = 0
dt = 0.2
# init coords point central canevas
x0, y0 = canvas.winfo_reqwidth()//2, canvas.winfo_reqheight()//2
# rayon balle
rayon = 25
# création objets graphiques
ligne = canvas.create_line(0, 0, 0, 0, width=3)
balle = canvas.create_oval(0, 0, 0, 0, fill="red")
# programme principal
bouge()
fenetre.mainloop() |