|
1 from Tkinter import * |
|
2 |
|
3 # this file demonstrates the creation of widgets as part of a canvas object |
|
4 |
|
5 class Test(Frame): |
|
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. |
|
12 |
|
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. |
|
16 # ------ |
|
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, |
|
20 fill="green") |
|
21 self.draw.tag_bind(fred, "<Enter>", self.mouseEnter) |
|
22 self.draw.tag_bind(fred, "<Leave>", self.mouseLeave) |
|
23 self.lastx = event.x |
|
24 self.lasty = event.y |
|
25 |
|
26 def mouseMove(self, event): |
|
27 self.draw.move(CURRENT, event.x - self.lastx, event.y - self.lasty) |
|
28 self.lastx = event.x |
|
29 self.lasty = event.y |
|
30 |
|
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) |
|
39 |
|
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") |
|
44 |
|
45 def createWidgets(self): |
|
46 self.QUIT = Button(self, text='QUIT', foreground='red', |
|
47 command=self.quit) |
|
48 self.QUIT.pack(side=LEFT, fill=BOTH) |
|
49 self.draw = Canvas(self, width="5i", height="5i") |
|
50 self.draw.pack(side=LEFT) |
|
51 |
|
52 Widget.bind(self.draw, "<1>", self.mouseDown) |
|
53 Widget.bind(self.draw, "<B1-Motion>", self.mouseMove) |
|
54 |
|
55 def __init__(self, master=None): |
|
56 Frame.__init__(self, master) |
|
57 Pack.config(self) |
|
58 self.createWidgets() |
|
59 |
|
60 test = Test() |
|
61 test.mainloop() |