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 | 72e3164 | 1996-05-28 23:15:20 +0000 | [diff] [blame] | 3 | from Tkinter import Canvas |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 524e9a4 | 1996-08-08 18:31:11 +0000 | [diff] [blame] | 5 | from types import * |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 72e3164 | 1996-05-28 23:15:20 +0000 | [diff] [blame] | 7 | def _flatten(tuple): |
| 8 | res = () |
| 9 | for item in tuple: |
| 10 | if type(item) in (TupleType, ListType): |
| 11 | res = res + _flatten(item) |
| 12 | elif item is not None: |
| 13 | res = res + (item,) |
| 14 | return res |
| 15 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 16 | class CanvasItem: |
Guido van Rossum | efa3143 | 1996-01-26 17:45:07 +0000 | [diff] [blame] | 17 | def __init__(self, canvas, itemType, *args, **kw): |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 18 | self.canvas = canvas |
Guido van Rossum | efa3143 | 1996-01-26 17:45:07 +0000 | [diff] [blame] | 19 | self.id = canvas._create(itemType, args, kw) |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 20 | if not hasattr(canvas, 'items'): |
| 21 | canvas.items = {} |
| 22 | canvas.items[self.id] = self |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 23 | def __str__(self): |
| 24 | return str(self.id) |
| 25 | def __repr__(self): |
| 26 | return '<%s, id=%d>' % (self.__class__.__name__, self.id) |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 27 | def delete(self): |
| 28 | del self.canvas.items[self.id] |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 29 | self.canvas.delete(self.id) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 30 | def __getitem__(self, key): |
Guido van Rossum | fea128e | 1994-06-20 13:42:28 +0000 | [diff] [blame] | 31 | v = self.canvas.tk.split(self.canvas.tk.call( |
| 32 | self.canvas._w, 'itemconfigure', |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 33 | self.id, '-' + key)) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 34 | return v[4] |
| 35 | def __setitem__(self, key, value): |
Guido van Rossum | 72e3164 | 1996-05-28 23:15:20 +0000 | [diff] [blame] | 36 | self.canvas.itemconfig(self.id, {key: value}) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 37 | def keys(self): |
| 38 | if not hasattr(self, '_keys'): |
| 39 | self._keys = map(lambda x, tk=self.canvas.tk: |
Guido van Rossum | fea128e | 1994-06-20 13:42:28 +0000 | [diff] [blame] | 40 | tk.splitlist(x)[0][1:], |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 41 | self.canvas.tk.splitlist( |
| 42 | self.canvas._do( |
Guido van Rossum | fea128e | 1994-06-20 13:42:28 +0000 | [diff] [blame] | 43 | 'itemconfigure', |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 44 | (self.id,)))) |
Guido van Rossum | 72e3164 | 1996-05-28 23:15:20 +0000 | [diff] [blame] | 45 | return self._keys |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 46 | def has_key(self, key): |
| 47 | return key in self.keys() |
| 48 | 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): |
Guido van Rossum | 421bb0e | 1996-07-21 02:19:32 +0000 | [diff] [blame] | 54 | return self.canvas.tag_bind(self.id, sequence, command) |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 55 | def config(self, cnf=None): |
| 56 | return self.canvas.itemconfig(self.id, cnf) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 57 | def coords(self, pts = ()): |
| 58 | flat = () |
| 59 | for x, y in pts: flat = flat + (x, y) |
| 60 | return apply(self.canvas.coords, (self.id,) + flat) |
| 61 | def dchars(self, first, last=None): |
| 62 | self.canvas.dchars(self.id, first, last) |
| 63 | def dtag(self, ttd): |
| 64 | self.canvas.dtag(self.id, ttd) |
| 65 | def focus(self): |
| 66 | self.canvas.focus(self.id) |
| 67 | def gettags(self): |
| 68 | return self.canvas.gettags(self.id) |
| 69 | def icursor(self): |
| 70 | self.canvas.icursor(self.id) |
| 71 | def index(self): |
| 72 | return self.canvas.index(self.id) |
| 73 | def insert(self, beforethis, string): |
| 74 | self.canvas.insert(self.id, beforethis, string) |
| 75 | def lower(self, belowthis=None): |
| 76 | self.canvas.lower(self.id, belowthis) |
| 77 | def move(self, xamount, yamount): |
| 78 | self.canvas.move(self.id, xamount, yamount) |
Guido van Rossum | 72e3164 | 1996-05-28 23:15:20 +0000 | [diff] [blame] | 79 | def tkraise(self, abovethis=None): |
| 80 | self.canvas.tkraise(self.id, abovethis) |
| 81 | raise_ = tkraise # BW compat |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 82 | def scale(self, xorigin, yorigin, xscale, yscale): |
| 83 | self.canvas.scale(self.id, xorigin, yorigin, xscale, yscale) |
| 84 | def type(self): |
| 85 | return self.canvas.type(self.id) |
| 86 | |
| 87 | class Arc(CanvasItem): |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 88 | def __init__(self, canvas, *args): |
| 89 | CanvasItem.__init__(self, canvas, 'arc', args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 90 | |
| 91 | class Bitmap(CanvasItem): |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 92 | def __init__(self, canvas, *args): |
| 93 | CanvasItem.__init__(self, canvas, 'bitmap', args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 94 | |
Guido van Rossum | 72e3164 | 1996-05-28 23:15:20 +0000 | [diff] [blame] | 95 | class ImageItem(CanvasItem): |
| 96 | def __init__(self, canvas, *args): |
| 97 | CanvasItem.__init__(self, canvas, 'image', args) |
| 98 | |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 99 | class Line(CanvasItem): |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 100 | def __init__(self, canvas, *args): |
| 101 | CanvasItem.__init__(self, canvas, 'line', args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 102 | |
| 103 | class Oval(CanvasItem): |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 104 | def __init__(self, canvas, *args): |
| 105 | CanvasItem.__init__(self, canvas, 'oval', args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 106 | |
| 107 | class Polygon(CanvasItem): |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 108 | def __init__(self, canvas, *args): |
| 109 | CanvasItem.__init__(self, canvas, 'polygon', args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 110 | |
| 111 | class Rectangle(CanvasItem): |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 112 | def __init__(self, canvas, *args): |
| 113 | CanvasItem.__init__(self, canvas, 'rectangle', args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 114 | |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 115 | # XXX "Text" is taken by the Text widget... |
| 116 | class CanvasText(CanvasItem): |
| 117 | def __init__(self, canvas, *args): |
| 118 | CanvasItem.__init__(self, canvas, 'text', args) |
Guido van Rossum | 1846882 | 1994-06-20 07:49:28 +0000 | [diff] [blame] | 119 | |
| 120 | class Window(CanvasItem): |
Guido van Rossum | 0b0804b | 1994-06-28 13:48:26 +0000 | [diff] [blame] | 121 | def __init__(self, canvas, *args): |
| 122 | CanvasItem.__init__(self, canvas, 'window', args) |
| 123 | |
| 124 | class Group: |
| 125 | def __init__(self, canvas, tag=None): |
| 126 | if not tag: |
| 127 | tag = 'Group%d' % id(self) |
| 128 | self.tag = self.id = tag |
| 129 | self.canvas = canvas |
| 130 | self.canvas.dtag(self.tag) |
| 131 | def str(self): |
| 132 | return self.tag |
| 133 | def _do(self, cmd, *args): |
| 134 | return self.canvas._do(cmd, (self.tag,) + _flatten(args)) |
| 135 | def addtag_above(self, tagOrId): |
| 136 | self._do('addtag', 'above', tagOrId) |
| 137 | def addtag_all(self): |
| 138 | self._do('addtag', 'all') |
| 139 | def addtag_below(self, tagOrId): |
| 140 | self._do('addtag', 'below', tagOrId) |
| 141 | def addtag_closest(self, x, y, halo=None, start=None): |
| 142 | self._do('addtag', 'closest', x, y, halo, start) |
| 143 | def addtag_enclosed(self, x1, y1, x2, y2): |
| 144 | self._do('addtag', 'enclosed', x1, y1, x2, y2) |
| 145 | def addtag_overlapping(self, x1, y1, x2, y2): |
| 146 | self._do('addtag', 'overlapping', x1, y1, x2, y2) |
| 147 | def addtag_withtag(self, tagOrId): |
| 148 | self._do('addtag', 'withtag', tagOrId) |
| 149 | def bbox(self): |
| 150 | return self._getints(self._do('bbox')) |
| 151 | def bind(self, sequence=None, command=None): # XXX args? |
| 152 | return self._do('bind', sequence, command) |
| 153 | def coords(self, *pts): |
| 154 | return self._do('coords', pts) |
| 155 | def dchars(self, first, last=None): |
| 156 | self._do('dchars', first, last) |
| 157 | def delete(self): |
| 158 | self._do('delete') |
| 159 | def dtag(self, tagToDelete=None): |
| 160 | self._do('dtag', tagToDelete) |
| 161 | def focus(self): |
| 162 | self._do('focus') |
| 163 | def gettags(self): |
| 164 | return self.canvas.tk.splitlist(self._do('gettags', self.tag)) |
| 165 | def icursor(self, index): |
| 166 | return self._do('icursor') |
| 167 | def index(self, index): |
| 168 | return self.canvas.tk.getint(self._do('index', index)) |
| 169 | def insert(self, beforeThis, string): |
| 170 | self._do('insert', beforeThis, string) |
| 171 | def config(self, cnf=None): |
| 172 | return self.canvas.itemconfigure(self.tag, cnf) |
| 173 | def lower(self, belowThis=None): |
| 174 | self._do('lower', belowThis) |
| 175 | def move(self, xAmount, yAmount): |
| 176 | self._do('move', xAmount, yAmount) |
| 177 | def tkraise(self, aboveThis=None): |
| 178 | self._do('raise', aboveThis) |
| 179 | lift = tkraise |
| 180 | def scale(self, xOrigin, yOrigin, xScale, yScale): |
| 181 | self._do('scale', xOrigin, yOrigin, xScale, yScale) |
| 182 | def select_adjust(self, index): |
| 183 | self.canvas._do('select', ('adjust', self.tag, index)) |
| 184 | def select_from(self, index): |
| 185 | self.canvas._do('select', ('from', self.tag, index)) |
| 186 | def select_to(self, index): |
| 187 | self.canvas._do('select', ('to', self.tag, index)) |
| 188 | def type(self): |
| 189 | return self._do('type') |