blob: 37532740a348d1808098b6b6a850f282cebe80ec [file] [log] [blame]
Guido van Rossum18468821994-06-20 07:49:28 +00001# This module exports classes for the various canvas item types
2
Guido van Rossum5c1d1ee1996-09-06 21:09:31 +00003from Tkinter import Canvas, _cnfmerge, _flatten
Guido van Rossum0b0804b1994-06-28 13:48:26 +00004
Guido van Rossum72e31641996-05-28 23:15:20 +00005
Guido van Rossum18468821994-06-20 07:49:28 +00006class CanvasItem:
Guido van Rossumefa31431996-01-26 17:45:07 +00007 def __init__(self, canvas, itemType, *args, **kw):
Guido van Rossum18468821994-06-20 07:49:28 +00008 self.canvas = canvas
Guido van Rossumefa31431996-01-26 17:45:07 +00009 self.id = canvas._create(itemType, args, kw)
Guido van Rossum0b0804b1994-06-28 13:48:26 +000010 if not hasattr(canvas, 'items'):
11 canvas.items = {}
12 canvas.items[self.id] = self
Guido van Rossum18468821994-06-20 07:49:28 +000013 def __str__(self):
14 return str(self.id)
15 def __repr__(self):
16 return '<%s, id=%d>' % (self.__class__.__name__, self.id)
Guido van Rossum0b0804b1994-06-28 13:48:26 +000017 def delete(self):
18 del self.canvas.items[self.id]
Guido van Rossum18468821994-06-20 07:49:28 +000019 self.canvas.delete(self.id)
Guido van Rossum18468821994-06-20 07:49:28 +000020 def __getitem__(self, key):
Guido van Rossumfea128e1994-06-20 13:42:28 +000021 v = self.canvas.tk.split(self.canvas.tk.call(
22 self.canvas._w, 'itemconfigure',
Guido van Rossum0b0804b1994-06-28 13:48:26 +000023 self.id, '-' + key))
Guido van Rossum18468821994-06-20 07:49:28 +000024 return v[4]
Fred Drakee9c515a1996-09-26 20:21:26 +000025 cget = __getitem__
Guido van Rossum18468821994-06-20 07:49:28 +000026 def __setitem__(self, key, value):
Guido van Rossum72e31641996-05-28 23:15:20 +000027 self.canvas.itemconfig(self.id, {key: value})
Guido van Rossum18468821994-06-20 07:49:28 +000028 def keys(self):
29 if not hasattr(self, '_keys'):
30 self._keys = map(lambda x, tk=self.canvas.tk:
Guido van Rossumfea128e1994-06-20 13:42:28 +000031 tk.splitlist(x)[0][1:],
Guido van Rossum0b0804b1994-06-28 13:48:26 +000032 self.canvas.tk.splitlist(
33 self.canvas._do(
Guido van Rossumfea128e1994-06-20 13:42:28 +000034 'itemconfigure',
Guido van Rossum0b0804b1994-06-28 13:48:26 +000035 (self.id,))))
Guido van Rossum72e31641996-05-28 23:15:20 +000036 return self._keys
Guido van Rossum18468821994-06-20 07:49:28 +000037 def has_key(self, key):
38 return key in self.keys()
39 def addtag(self, tag, option='withtag'):
40 self.canvas.addtag(tag, option, self.id)
41 def bbox(self):
42 x1, y1, x2, y2 = self.canvas.bbox(self.id)
43 return (x1, y1), (x2, y2)
44 def bind(self, sequence=None, command=None):
Guido van Rossum421bb0e1996-07-21 02:19:32 +000045 return self.canvas.tag_bind(self.id, sequence, command)
Guido van Rossum178ff351996-12-27 15:40:31 +000046 def unbind(self, sequence):
47 self.canvas.tag_bind(self.id, sequence, '')
Guido van Rossum5c1d1ee1996-09-06 21:09:31 +000048 def config(self, cnf={}, **kw):
49 return self.canvas.itemconfig(self.id, _cnfmerge((cnf, kw)))
Guido van Rossum18468821994-06-20 07:49:28 +000050 def coords(self, pts = ()):
51 flat = ()
52 for x, y in pts: flat = flat + (x, y)
53 return apply(self.canvas.coords, (self.id,) + flat)
54 def dchars(self, first, last=None):
55 self.canvas.dchars(self.id, first, last)
56 def dtag(self, ttd):
57 self.canvas.dtag(self.id, ttd)
58 def focus(self):
59 self.canvas.focus(self.id)
60 def gettags(self):
61 return self.canvas.gettags(self.id)
Guido van Rossum6b7a7e91996-09-11 14:23:43 +000062 def icursor(self, index):
63 self.canvas.icursor(self.id, index)
64 def index(self, index):
65 return self.canvas.index(self.id, index)
Guido van Rossum18468821994-06-20 07:49:28 +000066 def insert(self, beforethis, string):
67 self.canvas.insert(self.id, beforethis, string)
68 def lower(self, belowthis=None):
69 self.canvas.lower(self.id, belowthis)
70 def move(self, xamount, yamount):
71 self.canvas.move(self.id, xamount, yamount)
Guido van Rossum72e31641996-05-28 23:15:20 +000072 def tkraise(self, abovethis=None):
73 self.canvas.tkraise(self.id, abovethis)
74 raise_ = tkraise # BW compat
Guido van Rossum18468821994-06-20 07:49:28 +000075 def scale(self, xorigin, yorigin, xscale, yscale):
76 self.canvas.scale(self.id, xorigin, yorigin, xscale, yscale)
77 def type(self):
78 return self.canvas.type(self.id)
79
80class Arc(CanvasItem):
Guido van Rossumc4570481998-03-20 20:45:49 +000081 def __init__(self, canvas, *args, **kw):
82 apply(CanvasItem.__init__, (self, canvas, 'arc') + args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +000083
84class Bitmap(CanvasItem):
Guido van Rossumc4570481998-03-20 20:45:49 +000085 def __init__(self, canvas, *args, **kw):
86 apply(CanvasItem.__init__, (self, canvas, 'bitmap') + args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +000087
Guido van Rossum72e31641996-05-28 23:15:20 +000088class ImageItem(CanvasItem):
Guido van Rossumc4570481998-03-20 20:45:49 +000089 def __init__(self, canvas, *args, **kw):
90 apply(CanvasItem.__init__, (self, canvas, 'image') + args, kw)
Guido van Rossum72e31641996-05-28 23:15:20 +000091
Guido van Rossum18468821994-06-20 07:49:28 +000092class Line(CanvasItem):
Guido van Rossumc4570481998-03-20 20:45:49 +000093 def __init__(self, canvas, *args, **kw):
94 apply(CanvasItem.__init__, (self, canvas, 'line') + args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +000095
96class Oval(CanvasItem):
Guido van Rossumc4570481998-03-20 20:45:49 +000097 def __init__(self, canvas, *args, **kw):
98 apply(CanvasItem.__init__, (self, canvas, 'oval') + args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +000099
100class Polygon(CanvasItem):
Guido van Rossumc4570481998-03-20 20:45:49 +0000101 def __init__(self, canvas, *args, **kw):
102 apply(CanvasItem.__init__, (self, canvas, 'polygon') + args,kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000103
104class Rectangle(CanvasItem):
Guido van Rossumc4570481998-03-20 20:45:49 +0000105 def __init__(self, canvas, *args, **kw):
106 apply(CanvasItem.__init__, (self, canvas, 'rectangle')+args,kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000107
Guido van Rossum0b0804b1994-06-28 13:48:26 +0000108# XXX "Text" is taken by the Text widget...
109class CanvasText(CanvasItem):
Guido van Rossumc4570481998-03-20 20:45:49 +0000110 def __init__(self, canvas, *args, **kw):
111 apply(CanvasItem.__init__, (self, canvas, 'text') + args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000112
113class Window(CanvasItem):
Guido van Rossumc4570481998-03-20 20:45:49 +0000114 def __init__(self, canvas, *args, **kw):
115 apply(CanvasItem.__init__, (self, canvas, 'window') + args, kw)
Guido van Rossum0b0804b1994-06-28 13:48:26 +0000116
117class Group:
118 def __init__(self, canvas, tag=None):
119 if not tag:
120 tag = 'Group%d' % id(self)
121 self.tag = self.id = tag
122 self.canvas = canvas
123 self.canvas.dtag(self.tag)
124 def str(self):
125 return self.tag
Guido van Rossum5c1d1ee1996-09-06 21:09:31 +0000126 __str__ = str
Guido van Rossum0b0804b1994-06-28 13:48:26 +0000127 def _do(self, cmd, *args):
128 return self.canvas._do(cmd, (self.tag,) + _flatten(args))
129 def addtag_above(self, tagOrId):
130 self._do('addtag', 'above', tagOrId)
131 def addtag_all(self):
132 self._do('addtag', 'all')
133 def addtag_below(self, tagOrId):
134 self._do('addtag', 'below', tagOrId)
135 def addtag_closest(self, x, y, halo=None, start=None):
136 self._do('addtag', 'closest', x, y, halo, start)
137 def addtag_enclosed(self, x1, y1, x2, y2):
138 self._do('addtag', 'enclosed', x1, y1, x2, y2)
139 def addtag_overlapping(self, x1, y1, x2, y2):
140 self._do('addtag', 'overlapping', x1, y1, x2, y2)
141 def addtag_withtag(self, tagOrId):
142 self._do('addtag', 'withtag', tagOrId)
143 def bbox(self):
Guido van Rossum93d1fe11998-07-16 13:43:05 +0000144 return self.canvas._getints(self._do('bbox'))
Guido van Rossum178ff351996-12-27 15:40:31 +0000145 def bind(self, sequence=None, command=None):
146 return self.canvas.tag_bind(self.id, sequence, command)
147 def unbind(self, sequence):
148 self.canvas.tag_bind(self.id, sequence, '')
Guido van Rossum0b0804b1994-06-28 13:48:26 +0000149 def coords(self, *pts):
150 return self._do('coords', pts)
151 def dchars(self, first, last=None):
152 self._do('dchars', first, last)
153 def delete(self):
154 self._do('delete')
155 def dtag(self, tagToDelete=None):
156 self._do('dtag', tagToDelete)
157 def focus(self):
158 self._do('focus')
159 def gettags(self):
160 return self.canvas.tk.splitlist(self._do('gettags', self.tag))
161 def icursor(self, index):
Guido van Rossum6b7a7e91996-09-11 14:23:43 +0000162 return self._do('icursor', index)
Guido van Rossum0b0804b1994-06-28 13:48:26 +0000163 def index(self, index):
164 return self.canvas.tk.getint(self._do('index', index))
165 def insert(self, beforeThis, string):
166 self._do('insert', beforeThis, string)
Guido van Rossum5c1d1ee1996-09-06 21:09:31 +0000167 def config(self, cnf={}, **kw):
168 return self.canvas.itemconfigure(self.tag, _cnfmerge((cnf,kw)))
Guido van Rossum0b0804b1994-06-28 13:48:26 +0000169 def lower(self, belowThis=None):
170 self._do('lower', belowThis)
171 def move(self, xAmount, yAmount):
172 self._do('move', xAmount, yAmount)
173 def tkraise(self, aboveThis=None):
174 self._do('raise', aboveThis)
175 lift = tkraise
176 def scale(self, xOrigin, yOrigin, xScale, yScale):
177 self._do('scale', xOrigin, yOrigin, xScale, yScale)
178 def select_adjust(self, index):
179 self.canvas._do('select', ('adjust', self.tag, index))
180 def select_from(self, index):
181 self.canvas._do('select', ('from', self.tag, index))
182 def select_to(self, index):
183 self.canvas._do('select', ('to', self.tag, index))
184 def type(self):
185 return self._do('type')