Add better error reporting for MemoryErrors caused by str->float conversions.
[python.git] / Demo / tkinter / matt / canvas-moving-w-mouse.py
blob81785d86a82d561d9b58d5e14fd6c5b33e667149
1 from Tkinter import *
3 # this file demonstrates the movement of a single canvas item under mouse control
5 class Test(Frame):
6 ###################################################################
7 ###### Event callbacks for THE CANVAS (not the stuff drawn on it)
8 ###################################################################
9 def mouseDown(self, event):
10 # remember where the mouse went down
11 self.lastx = event.x
12 self.lasty = event.y
14 def mouseMove(self, event):
15 # whatever the mouse is over gets tagged as CURRENT for free by tk.
16 self.draw.move(CURRENT, event.x - self.lastx, event.y - self.lasty)
17 self.lastx = event.x
18 self.lasty = event.y
20 ###################################################################
21 ###### Event callbacks for canvas ITEMS (stuff drawn on the canvas)
22 ###################################################################
23 def mouseEnter(self, event):
24 # the CURRENT tag is applied to the object the cursor is over.
25 # this happens automatically.
26 self.draw.itemconfig(CURRENT, fill="red")
28 def mouseLeave(self, event):
29 # the CURRENT tag is applied to the object the cursor is over.
30 # this happens automatically.
31 self.draw.itemconfig(CURRENT, fill="blue")
33 def createWidgets(self):
34 self.QUIT = Button(self, text='QUIT', foreground='red',
35 command=self.quit)
36 self.QUIT.pack(side=LEFT, fill=BOTH)
37 self.draw = Canvas(self, width="5i", height="5i")
38 self.draw.pack(side=LEFT)
40 fred = self.draw.create_oval(0, 0, 20, 20,
41 fill="green", tags="selected")
43 self.draw.tag_bind(fred, "<Any-Enter>", self.mouseEnter)
44 self.draw.tag_bind(fred, "<Any-Leave>", self.mouseLeave)
46 Widget.bind(self.draw, "<1>", self.mouseDown)
47 Widget.bind(self.draw, "<B1-Motion>", self.mouseMove)
49 def __init__(self, master=None):
50 Frame.__init__(self, master)
51 Pack.config(self)
52 self.createWidgets()
54 test = Test()
55 test.mainloop()