| 12
 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
 
 |  
import tkinter
import threading
import time
 
class Appl(object) :
    def __init__(self) :
        # Création de la fenetre :
        self.root = tkinter.Tk()
        self.can = tkinter.Canvas(self.root, width=600, height=600, bg='ivory')
        self.can.grid(row=0, column=0)
        self.balleR = self.can.create_oval(85,85,115,115, fill='red')
        self.balleB = self.can.create_oval(485,485,515,515, fill='blue')
 
        # Création de deux threads pour un traitement multitache :
        # La méthode start de la classe threading.Thread dont dérive 
        # nos classes ThreadBallR et ThreadBallB va lancera elle-même
        # la méthode run que nous allons surchargée
        tbr = ThreadBallR(self.can, self.balleR)
        tbr.start()
        tbb = ThreadBallB(self.can, self.balleB)
        tbb.start()
 
 
 
class ThreadBallR(threading.Thread) :
    def __init__(self, can, balle) :
        threading.Thread.__init__(self)
        self.can , self.balle = can , balle
 
    def run(self) :
        self.can.focus_set()
        self.can.bind("<Up>", self.moove)
        self.can.bind("<Down>", self.moove)
        self.can.bind("<Right>", self.moove)
        self.can.bind("<Left>", self.moove)
 
 
    def moove(self, event) :
        if event.keysym == 'Up' :
            self.can.move(self.balle, 0, -5)
        if event.keysym == 'Down' :
            self.can.move(self.balle, 0, 5)
        if event.keysym == 'Right' :
            self.can.move(self.balle, 5, 0)
        if event.keysym == 'Left' :
            self.can.move(self.balle, -5, 0)
        time.sleep(0.01)
 
 
 
class ThreadBallB(threading.Thread) :
    def __init__(self, can, balle) :
        threading.Thread.__init__(self)
        self.can , self.balle = can , balle
 
    def run(self) :
        self.can.focus_set()
        self.can.bind("<KeyPress-e>", self.moove)
        self.can.bind("<KeyPress-x>", self.moove)
        self.can.bind("<KeyPress-d>", self.moove)
        self.can.bind("<KeyPress-s>", self.moove)
 
 
    def moove(self, event) :
        if event.keysym == 'e' :
            self.can.move(self.balle, 0, -5)
        if event.keysym == 'x' :
            self.can.move(self.balle, 0, 5)
        if event.keysym == 'd' :
            self.can.move(self.balle, 5, 0)
        if event.keysym == 's' :
            self.can.move(self.balle, -5, 0)
        time.sleep(0.01)
 
 
 
 
 
Appl().root.mainloop() | 
Partager