Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 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): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 10 | # see if we're inside a dot. If we are, it |
| 11 | # gets tagged as CURRENT for free by tk. |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 12 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 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 |
Guido van Rossum | 89cb67b | 1996-07-30 18:57:18 +0000 | [diff] [blame] | 25 | |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 26 | def mouseMove(self, event): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 27 | self.draw.move(CURRENT, event.x - self.lastx, event.y - self.lasty) |
| 28 | self.lastx = event.x |
| 29 | self.lasty = event.y |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 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. |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 36 | # this happens automatically. |
| 37 | self.draw.itemconfig(CURRENT, fill="red") |
Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame^] | 38 | print(self.draw.coords(CURRENT)) |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 39 | |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 40 | def mouseLeave(self, event): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 41 | # the "current" tag is applied to the object the cursor is over. |
| 42 | # this happens automatically. |
| 43 | self.draw.itemconfig(CURRENT, fill="blue") |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 44 | |
| 45 | def createWidgets(self): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 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) |
Guido van Rossum | 89cb67b | 1996-07-30 18:57:18 +0000 | [diff] [blame] | 51 | |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 52 | Widget.bind(self.draw, "<1>", self.mouseDown) |
| 53 | Widget.bind(self.draw, "<B1-Motion>", self.mouseMove) |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 54 | |
| 55 | def __init__(self, master=None): |
Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 56 | Frame.__init__(self, master) |
| 57 | Pack.config(self) |
| 58 | self.createWidgets() |
Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 59 | |
| 60 | test = Test() |
| 61 | test.mainloop() |