#! /usr/bin/python2.7
from Tkinter import *
from Canvas import Oval, Group, CanvasText
# Fix a bug in Canvas.Group as distributed in Python 1.4. The
# distributed bind() method is broken. This is what should be used:
class Group(Group):
def bind(self, sequence=None, command=None):
return self.canvas.tag_bind(self.id, sequence, command)
class Object:
"""Base class for composite graphical objects.
Objects belong to a canvas, and can be moved around on the canvas.
They also belong to at most one ``pile'' of objects, and can be
transferred between piles (or removed from their pile).
Objects have a canonical ``x, y'' position which is moved when the
object is moved. Where the object is relative to this position
depends on the object; for simple objects, it may be their center.
Objects have mouse sensitivity. They can be clicked, dragged and
double-clicked. The behavior may actually be determined by the pile
they are in.
All instance attributes are public since the derived class may
need them.
"""
def __init__(self, canvas, x=0, y=0, fill='red', text='object'):
self.canvas = canvas
self.x = x
self.y = y
self.pile = None
self.group = Group(self.canvas)
self.createitems(fill, text)
def __str__(self):
return str(self.group)
def createitems(self, fill, text):
self.__oval = Oval(self.canvas,
self.x-20, self.y-10, self.x+20, self.y+10,
fill=fill, width=3)
self.group.addtag_withtag(self.__oval)
self.__text = CanvasText(self.canvas,
self.x, self.y, text=text)
self.group.addtag_withtag(self.__text)
def moveby(self, dx, dy):
if dx == dy == 0:
return
self.group.move(dx, dy)
self.x = self.x + dx
self.y = self.y + dy
def moveto(self, x, y):
self.moveby(x - self.x, y - self.y)
def transfer(self, pile):
if self.pile:
self.pile.delete(self)
self.pile = None
self.pile = pile
if self.pile:
self.pile.add(self)
def tkraise(self):
self.group.tkraise()
class Bottom(Object):
"""An object to serve as the bottom of a pile."""
def createitems(self, *args):
self.__oval = Oval(self.canvas,
self.x-20, self.y-10, self.x+20, self.y+10,
fill='gray', outline='')
self.group.addtag_withtag(self.__oval)
class Pile:
"""A group of graphical objects."""
def __init__(self, canvas, x, y, tag=None):
self.canvas = canvas
self.x = x
self.y = y
self.objects = []
self.bottom = Bottom(self.canvas, self.x, self.y)
self.group = Group(self.canvas, tag=tag)
self.group.addtag_withtag(self.bottom.group)
self.bindhandlers()
def bindhandlers(self):
self.group.bind('<1>', self.clickhandler)
self.group.bind('