blob: fab480b782246694a8f06b55d1351006ccc2c67e [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
47 self.draw.bind(fred, "<Any-Enter>", self.mouseEnter)
48 self.draw.bind(fred, "<Any-Leave>", self.mouseLeave)
49
50
51 Widget.bind(self.draw, "<1>", self.mouseDown)
52 Widget.bind(self.draw, "<B1-Motion>", self.mouseMove)
53
54 def __init__(self, master=None):
55 Frame.__init__(self, master)
56 Pack.config(self)
57 self.createWidgets()
58
59test = Test()
60test.mainloop()