cpython/Lib/lib-tk/Canvas.py

186 lines
6.1 KiB
Python
Raw Normal View History

1994-06-20 04:49:28 -03:00
# This module exports classes for the various canvas item types
from Tkinter import Canvas, _cnfmerge, _flatten
1994-06-28 10:48:26 -03:00
1994-06-20 04:49:28 -03:00
class CanvasItem:
def __init__(self, canvas, itemType, *args, **kw):
1994-06-20 04:49:28 -03:00
self.canvas = canvas
self.id = canvas._create(itemType, args, kw)
1994-06-28 10:48:26 -03:00
if not hasattr(canvas, 'items'):
canvas.items = {}
canvas.items[self.id] = self
1994-06-20 04:49:28 -03:00
def __str__(self):
return str(self.id)
def __repr__(self):
return '<%s, id=%d>' % (self.__class__.__name__, self.id)
1994-06-28 10:48:26 -03:00
def delete(self):
del self.canvas.items[self.id]
1994-06-20 04:49:28 -03:00
self.canvas.delete(self.id)
def __getitem__(self, key):
1994-06-20 10:42:28 -03:00
v = self.canvas.tk.split(self.canvas.tk.call(
self.canvas._w, 'itemconfigure',
1994-06-28 10:48:26 -03:00
self.id, '-' + key))
1994-06-20 04:49:28 -03:00
return v[4]
cget = __getitem__
1994-06-20 04:49:28 -03:00
def __setitem__(self, key, value):
self.canvas.itemconfig(self.id, {key: value})
1994-06-20 04:49:28 -03:00
def keys(self):
if not hasattr(self, '_keys'):
self._keys = map(lambda x, tk=self.canvas.tk:
1994-06-20 10:42:28 -03:00
tk.splitlist(x)[0][1:],
1994-06-28 10:48:26 -03:00
self.canvas.tk.splitlist(
self.canvas._do(
1994-06-20 10:42:28 -03:00
'itemconfigure',
1994-06-28 10:48:26 -03:00
(self.id,))))
return self._keys
1994-06-20 04:49:28 -03:00
def has_key(self, key):
return key in self.keys()
def addtag(self, tag, option='withtag'):
self.canvas.addtag(tag, option, self.id)
def bbox(self):
x1, y1, x2, y2 = self.canvas.bbox(self.id)
return (x1, y1), (x2, y2)
def bind(self, sequence=None, command=None):
1996-07-20 23:19:32 -03:00
return self.canvas.tag_bind(self.id, sequence, command)
def unbind(self, sequence):
self.canvas.tag_bind(self.id, sequence, '')
def config(self, cnf={}, **kw):
return self.canvas.itemconfig(self.id, _cnfmerge((cnf, kw)))
1994-06-20 04:49:28 -03:00
def coords(self, pts = ()):
flat = ()
for x, y in pts: flat = flat + (x, y)
return apply(self.canvas.coords, (self.id,) + flat)
def dchars(self, first, last=None):
self.canvas.dchars(self.id, first, last)
def dtag(self, ttd):
self.canvas.dtag(self.id, ttd)
def focus(self):
self.canvas.focus(self.id)
def gettags(self):
return self.canvas.gettags(self.id)
def icursor(self, index):
self.canvas.icursor(self.id, index)
def index(self, index):
return self.canvas.index(self.id, index)
1994-06-20 04:49:28 -03:00
def insert(self, beforethis, string):
self.canvas.insert(self.id, beforethis, string)
def lower(self, belowthis=None):
self.canvas.lower(self.id, belowthis)
def move(self, xamount, yamount):
self.canvas.move(self.id, xamount, yamount)
def tkraise(self, abovethis=None):
self.canvas.tkraise(self.id, abovethis)
raise_ = tkraise # BW compat
1994-06-20 04:49:28 -03:00
def scale(self, xorigin, yorigin, xscale, yscale):
self.canvas.scale(self.id, xorigin, yorigin, xscale, yscale)
def type(self):
return self.canvas.type(self.id)
class Arc(CanvasItem):
1998-03-20 16:45:49 -04:00
def __init__(self, canvas, *args, **kw):
apply(CanvasItem.__init__, (self, canvas, 'arc') + args, kw)
1994-06-20 04:49:28 -03:00
class Bitmap(CanvasItem):
1998-03-20 16:45:49 -04:00
def __init__(self, canvas, *args, **kw):
apply(CanvasItem.__init__, (self, canvas, 'bitmap') + args, kw)
1994-06-20 04:49:28 -03:00
class ImageItem(CanvasItem):
1998-03-20 16:45:49 -04:00
def __init__(self, canvas, *args, **kw):
apply(CanvasItem.__init__, (self, canvas, 'image') + args, kw)
1994-06-20 04:49:28 -03:00
class Line(CanvasItem):
1998-03-20 16:45:49 -04:00
def __init__(self, canvas, *args, **kw):
apply(CanvasItem.__init__, (self, canvas, 'line') + args, kw)
1994-06-20 04:49:28 -03:00
class Oval(CanvasItem):
1998-03-20 16:45:49 -04:00
def __init__(self, canvas, *args, **kw):
apply(CanvasItem.__init__, (self, canvas, 'oval') + args, kw)
1994-06-20 04:49:28 -03:00
class Polygon(CanvasItem):
1998-03-20 16:45:49 -04:00
def __init__(self, canvas, *args, **kw):
apply(CanvasItem.__init__, (self, canvas, 'polygon') + args,kw)
1994-06-20 04:49:28 -03:00
class Rectangle(CanvasItem):
1998-03-20 16:45:49 -04:00
def __init__(self, canvas, *args, **kw):
apply(CanvasItem.__init__, (self, canvas, 'rectangle')+args,kw)
1994-06-20 04:49:28 -03:00
1994-06-28 10:48:26 -03:00
# XXX "Text" is taken by the Text widget...
class CanvasText(CanvasItem):
1998-03-20 16:45:49 -04:00
def __init__(self, canvas, *args, **kw):
apply(CanvasItem.__init__, (self, canvas, 'text') + args, kw)
1994-06-20 04:49:28 -03:00
class Window(CanvasItem):
1998-03-20 16:45:49 -04:00
def __init__(self, canvas, *args, **kw):
apply(CanvasItem.__init__, (self, canvas, 'window') + args, kw)
1994-06-28 10:48:26 -03:00
class Group:
def __init__(self, canvas, tag=None):
if not tag:
tag = 'Group%d' % id(self)
self.tag = self.id = tag
self.canvas = canvas
self.canvas.dtag(self.tag)
def str(self):
return self.tag
__str__ = str
1994-06-28 10:48:26 -03:00
def _do(self, cmd, *args):
return self.canvas._do(cmd, (self.tag,) + _flatten(args))
def addtag_above(self, tagOrId):
self._do('addtag', 'above', tagOrId)
def addtag_all(self):
self._do('addtag', 'all')
def addtag_below(self, tagOrId):
self._do('addtag', 'below', tagOrId)
def addtag_closest(self, x, y, halo=None, start=None):
self._do('addtag', 'closest', x, y, halo, start)
def addtag_enclosed(self, x1, y1, x2, y2):
self._do('addtag', 'enclosed', x1, y1, x2, y2)
def addtag_overlapping(self, x1, y1, x2, y2):
self._do('addtag', 'overlapping', x1, y1, x2, y2)
def addtag_withtag(self, tagOrId):
self._do('addtag', 'withtag', tagOrId)
def bbox(self):
return self._getints(self._do('bbox'))
def bind(self, sequence=None, command=None):
return self.canvas.tag_bind(self.id, sequence, command)
def unbind(self, sequence):
self.canvas.tag_bind(self.id, sequence, '')
1994-06-28 10:48:26 -03:00
def coords(self, *pts):
return self._do('coords', pts)
def dchars(self, first, last=None):
self._do('dchars', first, last)
def delete(self):
self._do('delete')
def dtag(self, tagToDelete=None):
self._do('dtag', tagToDelete)
def focus(self):
self._do('focus')
def gettags(self):
return self.canvas.tk.splitlist(self._do('gettags', self.tag))
def icursor(self, index):
return self._do('icursor', index)
1994-06-28 10:48:26 -03:00
def index(self, index):
return self.canvas.tk.getint(self._do('index', index))
def insert(self, beforeThis, string):
self._do('insert', beforeThis, string)
def config(self, cnf={}, **kw):
return self.canvas.itemconfigure(self.tag, _cnfmerge((cnf,kw)))
1994-06-28 10:48:26 -03:00
def lower(self, belowThis=None):
self._do('lower', belowThis)
def move(self, xAmount, yAmount):
self._do('move', xAmount, yAmount)
def tkraise(self, aboveThis=None):
self._do('raise', aboveThis)
lift = tkraise
def scale(self, xOrigin, yOrigin, xScale, yScale):
self._do('scale', xOrigin, yOrigin, xScale, yScale)
def select_adjust(self, index):
self.canvas._do('select', ('adjust', self.tag, index))
def select_from(self, index):
self.canvas._do('select', ('from', self.tag, index))
def select_to(self, index):
self.canvas._do('select', ('to', self.tag, index))
def type(self):
return self._do('type')