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
   |  
#!/usr/bin/python
# -*- coding: iso-8859-1 -*-
 
from Tkinter import *
from math import *
 
seedList = []
 
class Env_Classification (Frame):
	" classification des Seeds "
	def __init__(self, larg=400, haut=700):
		Frame.__init__(self)											# Appel au constructeur de la classe parente
		self.larg, self.haut = larg, haut								# Memorisation des variables
		self.can = Canvas(self, width=larg, height=haut, bg ='light grey')	# Initialisation du canvas conteneur
		self.can.pack(padx =5, pady =3)
		Bouton(self, text="New seed", command=self.newSeed)				# Bouton d'ajout d'un nouveau Seed
		self.pack()														# Affichage
 
	def newSeedBinding(self, tag=None) :
		"	Initialise un gestionnaire d'evenements pour chaque Seed	"
		self.tag = tag
		self.can.tag_bind(tag, "<Button-1>", self.mouseDown)
		self.can.tag_bind(tag, "<Button1-Motion>", self.mouseMove)
		self.can.tag_bind(tag, "<Button1-ButtonRelease>", self.mouseUp)
 
	def newSeed(self) :
		"	methode affichant un nouveau seed	"
		nm = "seed_" + str(len(seedList))
		Seed(self.can, tag=nm)
		self.newSeedBinding(nm)
		seedList.append(nm)
 
	def mouseDown(self, event):
		"Op. à effectuer quand le bouton gauche de la souris est enfoncé"
		self.currObject =None
		# event.x et event.y contiennent les coordonnées du clic effectué :
		self.x1, self.y1 = event.x, event.y
		# <find_closest> renvoie la référence du dessin le plus proche :
		self.selObject = self.can.find_closest(self.x1, self.y1)
		# modification de l'épaisseur du contour du dessin :
		self.can.itemconfig(self.selObject, width =10)
	def mouseMove(self, event):
		"Op. à effectuer quand la souris se déplace, bouton gauche enfoncé"
		x2, y2 = event.x, event.y
		dx, dy = x2 -self.x1, y2 -self.y1
		if self.selObject:
			currentTag = self.can.gettags(CURRENT)
			self.can.move(currentTag[0], dx, dy)
			self.x1, self.y1 = x2, y2
	def mouseUp(self, event):
		"Op. à effectuer quand le bouton gauche de la souris est relâché"
		if self.selObject:
			self.can.itemconfig(self.selObject, width =1)
			self.selObject =None
 
class Seed :
	""" information """
	def __init__(self, boss, x=200, y=350, text='empty unit', tag=None):
		self.boss = boss            # référence du canevas
		self.text = text
		self.tag = tag
		self.max = 0
		self.x, self.y = x, y     # Coordonnees  du Seed
		# dessiner le Seed :
		self.hSeed = 20               # hauteur du Seed
		self.lSeed = 200               # largeur du Seed
		self.x1, self.y1 = x-self.lSeed/2, y-self.hSeed
		self.x2, self.y2 = x+self.lSeed/2, y+self.hSeed
		self.SeedF = boss.create_rectangle(self.x1, self.y1, self.x2, self.y2, width =3, fill="ivory", tags=self.tag)
		self.SeedT = boss.create_text(self.x, self.y, text=self.text, tags=self.tag)
 
class Bouton (Button):
	" Design general des boutons "
	def __init__(self, boss, larg=12, haut=1, text="button", command=None):
		Button.__init__(self)
		self.boss = boss
		self.text = text
		self.command = command
		self.larg, self.haut = larg, haut
 
		self.bouton = Button(boss, width=larg, height=haut, bg ='ivory', activebackground="red", activeforeground="black",
						anchor=CENTER, bd=3, fg="blue", highlightcolor="black", highlightthickness=4, text=self.text, command=self.command).pack(side=LEFT, padx =5, pady =3)
 
if __name__ == '__main__':
	# Code pour tester sommairement la classe Seed :
	Env_Classification().mainloop()  |