Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame^] | 1 | from Tkinter import * |
| 2 | |
| 3 | # this is the same as simple-demo-1.py, but uses |
| 4 | # subclassing. |
| 5 | # note that there is no explicit call to start Tk. |
| 6 | # Tkinter is smart enough to start the system if it's not already going. |
| 7 | |
| 8 | class Test(Frame): |
| 9 | def printit(self): |
| 10 | print "hi" |
| 11 | |
| 12 | def createWidgets(self): |
| 13 | self.QUIT = Button(self, {'text': 'QUIT', |
| 14 | 'bg': 'red', |
| 15 | 'fg': 'white', |
| 16 | 'height' : 3, |
| 17 | 'command': self.quit}) |
| 18 | self.QUIT.pack({'side': 'bottom', 'fill': 'both'}) |
| 19 | |
| 20 | self.canvasObject = Canvas(self, {"width" : "5i", "height" : "5i"}) |
| 21 | self.canvasObject.pack({'side': 'left'}) |
| 22 | |
| 23 | def mouseDown(self, event): |
| 24 | # canvas x and y take the screen coords from the event and translate |
| 25 | # them into the coordinate system of the canvas object |
| 26 | self.startx = self.canvasObject.canvasx(event.x, self.griddingSize) |
| 27 | self.starty = self.canvasObject.canvasy(event.y, self.griddingSize) |
| 28 | |
| 29 | def mouseMotion(self, event): |
| 30 | # canvas x and y take the screen coords from the event and translate |
| 31 | # them into the coordinate system of the canvas object |
| 32 | x = self.canvasObject.canvasx(event.x, self.griddingsize) |
| 33 | y = self.canvasObject.canvasy(event.y, self.griddingsize) |
| 34 | |
| 35 | if (self.startx != event.x) and (self.starty != event.y) : |
| 36 | self.canvasObject.delete(self.rubberbandBox) |
| 37 | self.rubberbandBox = self.canvasObject.create_rectangle(self.startx, self.starty, x, y) |
| 38 | # this flushes the output, making sure that |
| 39 | # the rectangle makes it to the screen |
| 40 | # before the next event is handled |
| 41 | self.update_idletasks() |
| 42 | |
| 43 | def __init__(self, master=None): |
| 44 | Frame.__init__(self, master) |
| 45 | Pack.config(self) |
| 46 | self.createWidgets() |
| 47 | |
| 48 | # this is a "tagOrId" for the rectangle we draw on the canvas |
| 49 | self.rubberbandBox = None |
| 50 | |
| 51 | # this is the size of the gridding squares |
| 52 | self.griddingSize = 50 |
| 53 | |
| 54 | Widget.bind(self.canvasObject, "<Button-1>", self.mouseDown) |
| 55 | Widget.bind(self.canvasObject, "<Button1-Motion>", self.mouseMotion) |
| 56 | |
| 57 | |
| 58 | test = Test() |
| 59 | |
| 60 | test.mainloop() |