3 # this file demonstrates a more sophisticated movement --
4 # move dots or create new ones if you click outside the dots
7 ###################################################################
8 ###### Event callbacks for THE CANVAS (not the stuff drawn on it)
9 ###################################################################
10 def mouseDown(self
, event
):
11 # see if we're inside a dot. If we are, it
12 # gets tagged as "current" for free by tk.
14 if not event
.widget
.find_withtag("current"):
15 # there is no dot here, so we can make one,
16 # and bind some interesting behavior to it.
19 # create a dot, and mark it as current
20 fred
= self
.draw
.create_oval(event
.x
- 10, event
.y
-10, event
.x
+10, event
.y
+ 10,
21 {"fill" : "green", "tag" : "current"})
23 self
.draw
.bind(fred
, "<Any-Enter>", self
.mouseEnter
)
24 self
.draw
.bind(fred
, "<Any-Leave>", self
.mouseLeave
)
30 def mouseMove(self
, event
):
31 self
.draw
.move("current", event
.x
- self
.lastx
, event
.y
- self
.lasty
)
35 ###################################################################
36 ###### Event callbacks for canvas ITEMS (stuff drawn on the canvas)
37 ###################################################################
38 def mouseEnter(self
, event
):
39 # the "current" tag is applied to the object the cursor is over.
40 # this happens automatically.
41 self
.draw
.itemconfig("current", {"fill" : "red"})
43 def mouseLeave(self
, event
):
44 # the "current" tag is applied to the object the cursor is over.
45 # this happens automatically.
46 self
.draw
.itemconfig("current", {"fill" : "blue"})
49 def createWidgets(self
):
50 self
.QUIT
= Button(self
, {'text': 'QUIT',
52 'command': self
.quit
})
53 self
.QUIT
.pack({'side': 'left', 'fill': 'both'})
54 self
.draw
= Canvas(self
, {"width" : "5i", "height" : "5i"})
55 self
.draw
.pack({'side': 'left'})
58 Widget
.bind(self
.draw
, "<1>", self
.mouseDown
)
59 Widget
.bind(self
.draw
, "<B1-Motion>", self
.mouseMove
)
61 def __init__(self
, master
=None):
62 Frame
.__init
__(self
, master
)