blob: ed4ba348cc49163e75e24058b2af90d6bd817875 [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 Rossum00236f32000-10-06 00:38:51 +00003# NOTE: This module was an experiment and is now obsolete.
4# It's best to use the Tkinter.Canvas class directly.
5
Guido van Rossum5c1d1ee1996-09-06 21:09:31 +00006from Tkinter import Canvas, _cnfmerge, _flatten
Guido van Rossum0b0804b1994-06-28 13:48:26 +00007
Guido van Rossum72e31641996-05-28 23:15:20 +00008
Guido van Rossum18468821994-06-20 07:49:28 +00009class CanvasItem:
Fred Draked038ca82000-10-23 18:31:14 +000010 def __init__(self, canvas, itemType, *args, **kw):
11 self.canvas = canvas
12 self.id = canvas._create(itemType, args, kw)
13 if not hasattr(canvas, 'items'):
14 canvas.items = {}
15 canvas.items[self.id] = self
16 def __str__(self):
17 return str(self.id)
18 def __repr__(self):
19 return '<%s, id=%d>' % (self.__class__.__name__, self.id)
20 def delete(self):
21 del self.canvas.items[self.id]
22 self.canvas.delete(self.id)
23 def __getitem__(self, key):
24 v = self.canvas.tk.split(self.canvas.tk.call(
25 self.canvas._w, 'itemconfigure',
26 self.id, '-' + key))
27 return v[4]
28 cget = __getitem__
29 def __setitem__(self, key, value):
30 self.canvas.itemconfig(self.id, {key: value})
31 def keys(self):
32 if not hasattr(self, '_keys'):
33 self._keys = map(lambda x, tk=self.canvas.tk:
34 tk.splitlist(x)[0][1:],
35 self.canvas.tk.splitlist(
36 self.canvas._do(
37 'itemconfigure',
38 (self.id,))))
39 return self._keys
40 def has_key(self, key):
41 return key in self.keys()
42 def addtag(self, tag, option='withtag'):
43 self.canvas.addtag(tag, option, self.id)
44 def bbox(self):
45 x1, y1, x2, y2 = self.canvas.bbox(self.id)
46 return (x1, y1), (x2, y2)
47 def bind(self, sequence=None, command=None, add=None):
48 return self.canvas.tag_bind(self.id, sequence, command, add)
49 def unbind(self, sequence, funcid=None):
50 self.canvas.tag_unbind(self.id, sequence, funcid)
51 def config(self, cnf={}, **kw):
52 return self.canvas.itemconfig(self.id, _cnfmerge((cnf, kw)))
53 def coords(self, pts = ()):
54 flat = ()
55 for x, y in pts: flat = flat + (x, y)
56 return apply(self.canvas.coords, (self.id,) + flat)
57 def dchars(self, first, last=None):
58 self.canvas.dchars(self.id, first, last)
59 def dtag(self, ttd):
60 self.canvas.dtag(self.id, ttd)
61 def focus(self):
62 self.canvas.focus(self.id)
63 def gettags(self):
64 return self.canvas.gettags(self.id)
65 def icursor(self, index):
66 self.canvas.icursor(self.id, index)
67 def index(self, index):
68 return self.canvas.index(self.id, index)
69 def insert(self, beforethis, string):
70 self.canvas.insert(self.id, beforethis, string)
71 def lower(self, belowthis=None):
72 self.canvas.tag_lower(self.id, belowthis)
73 def move(self, xamount, yamount):
74 self.canvas.move(self.id, xamount, yamount)
75 def tkraise(self, abovethis=None):
76 self.canvas.tag_raise(self.id, abovethis)
77 raise_ = tkraise # BW compat
78 def scale(self, xorigin, yorigin, xscale, yscale):
79 self.canvas.scale(self.id, xorigin, yorigin, xscale, yscale)
80 def type(self):
81 return self.canvas.type(self.id)
Guido van Rossum18468821994-06-20 07:49:28 +000082
83class Arc(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +000084 def __init__(self, canvas, *args, **kw):
85 apply(CanvasItem.__init__, (self, canvas, 'arc') + args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +000086
87class Bitmap(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +000088 def __init__(self, canvas, *args, **kw):
89 apply(CanvasItem.__init__, (self, canvas, 'bitmap') + args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +000090
Guido van Rossum72e31641996-05-28 23:15:20 +000091class ImageItem(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +000092 def __init__(self, canvas, *args, **kw):
93 apply(CanvasItem.__init__, (self, canvas, 'image') + args, kw)
Guido van Rossum72e31641996-05-28 23:15:20 +000094
Guido van Rossum18468821994-06-20 07:49:28 +000095class Line(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +000096 def __init__(self, canvas, *args, **kw):
97 apply(CanvasItem.__init__, (self, canvas, 'line') + args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +000098
99class Oval(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +0000100 def __init__(self, canvas, *args, **kw):
101 apply(CanvasItem.__init__, (self, canvas, 'oval') + args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000102
103class Polygon(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +0000104 def __init__(self, canvas, *args, **kw):
105 apply(CanvasItem.__init__, (self, canvas, 'polygon') + args,kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000106
107class Rectangle(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +0000108 def __init__(self, canvas, *args, **kw):
109 apply(CanvasItem.__init__, (self, canvas, 'rectangle')+args,kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000110
Guido van Rossum0b0804b1994-06-28 13:48:26 +0000111# XXX "Text" is taken by the Text widget...
112class CanvasText(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +0000113 def __init__(self, canvas, *args, **kw):
114 apply(CanvasItem.__init__, (self, canvas, 'text') + args, kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000115
116class Window(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +0000117 def __init__(self, canvas, *args, **kw):
118 apply(CanvasItem.__init__, (self, canvas, 'window') + args, kw)
Guido van Rossum0b0804b1994-06-28 13:48:26 +0000119
120class Group:
Fred Draked038ca82000-10-23 18:31:14 +0000121 def __init__(self, canvas, tag=None):
122 if not tag:
123 tag = 'Group%d' % id(self)
124 self.tag = self.id = tag
125 self.canvas = canvas
126 self.canvas.dtag(self.tag)
127 def str(self):
128 return self.tag
129 __str__ = str
130 def _do(self, cmd, *args):
131 return self.canvas._do(cmd, (self.tag,) + _flatten(args))
132 def addtag_above(self, tagOrId):
133 self._do('addtag', 'above', tagOrId)
134 def addtag_all(self):
135 self._do('addtag', 'all')
136 def addtag_below(self, tagOrId):
137 self._do('addtag', 'below', tagOrId)
138 def addtag_closest(self, x, y, halo=None, start=None):
139 self._do('addtag', 'closest', x, y, halo, start)
140 def addtag_enclosed(self, x1, y1, x2, y2):
141 self._do('addtag', 'enclosed', x1, y1, x2, y2)
142 def addtag_overlapping(self, x1, y1, x2, y2):
143 self._do('addtag', 'overlapping', x1, y1, x2, y2)
144 def addtag_withtag(self, tagOrId):
145 self._do('addtag', 'withtag', tagOrId)
146 def bbox(self):
147 return self.canvas._getints(self._do('bbox'))
148 def bind(self, sequence=None, command=None, add=None):
149 return self.canvas.tag_bind(self.id, sequence, command, add)
150 def unbind(self, sequence, funcid=None):
151 self.canvas.tag_unbind(self.id, sequence, funcid)
152 def coords(self, *pts):
153 return self._do('coords', pts)
154 def dchars(self, first, last=None):
155 self._do('dchars', first, last)
156 def delete(self):
157 self._do('delete')
158 def dtag(self, tagToDelete=None):
159 self._do('dtag', tagToDelete)
160 def focus(self):
161 self._do('focus')
162 def gettags(self):
163 return self.canvas.tk.splitlist(self._do('gettags', self.tag))
164 def icursor(self, index):
165 return self._do('icursor', index)
166 def index(self, index):
167 return self.canvas.tk.getint(self._do('index', index))
168 def insert(self, beforeThis, string):
169 self._do('insert', beforeThis, string)
170 def config(self, cnf={}, **kw):
171 return self.canvas.itemconfigure(self.tag, _cnfmerge((cnf,kw)))
172 def lower(self, belowThis=None):
Guido van Rossum40f3c7f2001-04-10 21:13:06 +0000173 self._do('lower', belowThis)
Fred Draked038ca82000-10-23 18:31:14 +0000174 def move(self, xAmount, yAmount):
175 self._do('move', xAmount, yAmount)
176 def tkraise(self, aboveThis=None):
Guido van Rossum40f3c7f2001-04-10 21:13:06 +0000177 self._do('raise', aboveThis)
Fred Draked038ca82000-10-23 18:31:14 +0000178 lift = tkraise
179 def scale(self, xOrigin, yOrigin, xScale, yScale):
180 self._do('scale', xOrigin, yOrigin, xScale, yScale)
181 def select_adjust(self, index):
182 self.canvas._do('select', ('adjust', self.tag, index))
183 def select_from(self, index):
184 self.canvas._do('select', ('from', self.tag, index))
185 def select_to(self, index):
186 self.canvas._do('select', ('to', self.tag, index))
187 def type(self):
188 return self._do('type')