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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
from tkinter import *
from math import *
#calcul de la distance
def dist(a, b, c, d, e, f, g, h):
X1 = (a+b)//2
Y1 = (c+d)//2
X2 = (e+f)//2
Y2 = (g+h)//2
A = pow(X1-X2,2)
B = pow(Y1-Y2,2)
return sqrt(A+B)
##affichage distance
#creer une fonction qui modifiera un texte
def affichdist():
global x1, y1, x2, y2
distance = dist(x1,x1+10,y1,y1+10,x2,x2+20,y2,y2+20)
chaine1.configure(text="distance entre les astres = "+str(distance))
#deplacements des astres
#astre1
def depl1(hb,gd):
global x1,y1,x2,y2
x1,y1 = x1+gd, y1+hb
canv.coords(oval1,x1,y1,x1+10,y1+10)
affichdist()
def haut1():
depl1(-10,0)
def bas1():
depl1(10,0)
def gche1():
depl1(0,-10)
def dte1():
depl1(0,10)
#astre2
def depl2(hb,gd):
global x2,y2
x2, y2 = x2+gd, y2+hb
canv.coords(oval2, x2, y2, x2+20, y2+20)
affichdist()
def haut2():
depl2(-10,0)
def bas2():
depl2(10,0)
def gche2():
depl2(0,-10)
def dte2():
depl2(0,10)
##main
x1,y1,x2,y2 = 10,10,150,150
##interface##
fen1 = Tk()
canv = Canvas(fen1,height=300,width=300,bg='white')
canv.bind()
canv.grid(row=0,column=0,rowspan=7,columnspan= 4,pady=5,padx=5)
##astre 1
oval1 = canv.create_oval(x1,y1,x1+10,y1+10,fill="red")
Button(fen1,text='haut',command=haut1).grid(row=0,column=5)
Button(fen1,text='gauche',command=gche1).grid(row=1,column=4)
Button(fen1,text='droite',command=dte1).grid(row=1,column=6)
Button(fen1,text='bas',command=bas1).grid(row=2,column=5)
Label(fen1,text="astre 1").grid(row=1,column=5)
##astre 2
oval2 = canv.create_oval(x2,y2,x2+20,y2+20,fill="dark blue",width=2)
Button(fen1,text='haut',command=haut2).grid(row=4,column=5)
Button(fen1,text='gauche',command=gche2).grid(row=5,column=4)
Button(fen1,text='droite',command=dte2).grid(row=5,column=6)
Button(fen1,text='bas',command=bas2).grid(row=6,column=5)
Label(fen1,text="astre 2").grid(row=5,column=5)
##affichage distance
chaine1 = Label(fen1)
chaine1.grid(row=17, column=0)
##affichage force gravitationnelle
#creer une fonction qui modifiera un texte
fen1.mainloop() |
Partager