1- Python Application   
   

Draw line application

This application in Python is an drawing tool.
   3 radio button allow users to create, move or delete lines.
   The radio button are code directly without using python's library because of the genericity.

telecharger

Code (about 130 lines)

from Tkinter import *
from time import *

class droite(Frame):  
    
    def __init__(self,master=None,height=400, width=400):
        #init of the Canvas object
        Frame.__init__(self, master)
        self.pack(fill=BOTH, expand=1)
        self.drawing = Canvas(self, bg="white", width=400, height=400)
        self.drawing.focus_set()
        self.drawing.pack()
        self.id = self.drawing.create_rectangle(0,350,500,500,width=5.0, fill='grey')
        self.drawing.create_text(50, 380, text="¤", fill="red")
        self.drawing.create_text(70, 380, text="Insert")
        self.drawing.create_text(200, 380, text="o")
        self.drawing.create_text(220, 380, text="Move")
        self.drawing.create_text(330, 380, text="o")
        self.drawing.create_text(350, 380, text="Delete")
        self.modeString = "Insert"
        self.optioncourante = "Insert"
        self.begining()
        
    def begining(self):        
        self.drawing.bind("<ButtonPress-1>", self.execute)
        self.drawing.bind("<ButtonRelease-1>", self.stopmoving)
        self.drawing.bind("<B1-Motion>", self.move)  
        self.drawing.bind("<Key>", self.keybords)
    
    def keybords(self, event):
        if (event.keysym == "Tab"):
            if (self.optioncourante == "Insert"):
                self.optioncourante = "Move"
                self.choixOptionMove()
            elif (self.optioncourante == "Move"):
                self.optioncourante = "Delete"
                self.choixOptionDelete()
            else:
                self.optioncourante = "Insert"
                self.choixOptionInsert()
                
        if (event.keysym == "space"):
            if (self.optioncourante == "Insert"):
                self.choixInsert()
            elif (self.optioncourante == "Move"):
                self.choixMove()
            else:
                self.choixDelete()
                
                
    def execute(self, event):
        if (event.y < 348):
            
            if self.modeString == "Insert":
                self.premier(event)
            
            elif self.modeString == "Delete":
                self.Obj = self.drawing.find_closest(event.x, event.y)
                if (self.Obj[0] == self.id):
                    self.begining()
                else:
                    self.drawing.delete(self.drawing.find_closest(event.x, event.y))
                    self.begining()
                
            elif self.modeString == "Move":
                self.moveMode = "Moving"
                self.movingObject = self.drawing.find_closest(event.x, event.y)
                if (self.movingObject[0] == self.id):
                    self.movingObject = None
                    self.moveMode = "idle"
                    self.begining()
                
                
        else:
            if(event.x > 30 and event.x <  80):
                self.choixInsert()
            elif (event.x > 180 and event.x <  240):
                self.choixMove()
            elif (event.x > 310 and event.x <  370):
                self.choixDelete()
    
    def choixInsert(self):
        self.drawing.itemconfigure(self.drawing.find_closest(50,380),fill="red",text="¤")
        self.drawing.itemconfigure(self.drawing.find_closest(200,380),fill="black", text="o")
        self.drawing.itemconfigure(self.drawing.find_closest(330,380),fill="black", text="o")
        self.modeString = "Insert"
        self.begining()
        
    def choixMove(self):
        self.drawing.itemconfigure(self.drawing.find_closest(50,380),fill="black",text="o")
        self.drawing.itemconfigure(self.drawing.find_closest(200,380),fill="red", text="¤")
        self.drawing.itemconfigure(self.drawing.find_closest(330,380),fill="black", text="o")
        self.modeString = "Move"
        self.begining()   
        
    def choixDelete(self):
        self.drawing.itemconfigure(self.drawing.find_closest(50,380),fill="black",text="o")
        self.drawing.itemconfigure(self.drawing.find_closest(200,380),fill="black", text="o")
        self.drawing.itemconfigure(self.drawing.find_closest(330,380),fill="red", text="¤")
        self.modeString = "Delete"
        self.begining()  
        
    def choixOptionInsert(self):
        self.drawing.itemconfigure(self.drawing.find_closest(70,380),fill="brown")
        self.drawing.itemconfigure(self.drawing.find_closest(220,380),fill="black")
        self.drawing.itemconfigure(self.drawing.find_closest(350,380),fill="black")
        self.begining()
        
    def choixOptionMove(self):
        self.drawing.itemconfigure(self.drawing.find_closest(70,380),fill="black")
        self.drawing.itemconfigure(self.drawing.find_closest(220,380),fill="brown")
        self.drawing.itemconfigure(self.drawing.find_closest(350,380),fill="black")
        self.begining()   
        
    def choixOptionDelete(self):
        self.drawing.itemconfigure(self.drawing.find_closest(70,380),fill="black")
        self.drawing.itemconfigure(self.drawing.find_closest(220,380),fill="black")
        self.drawing.itemconfigure(self.drawing.find_closest(350,380),fill="brown")
        self.begining()  
        
    def stopmoving(self, event):
        if self.modeString == "Move":
            self.moveMode = "Idle"
            self.begining()            
        
    def move(self, event):
        if (event.y < 350):
            if self.modeString == "Move":
                if self.moveMode == "Moving":
                    self.oldCoords=self.drawing.coords(self.movingObject)
                    self.drawing.move(self.movingObject,event.x-self.oldCoords[0],event.y-self.oldCoords[1])

    def premier(self, event):
        if self.modeString == "Insert":
            self.p1 = (event.x, event.y)
            self.drawing.bind("<Button-1>", self.deuxieme)   
        
    def deuxieme(self, event):
        if (event.y < 350):
            if self.modeString == "Insert":
                self.p2 = (event.x, event.y)
                self.drawing.create_line((self.p1[0],self.p1[1],self.p2[0],self.p2[1]),fill="red",width=2)
                self.begining()
               
root = Tk()
droite(root)
root.mainloop()
Maintained by Julien Gardette. Last Modified: 2008/09/10 00:03:05.