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
|
# -*- coding:Utf-8 -*-
#####################
# Script python 2.4 #
# Formation #
#####################
from Tkinter import *
def deplacement(gd, hd, indice):
"Fonction globale pour le déplacement"
if(indice == 1):
global x1, y1
x1, y1 = x1+gd, y1+hd
can.coords(astre1, x1, y1, x1+10, y1+10)
else:
global x2, y2
x2, y2 = x2+gd, y2+hd
can.coords(astre2, x2, y2, x2+10, y2+10)
def depl_gauche(indice):
"Déplacement d'un astre à gauche"
deplacement(-10, 0, indice)
def depl_droite(indice):
"Déplacement d'un astre à droite"
deplacement(10, 0, indice)
def depl_haut(indice):
"Déplacement d'un astre vers le haut"
deplacement(0, -10, indice)
def depl_bas(indice):
"Déplacement d'un astre vers le bas"
deplacement(0, 10, indice)
# coordonnées de l'astre 1 déclarées en global
x1, y1 = 20, 20 # coordonnées initiales
# coordonnées de l'astre 2 déclarées en global
x2, y2 = 120, 120 # coordonnées initiales
fen = Tk() # création de la fenêtre
# création des boutons pour diriger l'astre 1
Button(fen, text = 'Gauche', command = depl_gauche(1)).grid(row = 1, column = 1, sticky = E)
Button(fen, text = 'Droite', command = depl_droite(1)).grid(row = 2, column = 1, sticky = E)
Button(fen, text = 'Haut', command = depl_haut(1)).grid(row = 3, column = 1, sticky = E)
Button(fen, text = 'Bas', command = depl_bas(1)).grid(row = 4, column = 1, sticky = E)
# création du canevas central
can = Canvas(fen, width = 200, height = 200, bg = 'white')
can.grid(row = 1, column = 2, rowspan = 4, padx = 5, pady = 5)
# création de l'astre 1
astre1 = can.create_oval(x1, y1, x1+10, y1+10, fill = 'red')
# création de l'astre 2
astre2 = can.create_oval(x2, y2, x2+10, y2+10, fill = 'blue')
# création du label central
Label(fen, text = 'Distance').grid(row = 5, column = 2)
# création des boutons pour diriger l'astre 2
Button(fen, text = 'Gauche', command = depl_gauche(2)).grid(row = 1, column = 3, sticky = W)
Button(fen, text = 'Droite', command = depl_droite(2)).grid(row = 2, column = 3, sticky = W)
Button(fen, text = 'Haut', command = depl_haut(2)).grid(row = 3, column = 3, sticky = W)
Button(fen, text = 'Bas', command = depl_bas(2)).grid(row = 4, column = 3, sticky = W)
fen.mainloop() |