3
# this file demonstrates the movement of a single canvas item under mouse control
6
###################################################################
7
###### Event callbacks for THE CANVAS (not the stuff drawn on it)
8
###################################################################
9
def mouseDown(self, event):
10
# remember where the mouse went down
14
def mouseMove(self, event):
15
# whatever the mouse is over gets tagged as CURRENT for free by tk.
16
self.draw.move(CURRENT, event.x - self.lastx, event.y - self.lasty)
20
###################################################################
21
###### Event callbacks for canvas ITEMS (stuff drawn on the canvas)
22
###################################################################
23
def mouseEnter(self, event):
24
# the CURRENT tag is applied to the object the cursor is over.
25
# this happens automatically.
26
self.draw.itemconfig(CURRENT, fill="red")
28
def mouseLeave(self, event):
29
# the CURRENT tag is applied to the object the cursor is over.
30
# this happens automatically.
31
self.draw.itemconfig(CURRENT, fill="blue")
33
def createWidgets(self):
34
self.QUIT = Button(self, text='QUIT', foreground='red',
36
self.QUIT.pack(side=LEFT, fill=BOTH)
37
self.draw = Canvas(self, width="5i", height="5i")
38
self.draw.pack(side=LEFT)
40
fred = self.draw.create_oval(0, 0, 20, 20,
41
fill="green", tags="selected")
43
self.draw.tag_bind(fred, "<Any-Enter>", self.mouseEnter)
44
self.draw.tag_bind(fred, "<Any-Leave>", self.mouseLeave)
46
Widget.bind(self.draw, "<1>", self.mouseDown)
47
Widget.bind(self.draw, "<B1-Motion>", self.mouseMove)
49
def __init__(self, master=None):
50
Frame.__init__(self, master)