| Benjamin Peterson | d6d63f5 | 2009-01-04 18:53:28 +0000 | [diff] [blame] | 1 | from tkinter import * | 
| Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 2 |  | 
|  | 3 | class Test(Frame): | 
|  | 4 | def printit(self): | 
| Collin Winter | 6f2df4d | 2007-07-17 20:59:35 +0000 | [diff] [blame] | 5 | print("hi") | 
| Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 6 |  | 
|  | 7 | def createWidgets(self): | 
| Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 8 | self.QUIT = Button(self, text='QUIT', | 
|  | 9 | background='red', | 
|  | 10 | foreground='white', | 
|  | 11 | height=3, | 
|  | 12 | command=self.quit) | 
|  | 13 | self.QUIT.pack(side=BOTTOM, fill=BOTH) | 
| Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 14 |  | 
| Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 15 | self.canvasObject = Canvas(self, width="5i", height="5i") | 
|  | 16 | self.canvasObject.pack(side=LEFT) | 
| Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 17 |  | 
|  | 18 | def mouseDown(self, event): | 
| Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 19 | # canvas x and y take the screen coords from the event and translate | 
|  | 20 | # them into the coordinate system of the canvas object | 
|  | 21 | self.startx = self.canvasObject.canvasx(event.x) | 
|  | 22 | self.starty = self.canvasObject.canvasy(event.y) | 
| Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 23 |  | 
|  | 24 | def mouseMotion(self, event): | 
| Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 25 | # canvas x and y take the screen coords from the event and translate | 
|  | 26 | # them into the coordinate system of the canvas object | 
|  | 27 | x = self.canvasObject.canvasx(event.x) | 
|  | 28 | y = self.canvasObject.canvasy(event.y) | 
| Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 29 |  | 
| Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 30 | if (self.startx != event.x)  and (self.starty != event.y) : | 
|  | 31 | self.canvasObject.delete(self.rubberbandBox) | 
|  | 32 | self.rubberbandBox = self.canvasObject.create_rectangle( | 
|  | 33 | self.startx, self.starty, x, y) | 
|  | 34 | # this flushes the output, making sure that | 
|  | 35 | # the rectangle makes it to the screen | 
|  | 36 | # before the next event is handled | 
|  | 37 | self.update_idletasks() | 
| Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 38 |  | 
|  | 39 | def mouseUp(self, event): | 
| Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 40 | self.canvasObject.delete(self.rubberbandBox) | 
| Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 41 |  | 
| Tim Peters | 182b5ac | 2004-07-18 06:16:08 +0000 | [diff] [blame] | 42 | def __init__(self, master=None): | 
|  | 43 | Frame.__init__(self, master) | 
|  | 44 | Pack.config(self) | 
|  | 45 | self.createWidgets() | 
|  | 46 |  | 
|  | 47 | # this is a "tagOrId" for the rectangle we draw on the canvas | 
|  | 48 | self.rubberbandBox = None | 
|  | 49 |  | 
|  | 50 | # and the bindings that make it work.. | 
|  | 51 | Widget.bind(self.canvasObject, "<Button-1>", self.mouseDown) | 
|  | 52 | Widget.bind(self.canvasObject, "<Button1-Motion>", self.mouseMotion) | 
|  | 53 | Widget.bind(self.canvasObject, "<Button1-ButtonRelease>", self.mouseUp) | 
| Guido van Rossum | 89cb67b | 1996-07-30 18:57:18 +0000 | [diff] [blame] | 54 |  | 
| Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 55 |  | 
|  | 56 | test = Test() | 
|  | 57 |  | 
|  | 58 | test.mainloop() |