Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 1 | # This module exports classes for the various canvas item types |
| 2 | |
Guido van Rossum | 5c1d1ee | 1996-09-06 21:09:31 +0000 | [diff] [blame] | 3 | from Tkinter import Canvas, _cnfmerge, _flatten |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 72e3164 | 1996-05-28 23:15:20 +0000 | [diff] [blame] | 5 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 6 | class CanvasItem: |
Guido van Rossum | efa3143 | 1996-01-26 17:45:07 +0000 | [diff] [blame] | 7 | def __init__(self, canvas, itemType, *args, **kw): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 8 | self.canvas = canvas |
Guido van Rossum | efa3143 | 1996-01-26 17:45:07 +0000 | [diff] [blame] | 9 | self.id = canvas._create(itemType, args, kw) |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 10 | if not hasattr(canvas, 'items'): |
| 11 | canvas.items = {} |
| 12 | canvas.items[self.id] = self |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 13 | def __str__(self): |
| 14 | return str(self.id) |
| 15 | def __repr__(self): |
| 16 | return '<%s, id=%d>' % (self.__class__.__name__, self.id) |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 17 | def delete(self): |
| 18 | del self.canvas.items[self.id] |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 19 | self.canvas.delete(self.id) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 20 | def __getitem__(self, key): |
Guido van Rossum | fea128e | 1994-06-20 13:42:28 +0000 | [diff] [blame] | 21 | v = self.canvas.tk.split(self.canvas.tk.call( |
| 22 | self.canvas._w, 'itemconfigure', |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 23 | self.id, '-' + key)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 24 | return v[4] |
Fred Drake | e9c515a | 1996-09-26 20:21:26 +0000 | [diff] [blame] | 25 | cget = __getitem__ |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 26 | def __setitem__(self, key, value): |
Guido van Rossum | 72e3164 | 1996-05-28 23:15:20 +0000 | [diff] [blame] | 27 | self.canvas.itemconfig(self.id, {key: value}) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 28 | def keys(self): |
| 29 | if not hasattr(self, '_keys'): |
| 30 | self._keys = map(lambda x, tk=self.canvas.tk: |
Guido van Rossum | fea128e | 1994-06-20 13:42:28 +0000 | [diff] [blame] | 31 | tk.splitlist(x)[0][1:], |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 32 | self.canvas.tk.splitlist( |
| 33 | self.canvas._do( |
Guido van Rossum | fea128e | 1994-06-20 13:42:28 +0000 | [diff] [blame] | 34 | 'itemconfigure', |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 35 | (self.id,)))) |
Guido van Rossum | 72e3164 | 1996-05-28 23:15:20 +0000 | [diff] [blame] | 36 | return self._keys |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 37 | 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 Rossum | 421bb0e | 1996-07-21 02:19:32 +0000 | [diff] [blame] | 45 | return self.canvas.tag_bind(self.id, sequence, command) |
Guido van Rossum | 178ff35 | 1996-12-27 15:40:31 +0000 | [diff] [blame] | 46 | def unbind(self, sequence): |
| 47 | self.canvas.tag_bind(self.id, sequence, '') |
Guido van Rossum | 5c1d1ee | 1996-09-06 21:09:31 +0000 | [diff] [blame] | 48 | def config(self, cnf={}, **kw): |
| 49 | return self.canvas.itemconfig(self.id, _cnfmerge((cnf, kw))) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 50 | 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 Rossum | 6b7a7e9 | 1996-09-11 14:23:43 +0000 | [diff] [blame] | 62 | 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 Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 66 | 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 Rossum | 72e3164 | 1996-05-28 23:15:20 +0000 | [diff] [blame] | 72 | def tkraise(self, abovethis=None): |
| 73 | self.canvas.tkraise(self.id, abovethis) |
| 74 | raise_ = tkraise # BW compat |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 75 | 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 | |
| 80 | class Arc(CanvasItem): |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 81 | def __init__(self, canvas, *args, **kw): |
| 82 | apply(CanvasItem.__init__, (self, canvas, 'arc') + args, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 83 | |
| 84 | class Bitmap(CanvasItem): |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 85 | def __init__(self, canvas, *args, **kw): |
| 86 | apply(CanvasItem.__init__, (self, canvas, 'bitmap') + args, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | 72e3164 | 1996-05-28 23:15:20 +0000 | [diff] [blame] | 88 | class ImageItem(CanvasItem): |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 89 | def __init__(self, canvas, *args, **kw): |
| 90 | apply(CanvasItem.__init__, (self, canvas, 'image') + args, kw) |
Guido van Rossum | 72e3164 | 1996-05-28 23:15:20 +0000 | [diff] [blame] | 91 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 92 | class Line(CanvasItem): |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 93 | def __init__(self, canvas, *args, **kw): |
| 94 | apply(CanvasItem.__init__, (self, canvas, 'line') + args, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 95 | |
| 96 | class Oval(CanvasItem): |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 97 | def __init__(self, canvas, *args, **kw): |
| 98 | apply(CanvasItem.__init__, (self, canvas, 'oval') + args, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 99 | |
| 100 | class Polygon(CanvasItem): |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 101 | def __init__(self, canvas, *args, **kw): |
| 102 | apply(CanvasItem.__init__, (self, canvas, 'polygon') + args,kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 103 | |
| 104 | class Rectangle(CanvasItem): |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 105 | def __init__(self, canvas, *args, **kw): |
| 106 | apply(CanvasItem.__init__, (self, canvas, 'rectangle')+args,kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 107 | |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 108 | # XXX "Text" is taken by the Text widget... |
| 109 | class CanvasText(CanvasItem): |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 110 | def __init__(self, canvas, *args, **kw): |
| 111 | apply(CanvasItem.__init__, (self, canvas, 'text') + args, kw) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 112 | |
| 113 | class Window(CanvasItem): |
Guido van Rossum | c457048 | 1998-03-20 20:45:49 +0000 | [diff] [blame] | 114 | def __init__(self, canvas, *args, **kw): |
| 115 | apply(CanvasItem.__init__, (self, canvas, 'window') + args, kw) |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 116 | |
| 117 | class 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 Rossum | 5c1d1ee | 1996-09-06 21:09:31 +0000 | [diff] [blame] | 126 | __str__ = str |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 127 | 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 Rossum | 93d1fe1 | 1998-07-16 13:43:05 +0000 | [diff] [blame] | 144 | return self.canvas._getints(self._do('bbox')) |
Guido van Rossum | 178ff35 | 1996-12-27 15:40:31 +0000 | [diff] [blame] | 145 | 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 Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 149 | 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 Rossum | 6b7a7e9 | 1996-09-11 14:23:43 +0000 | [diff] [blame] | 162 | return self._do('icursor', index) |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 163 | 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 Rossum | 5c1d1ee | 1996-09-06 21:09:31 +0000 | [diff] [blame] | 167 | def config(self, cnf={}, **kw): |
| 168 | return self.canvas.itemconfigure(self.tag, _cnfmerge((cnf,kw))) |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 169 | 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') |