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.
17 # create a dot, and mark it as current
18 fred
= self
.draw
.create_oval(
19 event
.x
- 10, event
.y
-10, event
.x
+10, event
.y
+ 10,
21 self
.draw
.tag_bind(fred
, "<Enter>", self
.mouseEnter
)
22 self
.draw
.tag_bind(fred
, "<Leave>", self
.mouseLeave
)
26 def mouseMove(self
, event
):
27 self
.draw
.move(CURRENT
, event
.x
- self
.lastx
, event
.y
- self
.lasty
)
31 ###################################################################
32 ###### Event callbacks for canvas ITEMS (stuff drawn on the canvas)
33 ###################################################################
34 def mouseEnter(self
, event
):
35 # the "current" tag is applied to the object the cursor is over.
36 # this happens automatically.
37 self
.draw
.itemconfig(CURRENT
, fill
="red")
38 print self
.draw
.coords(CURRENT
)
40 def mouseLeave(self
, event
):
41 # the "current" tag is applied to the object the cursor is over.
42 # this happens automatically.
43 self
.draw
.itemconfig(CURRENT
, fill
="blue")
45 def createWidgets(self
):
46 self
.QUIT
= Button(self
, text
='QUIT', foreground
='red',
48 self
.QUIT
.pack(side
=LEFT
, fill
=BOTH
)
49 self
.draw
= Canvas(self
, width
="5i", height
="5i")
50 self
.draw
.pack(side
=LEFT
)
52 Widget
.bind(self
.draw
, "<1>", self
.mouseDown
)
53 Widget
.bind(self
.draw
, "<B1-Motion>", self
.mouseMove
)
55 def __init__(self
, master
=None):
56 Frame
.__init
__(self
, master
)