3 # this file demonstrates the creation of widgets as part of a canvas object
6 ###################################################################
7 ###### Event callbacks for THE CANVAS (not the stuff drawn on it)
8 ###################################################################
9 def mouseDown(self
, event
):
10 # see if we're inside a dot. If we are, it
11 # gets tagged as "current" for free by tk.
13 if not event
.widget
.find_withtag("current"):
14 # there is no dot here, so we can make one,
15 # and bind some interesting behavior to it.
18 # create a dot, and mark it as current
19 fred
= self
.draw
.create_oval(event
.x
- 10, event
.y
-10, event
.x
+10, event
.y
+ 10,
20 {"fill" : "green", "tag" : "current"})
22 self
.draw
.bind(fred
, "<Any-Enter>", self
.mouseEnter
)
23 self
.draw
.bind(fred
, "<Any-Leave>", self
.mouseLeave
)
29 def mouseMove(self
, event
):
30 self
.draw
.move("current", event
.x
- self
.lastx
, event
.y
- self
.lasty
)
34 ###################################################################
35 ###### Event callbacks for canvas ITEMS (stuff drawn on the canvas)
36 ###################################################################
37 def mouseEnter(self
, event
):
38 # the "current" tag is applied to the object the cursor is over.
39 # this happens automatically.
40 self
.draw
.itemconfig("current", {"fill" : "red"})
41 print self
.tk
.splitlist(self
.draw
.coords("current"))
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"})
48 def createWidgets(self
):
49 self
.QUIT
= Button(self
, {'text': 'QUIT',
51 'command': self
.quit
})
52 self
.QUIT
.pack({'side': 'left', 'fill': 'both'})
53 self
.draw
= Canvas(self
, {"width" : "5i", "height" : "5i"})
54 self
.draw
.pack({'side': 'left'})
57 Widget
.bind(self
.draw
, "<1>", self
.mouseDown
)
58 Widget
.bind(self
.draw
, "<B1-Motion>", self
.mouseMove
)
60 def __init__(self
, master
=None):
61 Frame
.__init
__(self
, master
)