| Guido van Rossum | 35820f7 | 1994-10-07 09:55:26 +0000 | [diff] [blame] | 1 | from Tkinter import * | 
|  | 2 |  | 
|  | 3 | class Test(Frame): | 
|  | 4 | def printit(self): | 
|  | 5 | print "hi" | 
|  | 6 |  | 
|  | 7 | def createWidgets(self): | 
|  | 8 | self.QUIT = Button(self, {'text': 'QUIT', | 
|  | 9 | 'bg': 'red', | 
|  | 10 | 'fg': 'white', | 
|  | 11 | 'height' : 3, | 
|  | 12 | 'command': self.quit}) | 
|  | 13 | self.QUIT.pack({'side': 'bottom', 'fill': 'both'}) | 
|  | 14 |  | 
|  | 15 | self.canvasObject = Canvas(self, {"width" : "5i", "height" : "5i"}) | 
|  | 16 | self.canvasObject.pack({'side': 'left'}) | 
|  | 17 |  | 
|  | 18 | def mouseDown(self, event): | 
|  | 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) | 
|  | 23 |  | 
|  | 24 | def mouseMotion(self, event): | 
|  | 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) | 
|  | 29 |  | 
|  | 30 | if (self.startx != event.x)  and (self.starty != event.y) : | 
|  | 31 | self.canvasObject.delete(self.rubberbandLine) | 
|  | 32 | self.rubberbandLine = self.canvasObject.create_line(self.startx, self.starty, x, y) | 
|  | 33 | # this flushes the output, making sure that | 
|  | 34 | # the rectangle makes it to the screen | 
|  | 35 | # before the next event is handled | 
|  | 36 | self.update_idletasks() | 
|  | 37 |  | 
|  | 38 | def __init__(self, master=None): | 
|  | 39 | Frame.__init__(self, master) | 
|  | 40 | Pack.config(self) | 
|  | 41 | self.createWidgets() | 
|  | 42 | # this is a "tagOrId" for the rectangle we draw on the canvas | 
|  | 43 | self.rubberbandLine = None | 
|  | 44 | Widget.bind(self.canvasObject, "<Button-1>", self.mouseDown) | 
|  | 45 | Widget.bind(self.canvasObject, "<Button1-Motion>", self.mouseMotion) | 
|  | 46 |  | 
|  | 47 |  | 
|  | 48 | test = Test() | 
|  | 49 |  | 
|  | 50 | test.mainloop() |