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
| # Chargement des bibliothèques graphique et mathématique :
from tkinter import *
from math import *
# événements souris :
def pointeur(event):
if choixAstre == 'deplTerre' :
can.coords(terre, event.x-45,event.y-45, event.x+45,event.y+45)
elif choixAstre == 'deplLune' :
can.coords(lune, event.x-15,event.y-15, event.x+15,event.y+15)
elif choixAstre == 'deplSatellite' :
can.coords(satellite, event.x-7,event.y-7, event.x+7,event.y+7)
# événements boutons du menu :
def choixAstreT():
global choixAstre
choixAstre = 'deplTerre'
def choixAstreL():
global choixAstre
choixAstre = 'deplLune'
def choixAstreS():
global choixAstre
choixAstre = 'deplSatellite'
############################# Début du programme principal (main) ###############################
xl1, yl1 = 280, 150 # coordonnées initiales de la lune (vatiables globales)
xt1, yt1 = 100, 180 # coordonnées initiales de la terre (vatiables globales)
xs1, ys1 = 200, 250 # coordonnées initiales du satellite (vatiables globales)
choixAstre = ''
labelHaut = 'Label vide pour le moment'
labelBas = StringVar()
# calcul distance terre lune :
distTerreLune = sqrt(abs(xl1-yl1)**2 + abs(xt1-yt1)**2)
labelBas.set('Distance Terre Lune = ' + str(distTerreLune) + ' pixels')
# Création du widget principal ("maître") :
fen = Tk()
fen.title('Système solaire') # texte haut de la fenètre
# création des widgets "esclaves" et leurs positionements :
label1 = Label(fen, text=labelHaut).grid(row =1, column =1, columnspan =2)
label2 = Label(fen, textvariable=labelBas).grid(row =22, column =1, columnspan =2)
can = Canvas(fen,bg='black',height=400,width=600)
can.bind('<Button-1>', pointeur) # souris dans le canva
terre = can.create_oval(xt1,yt1,xt1+90,yt1+90,width=2,fill='blue') # widget terre
lune = can.create_oval(xl1,yl1,xl1+30,yl1+30,width=2,fill='white') # widget lune
satellite = can.create_oval(xs1,ys1,xs1+14,ys1+14,width=2,fill='red') # widget satellite
can.grid(row =2, column =1, rowspan =20)
# boutons du menu
Button(fen,text='Depl. Terre',width=15,command=choixAstreT).grid(row =2, column =2)
Button(fen,text='Depl. Lune',width=15,command=choixAstreL).grid(row =3, column =2)
Button(fen,text='Depl. Satellite',width=15,command=choixAstreS).grid(row =4, column =2)
Button(fen,text='Quitter',width=15,command=fen.quit).grid(row =21, column =2)
# démarrage du réceptionnaire d'évènements (boucle principale) :
fen.mainloop() |
Partager