blob: 34464ab979fbbc9f9e8e63a290a5800f724fd18e [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
Brett Cannond5a09852008-05-10 03:14:32 +00006from warnings import warnpy3k
7warnpy3k("the Canvas module has been removed in Python 3.0", stacklevel=2)
8del warnpy3k
9
Georg Brandl6634bf22008-05-20 07:13:37 +000010from Tkinter import Canvas, _cnfmerge, _flatten
Guido van Rossum0b0804b1994-06-28 13:48:26 +000011
Guido van Rossum72e31641996-05-28 23:15:20 +000012
Guido van Rossum18468821994-06-20 07:49:28 +000013class CanvasItem:
Fred Draked038ca82000-10-23 18:31:14 +000014 def __init__(self, canvas, itemType, *args, **kw):
15 self.canvas = canvas
16 self.id = canvas._create(itemType, args, kw)
17 if not hasattr(canvas, 'items'):
18 canvas.items = {}
19 canvas.items[self.id] = self
20 def __str__(self):
21 return str(self.id)
22 def __repr__(self):
23 return '<%s, id=%d>' % (self.__class__.__name__, self.id)
24 def delete(self):
25 del self.canvas.items[self.id]
26 self.canvas.delete(self.id)
27 def __getitem__(self, key):
28 v = self.canvas.tk.split(self.canvas.tk.call(
29 self.canvas._w, 'itemconfigure',
30 self.id, '-' + key))
31 return v[4]
32 cget = __getitem__
33 def __setitem__(self, key, value):
34 self.canvas.itemconfig(self.id, {key: value})
35 def keys(self):
36 if not hasattr(self, '_keys'):
37 self._keys = map(lambda x, tk=self.canvas.tk:
38 tk.splitlist(x)[0][1:],
39 self.canvas.tk.splitlist(
40 self.canvas._do(
41 'itemconfigure',
42 (self.id,))))
43 return self._keys
44 def has_key(self, key):
45 return key in self.keys()
Raymond Hettinger0e449232003-01-30 00:56:33 +000046 def __contains__(self, key):
47 return key in self.keys()
Fred Draked038ca82000-10-23 18:31:14 +000048 def addtag(self, tag, option='withtag'):
49 self.canvas.addtag(tag, option, self.id)
50 def bbox(self):
51 x1, y1, x2, y2 = self.canvas.bbox(self.id)
52 return (x1, y1), (x2, y2)
53 def bind(self, sequence=None, command=None, add=None):
54 return self.canvas.tag_bind(self.id, sequence, command, add)
55 def unbind(self, sequence, funcid=None):
56 self.canvas.tag_unbind(self.id, sequence, funcid)
57 def config(self, cnf={}, **kw):
58 return self.canvas.itemconfig(self.id, _cnfmerge((cnf, kw)))
59 def coords(self, pts = ()):
60 flat = ()
61 for x, y in pts: flat = flat + (x, y)
Raymond Hettingerff41c482003-04-06 09:01:11 +000062 return self.canvas.coords(self.id, *flat)
Fred Draked038ca82000-10-23 18:31:14 +000063 def dchars(self, first, last=None):
64 self.canvas.dchars(self.id, first, last)
65 def dtag(self, ttd):
66 self.canvas.dtag(self.id, ttd)
67 def focus(self):
68 self.canvas.focus(self.id)
69 def gettags(self):
70 return self.canvas.gettags(self.id)
71 def icursor(self, index):
72 self.canvas.icursor(self.id, index)
73 def index(self, index):
74 return self.canvas.index(self.id, index)
75 def insert(self, beforethis, string):
76 self.canvas.insert(self.id, beforethis, string)
77 def lower(self, belowthis=None):
78 self.canvas.tag_lower(self.id, belowthis)
79 def move(self, xamount, yamount):
80 self.canvas.move(self.id, xamount, yamount)
81 def tkraise(self, abovethis=None):
82 self.canvas.tag_raise(self.id, abovethis)
83 raise_ = tkraise # BW compat
84 def scale(self, xorigin, yorigin, xscale, yscale):
85 self.canvas.scale(self.id, xorigin, yorigin, xscale, yscale)
86 def type(self):
87 return self.canvas.type(self.id)
Guido van Rossum18468821994-06-20 07:49:28 +000088
89class Arc(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +000090 def __init__(self, canvas, *args, **kw):
Raymond Hettingerff41c482003-04-06 09:01:11 +000091 CanvasItem.__init__(self, canvas, 'arc', *args, **kw)
Guido van Rossum18468821994-06-20 07:49:28 +000092
93class Bitmap(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +000094 def __init__(self, canvas, *args, **kw):
Raymond Hettingerff41c482003-04-06 09:01:11 +000095 CanvasItem.__init__(self, canvas, 'bitmap', *args, **kw)
Guido van Rossum18468821994-06-20 07:49:28 +000096
Guido van Rossum72e31641996-05-28 23:15:20 +000097class ImageItem(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +000098 def __init__(self, canvas, *args, **kw):
Raymond Hettingerff41c482003-04-06 09:01:11 +000099 CanvasItem.__init__(self, canvas, 'image', *args, **kw)
Guido van Rossum72e31641996-05-28 23:15:20 +0000100
Guido van Rossum18468821994-06-20 07:49:28 +0000101class Line(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +0000102 def __init__(self, canvas, *args, **kw):
Raymond Hettingerff41c482003-04-06 09:01:11 +0000103 CanvasItem.__init__(self, canvas, 'line', *args, **kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000104
105class Oval(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +0000106 def __init__(self, canvas, *args, **kw):
Raymond Hettingerff41c482003-04-06 09:01:11 +0000107 CanvasItem.__init__(self, canvas, 'oval', *args, **kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000108
109class Polygon(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +0000110 def __init__(self, canvas, *args, **kw):
Raymond Hettingerff41c482003-04-06 09:01:11 +0000111 CanvasItem.__init__(self, canvas, 'polygon', *args, **kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000112
113class Rectangle(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +0000114 def __init__(self, canvas, *args, **kw):
Raymond Hettingerff41c482003-04-06 09:01:11 +0000115 CanvasItem.__init__(self, canvas, 'rectangle', *args, **kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000116
Guido van Rossum0b0804b1994-06-28 13:48:26 +0000117# XXX "Text" is taken by the Text widget...
118class CanvasText(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +0000119 def __init__(self, canvas, *args, **kw):
Raymond Hettingerff41c482003-04-06 09:01:11 +0000120 CanvasItem.__init__(self, canvas, 'text', *args, **kw)
Guido van Rossum18468821994-06-20 07:49:28 +0000121
122class Window(CanvasItem):
Fred Draked038ca82000-10-23 18:31:14 +0000123 def __init__(self, canvas, *args, **kw):
Raymond Hettingerff41c482003-04-06 09:01:11 +0000124 CanvasItem.__init__(self, canvas, 'window', *args, **kw)
Guido van Rossum0b0804b1994-06-28 13:48:26 +0000125
126class Group:
Fred Draked038ca82000-10-23 18:31:14 +0000127 def __init__(self, canvas, tag=None):
128 if not tag:
129 tag = 'Group%d' % id(self)
130 self.tag = self.id = tag
131 self.canvas = canvas
132 self.canvas.dtag(self.tag)
133 def str(self):
134 return self.tag
135 __str__ = str
136 def _do(self, cmd, *args):
137 return self.canvas._do(cmd, (self.tag,) + _flatten(args))
138 def addtag_above(self, tagOrId):
139 self._do('addtag', 'above', tagOrId)
140 def addtag_all(self):
141 self._do('addtag', 'all')
142 def addtag_below(self, tagOrId):
143 self._do('addtag', 'below', tagOrId)
144 def addtag_closest(self, x, y, halo=None, start=None):
145 self._do('addtag', 'closest', x, y, halo, start)
146 def addtag_enclosed(self, x1, y1, x2, y2):
147 self._do('addtag', 'enclosed', x1, y1, x2, y2)
148 def addtag_overlapping(self, x1, y1, x2, y2):
149 self._do('addtag', 'overlapping', x1, y1, x2, y2)
150 def addtag_withtag(self, tagOrId):
151 self._do('addtag', 'withtag', tagOrId)
152 def bbox(self):
153 return self.canvas._getints(self._do('bbox'))
154 def bind(self, sequence=None, command=None, add=None):
155 return self.canvas.tag_bind(self.id, sequence, command, add)
156 def unbind(self, sequence, funcid=None):
157 self.canvas.tag_unbind(self.id, sequence, funcid)
158 def coords(self, *pts):
159 return self._do('coords', pts)
160 def dchars(self, first, last=None):
161 self._do('dchars', first, last)
162 def delete(self):
163 self._do('delete')
164 def dtag(self, tagToDelete=None):
165 self._do('dtag', tagToDelete)
166 def focus(self):
167 self._do('focus')
168 def gettags(self):
169 return self.canvas.tk.splitlist(self._do('gettags', self.tag))
170 def icursor(self, index):
171 return self._do('icursor', index)
172 def index(self, index):
173 return self.canvas.tk.getint(self._do('index', index))
174 def insert(self, beforeThis, string):
175 self._do('insert', beforeThis, string)
176 def config(self, cnf={}, **kw):
177 return self.canvas.itemconfigure(self.tag, _cnfmerge((cnf,kw)))
178 def lower(self, belowThis=None):
Guido van Rossum40f3c7f2001-04-10 21:13:06 +0000179 self._do('lower', belowThis)
Fred Draked038ca82000-10-23 18:31:14 +0000180 def move(self, xAmount, yAmount):
181 self._do('move', xAmount, yAmount)
182 def tkraise(self, aboveThis=None):
Guido van Rossum40f3c7f2001-04-10 21:13:06 +0000183 self._do('raise', aboveThis)
Fred Draked038ca82000-10-23 18:31:14 +0000184 lift = tkraise
185 def scale(self, xOrigin, yOrigin, xScale, yScale):
186 self._do('scale', xOrigin, yOrigin, xScale, yScale)
187 def select_adjust(self, index):
188 self.canvas._do('select', ('adjust', self.tag, index))
189 def select_from(self, index):
190 self.canvas._do('select', ('from', self.tag, index))
191 def select_to(self, index):
192 self.canvas._do('select', ('to', self.tag, index))
193 def type(self):
194 return self._do('type')