changes by Barry, e.g. font lock & email addresses
[python/dscho.git] / Demo / tkinter / matt / printing-coords-of-items.py
blob2440378d80977fa4b987420bebadffee908eaefa
1 from Tkinter import *
3 # this file demonstrates the creation of widgets as part of a canvas object
5 class Test(Frame):
6 ###################################################################
7 ###### Event callbacks for THE CANVAS (not the stuff drawn on it)
8 ###################################################################
9 def mouseDown(self, event):
10 # see if we're inside a dot. If we are, it
11 # gets tagged as "current" for free by tk.
13 if not event.widget.find_withtag("current"):
14 # there is no dot here, so we can make one,
15 # and bind some interesting behavior to it.
16 # ------
18 # create a dot, and mark it as current
19 fred = self.draw.create_oval(event.x - 10, event.y -10, event.x +10, event.y + 10,
20 {"fill" : "green", "tag" : "current"})
22 self.draw.bind(fred, "<Any-Enter>", self.mouseEnter)
23 self.draw.bind(fred, "<Any-Leave>", self.mouseLeave)
25 self.lastx = event.x
26 self.lasty = event.y
29 def mouseMove(self, event):
30 self.draw.move("current", event.x - self.lastx, event.y - self.lasty)
31 self.lastx = event.x
32 self.lasty = event.y
34 ###################################################################
35 ###### Event callbacks for canvas ITEMS (stuff drawn on the canvas)
36 ###################################################################
37 def mouseEnter(self, event):
38 # the "current" tag is applied to the object the cursor is over.
39 # this happens automatically.
40 self.draw.itemconfig("current", {"fill" : "red"})
41 print self.tk.splitlist(self.draw.coords("current"))
43 def mouseLeave(self, event):
44 # the "current" tag is applied to the object the cursor is over.
45 # this happens automatically.
46 self.draw.itemconfig("current", {"fill" : "blue"})
48 def createWidgets(self):
49 self.QUIT = Button(self, {'text': 'QUIT',
50 'fg': 'red',
51 'command': self.quit})
52 self.QUIT.pack({'side': 'left', 'fill': 'both'})
53 self.draw = Canvas(self, {"width" : "5i", "height" : "5i"})
54 self.draw.pack({'side': 'left'})
57 Widget.bind(self.draw, "<1>", self.mouseDown)
58 Widget.bind(self.draw, "<B1-Motion>", self.mouseMove)
60 def __init__(self, master=None):
61 Frame.__init__(self, master)
62 Pack.config(self)
63 self.createWidgets()
65 test = Test()
66 test.mainloop()