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 60 61 62 63 64 65
| from tkinter import *
from math import sqrt
def distance(x1,y1,x2,y2):
d = sqrt((x2-x1)**2 + (y2-y1)**2)
return d
def forceG(m1,m2,di):
return m1*m2*6.67e-11/di**2
def pointeur(event):
x[n],y[n] = event.x,event.y
can.coords(astre[n],x[n]-10,y[n]-10,x[n]+10,y[n]+10)
di12 = distance(x[0],y[0],x[1],y[1])*1e9
di13 = distance(x[0],y[0],x[2],y[2])*1e9
di23 = distance(x[1],y[1],x[2],y[2])*1e9
if n == 0:
f = forceG(m1,m2,di12) + forceG(m1,m3,di13)
if n == 1:
f = forceG(m1,m2,di12) + forceG(m2,m3,di23)
if n == 2:
f = forceG(m1,m3,di13) + forceG(m2,m3,di23)
valFor.configure(text="Force = "+str(f)+" N")
def astre1():
global n
n = 0
def astre2():
global n
n = 1
def astre3():
global n
n = 2
m1,m2,m3 = 6e24,6e24,6e24
x,y = [20.,50.,80],[20.,20.,20]
astre = [0]*3
n = 0
fen = Tk()
fen.title("Gravitation selon Newton")
valFor = Label(fen,text="Force")
valFor.grid(row=1,column=1,padx=10,sticky=W)
can = Canvas(fen,bg="light grey",width=300,height=200)
can.grid(row=0,column=1,columnspan=2,padx=10,pady=10)
can.bind("<Button-1>",pointeur)
astre[0] = can.create_oval(x[0]-10,y[0]-10,x[0]+10,y[0]+10,fill="red",width =2)
astre[1] = can.create_oval(x[1]-10,y[1]-10,x[1]+10,y[1]+10,fill="blue",width =2)
astre[2] = can.create_oval(x[2]-10,y[2]-10,x[2]+10,y[2]+10,fill="green",width =2)
Button(fen,text="Astre 1",command=astre1).grid(row=0,column=3,pady=10,sticky=N)
Button(fen,text="Astre 2",command=astre2).grid(row=0,column=3,pady=40,sticky=N)
Button(fen,text="Astre 3",command=astre3).grid(row=0,column=3,pady=70,sticky=N)
fen.mainloop() |
Partager