Mercurial > cpython-lru_cache-weakref
annotate Lib/tkinter/Canvas.py @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | c7e99ab627f3 |
| children |
| 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) |
|
4834
c7e99ab627f3
Fix bogus implementation of Group.bind().
Guido van Rossum <guido@python.org>
parents:
4337
diff
changeset
|
46 def unbind(self, sequence): |
|
c7e99ab627f3
Fix bogus implementation of Group.bind().
Guido van Rossum <guido@python.org>
parents:
4337
diff
changeset
|
47 self.canvas.tag_bind(self.id, sequence, '') |
|
4222
1d1f0c647e77
Change to allow keyword args for Group,
Guido van Rossum <guido@python.org>
parents:
4115
diff
changeset
|
48 def config(self, cnf={}, **kw): |
|
1d1f0c647e77
Change to allow keyword args for Group,
Guido van Rossum <guido@python.org>
parents:
4115
diff
changeset
|
49 return self.canvas.itemconfig(self.id, _cnfmerge((cnf, kw))) |
| 1664 | 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) | |
|
4276
ae178f150403
Bug fixes in index&icursor suggested by Nils Fischbeck.
Guido van Rossum <guido@python.org>
parents:
4222
diff
changeset
|
62 def icursor(self, index): |
|
ae178f150403
Bug fixes in index&icursor suggested by Nils Fischbeck.
Guido van Rossum <guido@python.org>
parents:
4222
diff
changeset
|
63 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
|
64 def index(self, index): |
|
ae178f150403
Bug fixes in index&icursor suggested by Nils Fischbeck.
Guido van Rossum <guido@python.org>
parents:
4222
diff
changeset
|
65 return self.canvas.index(self.id, index) |
| 1664 | 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) | |
|
3644
017d3356a5ed
made it work again with changed Tkinter
Guido van Rossum <guido@python.org>
parents:
3363
diff
changeset
|
72 def tkraise(self, abovethis=None): |
|
017d3356a5ed
made it work again with changed Tkinter
Guido van Rossum <guido@python.org>
parents:
3363
diff
changeset
|
73 self.canvas.tkraise(self.id, abovethis) |
|
017d3356a5ed
made it work again with changed Tkinter
Guido van Rossum <guido@python.org>
parents:
3363
diff
changeset
|
74 raise_ = tkraise # BW compat |
| 1664 | 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): | |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
81 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
82 apply(CanvasItem.__init__, (self, canvas, 'arc') + args, kw) |
| 1664 | 83 |
| 84 class Bitmap(CanvasItem): | |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
85 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
86 apply(CanvasItem.__init__, (self, canvas, 'bitmap') + args, kw) |
| 1664 | 87 |
|
3644
017d3356a5ed
made it work again with changed Tkinter
Guido van Rossum <guido@python.org>
parents:
3363
diff
changeset
|
88 class ImageItem(CanvasItem): |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
89 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
90 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
|
91 |
| 1664 | 92 class Line(CanvasItem): |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
93 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
94 apply(CanvasItem.__init__, (self, canvas, 'line') + args, kw) |
| 1664 | 95 |
| 96 class Oval(CanvasItem): | |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
97 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
98 apply(CanvasItem.__init__, (self, canvas, 'oval') + args, kw) |
| 1664 | 99 |
| 100 class Polygon(CanvasItem): | |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
101 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
102 apply(CanvasItem.__init__, (self, canvas, 'polygon') + args, kw) |
| 1664 | 103 |
| 104 class Rectangle(CanvasItem): | |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
105 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
106 apply(CanvasItem.__init__, (self, canvas, 'rectangle') + args, kw) |
| 1664 | 107 |
| 1695 | 108 # XXX "Text" is taken by the Text widget... |
| 109 class CanvasText(CanvasItem): | |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
110 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
111 apply(CanvasItem.__init__, (self, canvas, 'text') + args, kw) |
| 1664 | 112 |
| 113 class Window(CanvasItem): | |
|
4115
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
114 def __init__(self, canvas, *args, **kw): |
|
23589c016cb9
Changes for Canvas by Fred
Guido van Rossum <guido@python.org>
parents:
3951
diff
changeset
|
115 apply(CanvasItem.__init__, (self, canvas, 'window') + args, kw) |
| 1695 | 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 | |
|
4222
1d1f0c647e77
Change to allow keyword args for Group,
Guido van Rossum <guido@python.org>
parents:
4115
diff
changeset
|
126 __str__ = str |
| 1695 | 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): | |
| 144 return self._getints(self._do('bbox')) | |
|
4834
c7e99ab627f3
Fix bogus implementation of Group.bind().
Guido van Rossum <guido@python.org>
parents:
4337
diff
changeset
|
145 def bind(self, sequence=None, command=None): |
|
c7e99ab627f3
Fix bogus implementation of Group.bind().
Guido van Rossum <guido@python.org>
parents:
4337
diff
changeset
|
146 return self.canvas.tag_bind(self.id, sequence, command) |
|
c7e99ab627f3
Fix bogus implementation of Group.bind().
Guido van Rossum <guido@python.org>
parents:
4337
diff
changeset
|
147 def unbind(self, sequence): |
|
c7e99ab627f3
Fix bogus implementation of Group.bind().
Guido van Rossum <guido@python.org>
parents:
4337
diff
changeset
|
148 self.canvas.tag_bind(self.id, sequence, '') |
| 1695 | 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): | |
|
4276
ae178f150403
Bug fixes in index&icursor suggested by Nils Fischbeck.
Guido van Rossum <guido@python.org>
parents:
4222
diff
changeset
|
162 return self._do('icursor', index) |
| 1695 | 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) | |
|
4222
1d1f0c647e77
Change to allow keyword args for Group,
Guido van Rossum <guido@python.org>
parents:
4115
diff
changeset
|
167 def config(self, cnf={}, **kw): |
|
1d1f0c647e77
Change to allow keyword args for Group,
Guido van Rossum <guido@python.org>
parents:
4115
diff
changeset
|
168 return self.canvas.itemconfigure(self.tag, _cnfmerge((cnf,kw))) |
| 1695 | 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') |
