| 1 |
from Tkinter import * |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 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 |
background='red', |
| 15 |
foreground='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 |
|
| 25 |
|
| 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 |
|
| 31 |
|
| 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( |
| 38 |
self.startx, self.starty, x, y) |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
self.update_idletasks() |
| 43 |
|
| 44 |
def __init__(self, master=None): |
| 45 |
Frame.__init__(self, master) |
| 46 |
Pack.config(self) |
| 47 |
self.createWidgets() |
| 48 |
|
| 49 |
|
| 50 |
self.rubberbandBox = None |
| 51 |
|
| 52 |
|
| 53 |
self.griddingSize = 50 |
| 54 |
|
| 55 |
Widget.bind(self.canvasObject, "<Button-1>", self.mouseDown) |
| 56 |
Widget.bind(self.canvasObject, "<Button1-Motion>", self.mouseMotion) |
| 57 |
|
| 58 |
|
| 59 |
test = Test() |
| 60 |
|
| 61 |
test.mainloop() |