blob: f07e6589dd18b384d7c10d38af9aee0a730057d3 [file] [log] [blame]
Guido van Rossum35820f71994-10-07 09:55:26 +00001from Tkinter import *
2
3# this file demonstrates the movement of a single canvas item under mouse control
4
5class Test(Frame):
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
11 self.lastx = event.x
12 self.lasty = event.y
13
14
15 def mouseMove(self, event):
16 # whatever the mouse is over gets tagged as "current" for free by tk.
17 self.draw.move("current", event.x - self.lastx, event.y - self.lasty)
18 self.lastx = event.x
19 self.lasty = event.y
20
21 ###################################################################
22 ###### Event callbacks for canvas ITEMS (stuff drawn on the canvas)
23 ###################################################################
24 def mouseEnter(self, event):
25 # the "current" tag is applied to the object the cursor is over.
26 # this happens automatically.
27 self.draw.itemconfig("current", {"fill" : "red"})
28
29 def mouseLeave(self, event):
30 # the "current" tag is applied to the object the cursor is over.
31 # this happens automatically.
32 self.draw.itemconfig("current", {"fill" : "blue"})
33
34
35 def createWidgets(self):
36 self.QUIT = Button(self, {'text': 'QUIT',
37 'fg': 'red',
38 'command': self.quit})
39 self.QUIT.pack({'side': 'left', 'fill': 'both'})
40 self.draw = Canvas(self, {"width" : "5i", "height" : "5i"})
41 self.draw.pack({'side': 'left'})
42
43
44 fred = self.draw.create_oval(0, 0, 20, 20,
45 {"fill" : "green", "tag" : "selected"})
46
Guido van Rossumfaefe4c1996-05-24 18:40:46 +000047 self.draw.tag_bind(fred, "<Any-Enter>", self.mouseEnter)
48 self.draw.tag_bind(fred, "<Any-Leave>", self.mouseLeave)
Guido van Rossum35820f71994-10-07 09:55:26 +000049
50 Widget.bind(self.draw, "<1>", self.mouseDown)
51 Widget.bind(self.draw, "<B1-Motion>", self.mouseMove)
52
53 def __init__(self, master=None):
54 Frame.__init__(self, master)
55 Pack.config(self)
56 self.createWidgets()
57
58test = Test()
59test.mainloop()