blob: b1c8e78c60c958cfee52256d6683ab24ef4063d2 [file] [log] [blame]
Guido van Rossum35820f71994-10-07 09:55:26 +00001from Tkinter import *
2
3class Test(Frame):
4 def printit(self):
Collin Winter6f2df4d2007-07-17 20:59:35 +00005 print("hi")
Guido van Rossum35820f71994-10-07 09:55:26 +00006
7 def createWidgets(self):
Tim Peters182b5ac2004-07-18 06:16:08 +00008 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 Rossum35820f71994-10-07 09:55:26 +000014
Tim Peters182b5ac2004-07-18 06:16:08 +000015 self.canvasObject = Canvas(self, width="5i", height="5i")
16 self.canvasObject.pack(side=LEFT)
Guido van Rossum35820f71994-10-07 09:55:26 +000017
18 def mouseDown(self, event):
Tim Peters182b5ac2004-07-18 06:16:08 +000019 # 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 Rossum35820f71994-10-07 09:55:26 +000023
24 def mouseMotion(self, event):
Tim Peters182b5ac2004-07-18 06:16:08 +000025 # 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 Rossum35820f71994-10-07 09:55:26 +000029
Tim Peters182b5ac2004-07-18 06:16:08 +000030 if (self.startx != event.x) and (self.starty != event.y) :
31 self.canvasObject.delete(self.rubberbandLine)
32 self.rubberbandLine = self.canvasObject.create_line(
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 Rossum35820f71994-10-07 09:55:26 +000038
39 def __init__(self, master=None):
Tim Peters182b5ac2004-07-18 06:16:08 +000040 Frame.__init__(self, master)
41 Pack.config(self)
42 self.createWidgets()
43 # this is a "tagOrId" for the rectangle we draw on the canvas
44 self.rubberbandLine = None
45 Widget.bind(self.canvasObject, "<Button-1>", self.mouseDown)
46 Widget.bind(self.canvasObject, "<Button1-Motion>", self.mouseMotion)
47
Guido van Rossum35820f71994-10-07 09:55:26 +000048
49test = Test()
50
51test.mainloop()