source: python/trunk/Demo/tkinter/matt/canvas-moving-w-mouse.py

Last change on this file was 2, checked in by Yuri Dario, 15 years ago

Initial import for vendor code.

  • Property svn:eol-style set to native
File size: 2.1 KB
Line 
1from Tkinter import *
2
3# this file demonstrates the movement of a single canvas item under mouse control
4
5class 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
13
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
19
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")
27
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")
32
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)
39
40 fred = self.draw.create_oval(0, 0, 20, 20,
41 fill="green", tags="selected")
42
43 self.draw.tag_bind(fred, "<Any-Enter>", self.mouseEnter)
44 self.draw.tag_bind(fred, "<Any-Leave>", self.mouseLeave)
45
46 Widget.bind(self.draw, "<1>", self.mouseDown)
47 Widget.bind(self.draw, "<B1-Motion>", self.mouseMove)
48
49 def __init__(self, master=None):
50 Frame.__init__(self, master)
51 Pack.config(self)
52 self.createWidgets()
53
54test = Test()
55test.mainloop()
Note: See TracBrowser for help on using the repository browser.