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