Mercurial > cpython-lru_cache-weakref
annotate Lib/lib-tk/Canvas.py @ 4566:6403c94de50d legacy-trunk
Added safeguard against failure in __del__.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Wed, 27 Nov 1996 19:39:58 +0000 |
| parents | 376822c65810 |
| children | c7e99ab627f3 |
| rev | line source |
|---|---|
| 1664 | 1 # This module exports classes for the various canvas item types |
| 2 | |
|
4222
1d1f0c647e77
Change to allow keyword args for Group,
Guido van Rossum <guido@python.org>
parents:
4115
diff
changeset
|
3 from Tkinter import Canvas, _cnfmerge, _flatten |
| 1664 | 4 |
|
3644
017d3356a5ed
made it work again with changed Tkinter
Guido van Rossum <guido@python.org>
parents:
3363
diff
changeset
|
5 |
| 1664 | 6 class CanvasItem: |
|
3363
369b30f78d63
add keyword args to CanvasItem class __init__
Guido van Rossum <guido@python.org>
parents:
1695
diff
changeset
|
7 def __init__(self, canvas, itemType, *args, **kw): |
| 1664 | 8 self.canvas = canvas |
|
3363
369b30f78d63
add keyword args to CanvasItem class __init__
Guido van Rossum <guido@python.org>
parents:
1695
diff
changeset
|
9 self.id = canvas._create(itemType, args, kw) |
| 1695 | 10 if not hasattr(canvas, 'items'): |
| 11 canvas.items = {} | |
| 12 canvas.items[self.id] = self | |
| 1664 | 13 def __str__(self): |
| 14 return str(self.id) | |
| 15 def __repr__(self): | |
| 16 return '<%s, id=%d>' % (self.__class__.__name__, self.id) | |
| 1695 | 17 def delete(self): |
| 18 del self.canvas.items[self.id] | |
| 1664 | 19 self.canvas.delete(self.id) |
| 20 def __getitem__(self, key): | |
|
1673
344dd28da9c5
Reformatted using 8-space wide tabs
Guido van Rossum <guido@python.org>
parents:
1664
diff
changeset
|
21 v = self.canvas.tk.split(self.canvas.tk.call( |
|
344dd28da9c5
Reformatted using 8-space wide tabs
Guido van Rossum <guido@python.org>
parents:
1664
diff
changeset
|
22 self.canvas._w, 'itemconfigure', |
| 1695 | 23 self.id, '-' + key)) |
| 1664 | 24 return v[4] |
|
4337
376822c65810
(Canvas.py): Added definition of CanvasItem.cget() as suggested by
Fred Drake <fdrake@acm.org>
parents:
4276
diff
changeset
|
25 cget = __getitem__ |
| 1664 | 26 def __setitem__(self, key, value): |
|
3644
017d3356a5ed
made it work again with changed Tkinter
Guido van Rossum <guido@python.org>
parents:
3363
diff
changeset
|
27 self.canvas.itemconfig(self.id, {key: value}) |
| 1664 | 28 def keys(self): |
| 29 if not hasattr(self, '_keys'): | |
| 30 self._keys = map(lambda x, tk=self.canvas.tk: | |
|
1673
344dd28da9c5
Reformatted using 8-space wide tabs
Guido van Rossum <guido@python.org>
parents:
1664
diff
changeset
|
31 tk.splitlist(x)[0][1:], |
| 1695 | 32 self.canvas.tk.splitlist( |
| 33 self.canvas._do( | |
|
1673
344dd28da9c5
Reformatted using 8-space wide tabs
Guido van Rossum <guido@python.org>
parents:
1664
diff
changeset
|
34 'itemconfigure', |
| 1695 | 35 (self.id,)))) |
|
3644
017d3356a5ed
made it work again with changed Tkinter
Guido van Rossum <guido@python.org>
parents:
3363
diff
changeset
|
36 return self._keys |
| 1664 | 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): | |
|
3756
82bb7d2a6b31
fix canvas bind commands
Guido van Rossum <guido@python.org>
parents:
3644
diff
changeset
|
45 return self.canvas.tag_bind(self.id, sequence, command) |
|
4222
1d1f0c647e77
Change to allow keyword args for Group,
Guido van Rossum <guido@python.org>
parents:
4115
diff
changeset
|
46 def config(self, cnf={}, **kw): |
|
1d1f0c647e77
Change to allow keyword args for Group,
Guido van Rossum <guido@python.org>
parents:
4115
diff
changeset
|
47 return self.canvas.itemconfig(self.id, _cnfmerge((cnf, kw))) |
| 1664 | 48 def coords(self, pts = ()): |
| 49 flat = () | |
| 50 for x, y in pts: flat = flat + (x, y) | |
| 51 return apply(self.canvas.coords, (self.id,) + flat) | |
| 52 def dchars(self, first, last=None): | |
| 53 self.canvas.dchars(self.id, first, last) | |
| 54 def dtag(self, ttd): | |
| 55 self.canvas.dtag(self.id, ttd) | |
| 56 def focus(self): | |
| 57 self.canvas.focus(self.id) | |
| 58 def gettags(self): | |
| 59 return self.canvas.gettags(self.id) | |
|
4276
ae178f150403
Bug fixes in index&icursor suggested by Nils Fischbeck.
Guido van Rossum <guido@python.org>
parents:
4222
diff
changeset
|
60 def icursor(self, index): |
|
ae178f150403
Bug fixes in index&icursor suggested by Nils Fischbeck.
Guido van Rossum <guido@python.org>
parents:
4222
diff
changeset
|
61 self.canvas.icursor(self.id, index) |
|
ae178f150403
Bug fixes in index&icursor suggested by Nils Fischbeck.
Guido van Rossum <guido@python.org>
parents:
4222
diff
changeset
|
62 def index(self, index): |
|
ae178f150403
Bug fixes in index&icursor suggested by Nils Fischbeck.
Guido van Rossum <guido@python.org>
parents:
4222
diff
changeset
|
63 return self.canvas.index(self.id, index) |
| 1664 | 64 def insert(self, beforethis, string): |
| 65 self.canvas.insert(self.id, beforethis, string) | |
| 66 def lower(self, belowthis=None): | |
| 67 self.canvas.lower(self.id, belowthis) | |
| 68 def move(self, xamount, yamount): | |
| 69 self.canvas.move(self.id, xamount, yamount) | |
|
3644
017d3356a5ed
made it work again with changed Tkinter
Guido van Rossum <guido@python.org>
parents:
3363
diff
changeset
|
70 def tkraise(self, abovethis=None): |
|
017d3356a5ed
made it work again with changed Tkinter
Guido van Rossum <guido@python.org>
parents:
3363
diff
changeset
|
71 self.canvas.tkraise(self.id, abovethis) |
|
017d3356a5ed
made it work again with changed Tkinter
Guido van Rossum <guido@python.org>
parents:
3363
diff
changeset
|
72 raise_ = tkraise # BW compat |
| 1664 | 73 def scale(self, xorigin, yorigin, xscale, yscale): |
| 74 self.canvas.scale(self.id, xorigin, yorigin, xscale, yscale) | |
| 75 def type(self): | |
| 76 return self.canvas.type(self.id) | |
| 77 | |
| 78 class Arc(CanvasItem): | |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
79 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
80 apply(CanvasItem.__init__, (self, canvas, 'arc') + args, kw) |
| 1664 | 81 |
| 82 class Bitmap(CanvasItem): | |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
83 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
84 apply(CanvasItem.__init__, (self, canvas, 'bitmap') + args, kw) |
| 1664 | 85 |
|
3644
017d3356a5ed
made it work again with changed Tkinter
Guido van Rossum <guido@python.org>
parents:
3363
diff
changeset
|
86 class ImageItem(CanvasItem): |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
87 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
88 apply(CanvasItem.__init__, (self, canvas, 'image') + args, kw) |
|
3644
017d3356a5ed
made it work again with changed Tkinter
Guido van Rossum <guido@python.org>
parents:
3363
diff
changeset
|
89 |
| 1664 | 90 class Line(CanvasItem): |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
91 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
92 apply(CanvasItem.__init__, (self, canvas, 'line') + args, kw) |
| 1664 | 93 |
| 94 class Oval(CanvasItem): | |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
95 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
96 apply(CanvasItem.__init__, (self, canvas, 'oval') + args, kw) |
| 1664 | 97 |
| 98 class Polygon(CanvasItem): | |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
99 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
100 apply(CanvasItem.__init__, (self, canvas, 'polygon') + args, kw) |
| 1664 | 101 |
| 102 class Rectangle(CanvasItem): | |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
103 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
104 apply(CanvasItem.__init__, (self, canvas, 'rectangle') + args, kw) |
| 1664 | 105 |
| 1695 | 106 # XXX "Text" is taken by the Text widget... |
| 107 class CanvasText(CanvasItem): | |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
108 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
109 apply(CanvasItem.__init__, (self, canvas, 'text') + args, kw) |
| 1664 | 110 |
| 111 class Window(CanvasItem): | |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
112 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
113 apply(CanvasItem.__init__, (self, canvas, 'window') + args, kw) |
| 1695 | 114 |
| 115 class Group: | |
| 116 def __init__(self, canvas, tag=None): | |
| 117 if not tag: | |
| 118 tag = 'Group%d' % id(self) | |
| 119 self.tag = self.id = tag | |
| 120 self.canvas = canvas | |
| 121 self.canvas.dtag(self.tag) | |
| 122 def str(self): | |
| 123 return self.tag | |
|
4222
1d1f0c647e77
Change to allow keyword args for Group,
Guido van Rossum <guido@python.org>
parents:
4115
diff
changeset
|
124 __str__ = str |
| 1695 | 125 def _do(self, cmd, *args): |
| 126 return self.canvas._do(cmd, (self.tag,) + _flatten(args)) | |
| 127 def addtag_above(self, tagOrId): | |
| 128 self._do('addtag', 'above', tagOrId) | |
| 129 def addtag_all(self): | |
| 130 self._do('addtag', 'all') | |
| 131 def addtag_below(self, tagOrId): | |
| 132 self._do('addtag', 'below', tagOrId) | |
| 133 def addtag_closest(self, x, y, halo=None, start=None): | |
| 134 self._do('addtag', 'closest', x, y, halo, start) | |
| 135 def addtag_enclosed(self, x1, y1, x2, y2): | |
| 136 self._do('addtag', 'enclosed', x1, y1, x2, y2) | |
| 137 def addtag_overlapping(self, x1, y1, x2, y2): | |
| 138 self._do('addtag', 'overlapping', x1, y1, x2, y2) | |
| 139 def addtag_withtag(self, tagOrId): | |
| 140 self._do('addtag', 'withtag', tagOrId) | |
| 141 def bbox(self): | |
| 142 return self._getints(self._do('bbox')) | |
| 143 def bind(self, sequence=None, command=None): # XXX args? | |
| 144 return self._do('bind', sequence, command) | |
| 145 def coords(self, *pts): | |
| 146 return self._do('coords', pts) | |
| 147 def dchars(self, first, last=None): | |
| 148 self._do('dchars', first, last) | |
| 149 def delete(self): | |
| 150 self._do('delete') | |
| 151 def dtag(self, tagToDelete=None): | |
| 152 self._do('dtag', tagToDelete) | |
| 153 def focus(self): | |
| 154 self._do('focus') | |
| 155 def gettags(self): | |
| 156 return self.canvas.tk.splitlist(self._do('gettags', self.tag)) | |
| 157 def icursor(self, index): | |
|
4276
ae178f150403
Bug fixes in index&icursor suggested by Nils Fischbeck.
Guido van Rossum <guido@python.org>
parents:
4222
diff
changeset
|
158 return self._do('icursor', index) |
| 1695 | 159 def index(self, index): |
| 160 return self.canvas.tk.getint(self._do('index', index)) | |
| 161 def insert(self, beforeThis, string): | |
| 162 self._do('insert', beforeThis, string) | |
|
4222
1d1f0c647e77
Change to allow keyword args for Group,
Guido van Rossum <guido@python.org>
parents:
4115
diff
changeset
|
163 def config(self, cnf={}, **kw): |
|
1d1f0c647e77
Change to allow keyword args for Group,
Guido van Rossum <guido@python.org>
parents:
4115
diff
changeset
|
164 return self.canvas.itemconfigure(self.tag, _cnfmerge((cnf,kw))) |
| 1695 | 165 def lower(self, belowThis=None): |
| 166 self._do('lower', belowThis) | |
| 167 def move(self, xAmount, yAmount): | |
| 168 self._do('move', xAmount, yAmount) | |
| 169 def tkraise(self, aboveThis=None): | |
| 170 self._do('raise', aboveThis) | |
| 171 lift = tkraise | |
| 172 def scale(self, xOrigin, yOrigin, xScale, yScale): | |
| 173 self._do('scale', xOrigin, yOrigin, xScale, yScale) | |
| 174 def select_adjust(self, index): | |
| 175 self.canvas._do('select', ('adjust', self.tag, index)) | |
| 176 def select_from(self, index): | |
| 177 self.canvas._do('select', ('from', self.tag, index)) | |
| 178 def select_to(self, index): | |
| 179 self.canvas._do('select', ('to', self.tag, index)) | |
| 180 def type(self): | |
| 181 return self._do('type') |
