/Demo/tkinter/matt/printing-coords-of-items.py

http://unladen-swallow.googlecode.com/ · Python · 61 lines · 34 code · 10 blank · 17 comment · 1 complexity · b11aa1b253b09799258e360e1bab9ad2 MD5 · raw file

  1. from Tkinter import *
  2. # this file demonstrates the creation of widgets as part of a canvas object
  3. class Test(Frame):
  4. ###################################################################
  5. ###### Event callbacks for THE CANVAS (not the stuff drawn on it)
  6. ###################################################################
  7. def mouseDown(self, event):
  8. # see if we're inside a dot. If we are, it
  9. # gets tagged as CURRENT for free by tk.
  10. if not event.widget.find_withtag(CURRENT):
  11. # there is no dot here, so we can make one,
  12. # and bind some interesting behavior to it.
  13. # ------
  14. # create a dot, and mark it as current
  15. fred = self.draw.create_oval(
  16. event.x - 10, event.y -10, event.x +10, event.y + 10,
  17. fill="green")
  18. self.draw.tag_bind(fred, "<Enter>", self.mouseEnter)
  19. self.draw.tag_bind(fred, "<Leave>", self.mouseLeave)
  20. self.lastx = event.x
  21. self.lasty = event.y
  22. def mouseMove(self, event):
  23. self.draw.move(CURRENT, event.x - self.lastx, event.y - self.lasty)
  24. self.lastx = event.x
  25. self.lasty = event.y
  26. ###################################################################
  27. ###### Event callbacks for canvas ITEMS (stuff drawn on the canvas)
  28. ###################################################################
  29. def mouseEnter(self, event):
  30. # the "current" tag is applied to the object the cursor is over.
  31. # this happens automatically.
  32. self.draw.itemconfig(CURRENT, fill="red")
  33. print self.draw.coords(CURRENT)
  34. def mouseLeave(self, event):
  35. # the "current" tag is applied to the object the cursor is over.
  36. # this happens automatically.
  37. self.draw.itemconfig(CURRENT, fill="blue")
  38. def createWidgets(self):
  39. self.QUIT = Button(self, text='QUIT', foreground='red',
  40. command=self.quit)
  41. self.QUIT.pack(side=LEFT, fill=BOTH)
  42. self.draw = Canvas(self, width="5i", height="5i")
  43. self.draw.pack(side=LEFT)
  44. Widget.bind(self.draw, "<1>", self.mouseDown)
  45. Widget.bind(self.draw, "<B1-Motion>", self.mouseMove)
  46. def __init__(self, master=None):
  47. Frame.__init__(self, master)
  48. Pack.config(self)
  49. self.createWidgets()
  50. test = Test()
  51. test.mainloop()