/Demo/tkinter/guido/canvasevents.py

http://unladen-swallow.googlecode.com/ · Python · 244 lines · 232 code · 6 blank · 6 comment · 0 complexity · bd7a42b8ac4fb40ca6f302ca390b5b42 MD5 · raw file

  1. #! /usr/bin/env python
  2. from Tkinter import *
  3. from Canvas import Oval, Group, CanvasText
  4. # Fix a bug in Canvas.Group as distributed in Python 1.4. The
  5. # distributed bind() method is broken. This is what should be used:
  6. class Group(Group):
  7. def bind(self, sequence=None, command=None):
  8. return self.canvas.tag_bind(self.id, sequence, command)
  9. class Object:
  10. """Base class for composite graphical objects.
  11. Objects belong to a canvas, and can be moved around on the canvas.
  12. They also belong to at most one ``pile'' of objects, and can be
  13. transferred between piles (or removed from their pile).
  14. Objects have a canonical ``x, y'' position which is moved when the
  15. object is moved. Where the object is relative to this position
  16. depends on the object; for simple objects, it may be their center.
  17. Objects have mouse sensitivity. They can be clicked, dragged and
  18. double-clicked. The behavior may actually determined by the pile
  19. they are in.
  20. All instance attributes are public since the derived class may
  21. need them.
  22. """
  23. def __init__(self, canvas, x=0, y=0, fill='red', text='object'):
  24. self.canvas = canvas
  25. self.x = x
  26. self.y = y
  27. self.pile = None
  28. self.group = Group(self.canvas)
  29. self.createitems(fill, text)
  30. def __str__(self):
  31. return str(self.group)
  32. def createitems(self, fill, text):
  33. self.__oval = Oval(self.canvas,
  34. self.x-20, self.y-10, self.x+20, self.y+10,
  35. fill=fill, width=3)
  36. self.group.addtag_withtag(self.__oval)
  37. self.__text = CanvasText(self.canvas,
  38. self.x, self.y, text=text)
  39. self.group.addtag_withtag(self.__text)
  40. def moveby(self, dx, dy):
  41. if dx == dy == 0:
  42. return
  43. self.group.move(dx, dy)
  44. self.x = self.x + dx
  45. self.y = self.y + dy
  46. def moveto(self, x, y):
  47. self.moveby(x - self.x, y - self.y)
  48. def transfer(self, pile):
  49. if self.pile:
  50. self.pile.delete(self)
  51. self.pile = None
  52. self.pile = pile
  53. if self.pile:
  54. self.pile.add(self)
  55. def tkraise(self):
  56. self.group.tkraise()
  57. class Bottom(Object):
  58. """An object to serve as the bottom of a pile."""
  59. def createitems(self, *args):
  60. self.__oval = Oval(self.canvas,
  61. self.x-20, self.y-10, self.x+20, self.y+10,
  62. fill='gray', outline='')
  63. self.group.addtag_withtag(self.__oval)
  64. class Pile:
  65. """A group of graphical objects."""
  66. def __init__(self, canvas, x, y, tag=None):
  67. self.canvas = canvas
  68. self.x = x
  69. self.y = y
  70. self.objects = []
  71. self.bottom = Bottom(self.canvas, self.x, self.y)
  72. self.group = Group(self.canvas, tag=tag)
  73. self.group.addtag_withtag(self.bottom.group)
  74. self.bindhandlers()
  75. def bindhandlers(self):
  76. self.group.bind('<1>', self.clickhandler)
  77. self.group.bind('<Double-1>', self.doubleclickhandler)
  78. def add(self, object):
  79. self.objects.append(object)
  80. self.group.addtag_withtag(object.group)
  81. self.position(object)
  82. def delete(self, object):
  83. object.group.dtag(self.group)
  84. self.objects.remove(object)
  85. def position(self, object):
  86. object.tkraise()
  87. i = self.objects.index(object)
  88. object.moveto(self.x + i*4, self.y + i*8)
  89. def clickhandler(self, event):
  90. pass
  91. def doubleclickhandler(self, event):
  92. pass
  93. class MovingPile(Pile):
  94. def bindhandlers(self):
  95. Pile.bindhandlers(self)
  96. self.group.bind('<B1-Motion>', self.motionhandler)
  97. self.group.bind('<ButtonRelease-1>', self.releasehandler)
  98. movethis = None
  99. def clickhandler(self, event):
  100. tags = self.canvas.gettags('current')
  101. for i in range(len(self.objects)):
  102. o = self.objects[i]
  103. if o.group.tag in tags:
  104. break
  105. else:
  106. self.movethis = None
  107. return
  108. self.movethis = self.objects[i:]
  109. for o in self.movethis:
  110. o.tkraise()
  111. self.lastx = event.x
  112. self.lasty = event.y
  113. doubleclickhandler = clickhandler
  114. def motionhandler(self, event):
  115. if not self.movethis:
  116. return
  117. dx = event.x - self.lastx
  118. dy = event.y - self.lasty
  119. self.lastx = event.x
  120. self.lasty = event.y
  121. for o in self.movethis:
  122. o.moveby(dx, dy)
  123. def releasehandler(self, event):
  124. objects = self.movethis
  125. if not objects:
  126. return
  127. self.movethis = None
  128. self.finishmove(objects)
  129. def finishmove(self, objects):
  130. for o in objects:
  131. self.position(o)
  132. class Pile1(MovingPile):
  133. x = 50
  134. y = 50
  135. tag = 'p1'
  136. def __init__(self, demo):
  137. self.demo = demo
  138. MovingPile.__init__(self, self.demo.canvas, self.x, self.y, self.tag)
  139. def doubleclickhandler(self, event):
  140. try:
  141. o = self.objects[-1]
  142. except IndexError:
  143. return
  144. o.transfer(self.other())
  145. MovingPile.doubleclickhandler(self, event)
  146. def other(self):
  147. return self.demo.p2
  148. def finishmove(self, objects):
  149. o = objects[0]
  150. p = self.other()
  151. x, y = o.x, o.y
  152. if (x-p.x)**2 + (y-p.y)**2 < (x-self.x)**2 + (y-self.y)**2:
  153. for o in objects:
  154. o.transfer(p)
  155. else:
  156. MovingPile.finishmove(self, objects)
  157. class Pile2(Pile1):
  158. x = 150
  159. y = 50
  160. tag = 'p2'
  161. def other(self):
  162. return self.demo.p1
  163. class Demo:
  164. def __init__(self, master):
  165. self.master = master
  166. self.canvas = Canvas(master,
  167. width=200, height=200,
  168. background='yellow',
  169. relief=SUNKEN, borderwidth=2)
  170. self.canvas.pack(expand=1, fill=BOTH)
  171. self.p1 = Pile1(self)
  172. self.p2 = Pile2(self)
  173. o1 = Object(self.canvas, fill='red', text='o1')
  174. o2 = Object(self.canvas, fill='green', text='o2')
  175. o3 = Object(self.canvas, fill='light blue', text='o3')
  176. o1.transfer(self.p1)
  177. o2.transfer(self.p1)
  178. o3.transfer(self.p2)
  179. # Main function, run when invoked as a stand-alone Python program.
  180. def main():
  181. root = Tk()
  182. demo = Demo(root)
  183. root.protocol('WM_DELETE_WINDOW', root.quit)
  184. root.mainloop()
  185. if __name__ == '__main__':
  186. main()