PyXR

c:\python24\lib\lib-tk \ Canvas.py



0001 # This module exports classes for the various canvas item types
0002 
0003 # NOTE: This module was an experiment and is now obsolete.
0004 # It's best to use the Tkinter.Canvas class directly.
0005 
0006 from Tkinter import Canvas, _cnfmerge, _flatten
0007 
0008 
0009 class CanvasItem:
0010     def __init__(self, canvas, itemType, *args, **kw):
0011         self.canvas = canvas
0012         self.id = canvas._create(itemType, args, kw)
0013         if not hasattr(canvas, 'items'):
0014             canvas.items = {}
0015         canvas.items[self.id] = self
0016     def __str__(self):
0017         return str(self.id)
0018     def __repr__(self):
0019         return '<%s, id=%d>' % (self.__class__.__name__, self.id)
0020     def delete(self):
0021         del self.canvas.items[self.id]
0022         self.canvas.delete(self.id)
0023     def __getitem__(self, key):
0024         v = self.canvas.tk.split(self.canvas.tk.call(
0025                 self.canvas._w, 'itemconfigure',
0026                 self.id, '-' + key))
0027         return v[4]
0028     cget = __getitem__
0029     def __setitem__(self, key, value):
0030         self.canvas.itemconfig(self.id, {key: value})
0031     def keys(self):
0032         if not hasattr(self, '_keys'):
0033             self._keys = map(lambda x, tk=self.canvas.tk:
0034                              tk.splitlist(x)[0][1:],
0035                              self.canvas.tk.splitlist(
0036                                      self.canvas._do(
0037                                              'itemconfigure',
0038                                              (self.id,))))
0039         return self._keys
0040     def has_key(self, key):
0041         return key in self.keys()
0042     def __contains__(self, key):
0043         return key in self.keys()
0044     def addtag(self, tag, option='withtag'):
0045         self.canvas.addtag(tag, option, self.id)
0046     def bbox(self):
0047         x1, y1, x2, y2 = self.canvas.bbox(self.id)
0048         return (x1, y1), (x2, y2)
0049     def bind(self, sequence=None, command=None, add=None):
0050         return self.canvas.tag_bind(self.id, sequence, command, add)
0051     def unbind(self, sequence, funcid=None):
0052         self.canvas.tag_unbind(self.id, sequence, funcid)
0053     def config(self, cnf={}, **kw):
0054         return self.canvas.itemconfig(self.id, _cnfmerge((cnf, kw)))
0055     def coords(self, pts = ()):
0056         flat = ()
0057         for x, y in pts: flat = flat + (x, y)
0058         return self.canvas.coords(self.id, *flat)
0059     def dchars(self, first, last=None):
0060         self.canvas.dchars(self.id, first, last)
0061     def dtag(self, ttd):
0062         self.canvas.dtag(self.id, ttd)
0063     def focus(self):
0064         self.canvas.focus(self.id)
0065     def gettags(self):
0066         return self.canvas.gettags(self.id)
0067     def icursor(self, index):
0068         self.canvas.icursor(self.id, index)
0069     def index(self, index):
0070         return self.canvas.index(self.id, index)
0071     def insert(self, beforethis, string):
0072         self.canvas.insert(self.id, beforethis, string)
0073     def lower(self, belowthis=None):
0074         self.canvas.tag_lower(self.id, belowthis)
0075     def move(self, xamount, yamount):
0076         self.canvas.move(self.id, xamount, yamount)
0077     def tkraise(self, abovethis=None):
0078         self.canvas.tag_raise(self.id, abovethis)
0079     raise_ = tkraise # BW compat
0080     def scale(self, xorigin, yorigin, xscale, yscale):
0081         self.canvas.scale(self.id, xorigin, yorigin, xscale, yscale)
0082     def type(self):
0083         return self.canvas.type(self.id)
0084 
0085 class Arc(CanvasItem):
0086     def __init__(self, canvas, *args, **kw):
0087         CanvasItem.__init__(self, canvas, 'arc', *args, **kw)
0088 
0089 class Bitmap(CanvasItem):
0090     def __init__(self, canvas, *args, **kw):
0091         CanvasItem.__init__(self, canvas, 'bitmap', *args, **kw)
0092 
0093 class ImageItem(CanvasItem):
0094     def __init__(self, canvas, *args, **kw):
0095         CanvasItem.__init__(self, canvas, 'image', *args, **kw)
0096 
0097 class Line(CanvasItem):
0098     def __init__(self, canvas, *args, **kw):
0099         CanvasItem.__init__(self, canvas, 'line', *args, **kw)
0100 
0101 class Oval(CanvasItem):
0102     def __init__(self, canvas, *args, **kw):
0103         CanvasItem.__init__(self, canvas, 'oval', *args, **kw)
0104 
0105 class Polygon(CanvasItem):
0106     def __init__(self, canvas, *args, **kw):
0107         CanvasItem.__init__(self, canvas, 'polygon', *args, **kw)
0108 
0109 class Rectangle(CanvasItem):
0110     def __init__(self, canvas, *args, **kw):
0111         CanvasItem.__init__(self, canvas, 'rectangle', *args, **kw)
0112 
0113 # XXX "Text" is taken by the Text widget...
0114 class CanvasText(CanvasItem):
0115     def __init__(self, canvas, *args, **kw):
0116         CanvasItem.__init__(self, canvas, 'text', *args, **kw)
0117 
0118 class Window(CanvasItem):
0119     def __init__(self, canvas, *args, **kw):
0120         CanvasItem.__init__(self, canvas, 'window', *args, **kw)
0121 
0122 class Group:
0123     def __init__(self, canvas, tag=None):
0124         if not tag:
0125             tag = 'Group%d' % id(self)
0126         self.tag = self.id = tag
0127         self.canvas = canvas
0128         self.canvas.dtag(self.tag)
0129     def str(self):
0130         return self.tag
0131     __str__ = str
0132     def _do(self, cmd, *args):
0133         return self.canvas._do(cmd, (self.tag,) + _flatten(args))
0134     def addtag_above(self, tagOrId):
0135         self._do('addtag', 'above', tagOrId)
0136     def addtag_all(self):
0137         self._do('addtag', 'all')
0138     def addtag_below(self, tagOrId):
0139         self._do('addtag', 'below', tagOrId)
0140     def addtag_closest(self, x, y, halo=None, start=None):
0141         self._do('addtag', 'closest', x, y, halo, start)
0142     def addtag_enclosed(self, x1, y1, x2, y2):
0143         self._do('addtag', 'enclosed', x1, y1, x2, y2)
0144     def addtag_overlapping(self, x1, y1, x2, y2):
0145         self._do('addtag', 'overlapping', x1, y1, x2, y2)
0146     def addtag_withtag(self, tagOrId):
0147         self._do('addtag', 'withtag', tagOrId)
0148     def bbox(self):
0149         return self.canvas._getints(self._do('bbox'))
0150     def bind(self, sequence=None, command=None, add=None):
0151         return self.canvas.tag_bind(self.id, sequence, command, add)
0152     def unbind(self, sequence, funcid=None):
0153         self.canvas.tag_unbind(self.id, sequence, funcid)
0154     def coords(self, *pts):
0155         return self._do('coords', pts)
0156     def dchars(self, first, last=None):
0157         self._do('dchars', first, last)
0158     def delete(self):
0159         self._do('delete')
0160     def dtag(self, tagToDelete=None):
0161         self._do('dtag', tagToDelete)
0162     def focus(self):
0163         self._do('focus')
0164     def gettags(self):
0165         return self.canvas.tk.splitlist(self._do('gettags', self.tag))
0166     def icursor(self, index):
0167         return self._do('icursor', index)
0168     def index(self, index):
0169         return self.canvas.tk.getint(self._do('index', index))
0170     def insert(self, beforeThis, string):
0171         self._do('insert', beforeThis, string)
0172     def config(self, cnf={}, **kw):
0173         return self.canvas.itemconfigure(self.tag, _cnfmerge((cnf,kw)))
0174     def lower(self, belowThis=None):
0175         self._do('lower', belowThis)
0176     def move(self, xAmount, yAmount):
0177         self._do('move', xAmount, yAmount)
0178     def tkraise(self, aboveThis=None):
0179         self._do('raise', aboveThis)
0180     lift = tkraise
0181     def scale(self, xOrigin, yOrigin, xScale, yScale):
0182         self._do('scale', xOrigin, yOrigin, xScale, yScale)
0183     def select_adjust(self, index):
0184         self.canvas._do('select', ('adjust', self.tag, index))
0185     def select_from(self, index):
0186         self.canvas._do('select', ('from', self.tag, index))
0187     def select_to(self, index):
0188         self.canvas._do('select', ('to', self.tag, index))
0189     def type(self):
0190         return self._do('type')
0191 

Generated by PyXR 0.9.4
SourceForge.net Logo