1 # This module exports classes for the various canvas item types
3 from Tkinter
import Canvas
, _cnfmerge
, _flatten
7 def __init__(self
, canvas
, itemType
, *args
, **kw
):
9 self
.id = canvas
._create
(itemType
, args
, kw
)
10 if not hasattr(canvas
, 'items'):
12 canvas
.items
[self
.id] = self
16 return '<%s, id=%d>' % (self
.__class
__.__name
__, self
.id)
18 del self
.canvas
.items
[self
.id]
19 self
.canvas
.delete(self
.id)
20 def __getitem__(self
, key
):
21 v
= self
.canvas
.tk
.split(self
.canvas
.tk
.call(
22 self
.canvas
._w
, 'itemconfigure',
26 def __setitem__(self
, key
, value
):
27 self
.canvas
.itemconfig(self
.id, {key
: value
})
29 if not hasattr(self
, '_keys'):
30 self
._keys
= map(lambda x
, tk
=self
.canvas
.tk
:
31 tk
.splitlist(x
)[0][1:],
32 self
.canvas
.tk
.splitlist(
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)
42 x1
, y1
, x2
, y2
= self
.canvas
.bbox(self
.id)
43 return (x1
, y1
), (x2
, y2
)
44 def bind(self
, sequence
=None, command
=None):
45 return self
.canvas
.tag_bind(self
.id, sequence
, command
)
46 def unbind(self
, sequence
):
47 self
.canvas
.tag_bind(self
.id, sequence
, '')
48 def config(self
, cnf
={}, **kw
):
49 return self
.canvas
.itemconfig(self
.id, _cnfmerge((cnf
, kw
)))
50 def coords(self
, pts
= ()):
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
)
57 self
.canvas
.dtag(self
.id, ttd
)
59 self
.canvas
.focus(self
.id)
61 return self
.canvas
.gettags(self
.id)
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
)
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
)
72 def tkraise(self
, abovethis
=None):
73 self
.canvas
.tkraise(self
.id, abovethis
)
74 raise_
= tkraise
# BW compat
75 def scale(self
, xorigin
, yorigin
, xscale
, yscale
):
76 self
.canvas
.scale(self
.id, xorigin
, yorigin
, xscale
, yscale
)
78 return self
.canvas
.type(self
.id)
80 class Arc(CanvasItem
):
81 def __init__(self
, canvas
, *args
, **kw
):
82 apply(CanvasItem
.__init
__, (self
, canvas
, 'arc') + args
, kw
)
84 class Bitmap(CanvasItem
):
85 def __init__(self
, canvas
, *args
, **kw
):
86 apply(CanvasItem
.__init
__, (self
, canvas
, 'bitmap') + args
, kw
)
88 class ImageItem(CanvasItem
):
89 def __init__(self
, canvas
, *args
, **kw
):
90 apply(CanvasItem
.__init
__, (self
, canvas
, 'image') + args
, kw
)
92 class Line(CanvasItem
):
93 def __init__(self
, canvas
, *args
, **kw
):
94 apply(CanvasItem
.__init
__, (self
, canvas
, 'line') + args
, kw
)
96 class Oval(CanvasItem
):
97 def __init__(self
, canvas
, *args
, **kw
):
98 apply(CanvasItem
.__init
__, (self
, canvas
, 'oval') + args
, kw
)
100 class Polygon(CanvasItem
):
101 def __init__(self
, canvas
, *args
, **kw
):
102 apply(CanvasItem
.__init
__, (self
, canvas
, 'polygon') + args
,kw
)
104 class Rectangle(CanvasItem
):
105 def __init__(self
, canvas
, *args
, **kw
):
106 apply(CanvasItem
.__init
__, (self
, canvas
, 'rectangle')+args
,kw
)
108 # XXX "Text" is taken by the Text widget...
109 class CanvasText(CanvasItem
):
110 def __init__(self
, canvas
, *args
, **kw
):
111 apply(CanvasItem
.__init
__, (self
, canvas
, 'text') + args
, kw
)
113 class Window(CanvasItem
):
114 def __init__(self
, canvas
, *args
, **kw
):
115 apply(CanvasItem
.__init
__, (self
, canvas
, 'window') + args
, kw
)
118 def __init__(self
, canvas
, tag
=None):
120 tag
= 'Group%d' % id(self
)
121 self
.tag
= self
.id = tag
123 self
.canvas
.dtag(self
.tag
)
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
)
144 return self
.canvas
._getints
(self
._do
('bbox'))
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
, '')
149 def coords(self
, *pts
):
150 return self
._do
('coords', pts
)
151 def dchars(self
, first
, last
=None):
152 self
._do
('dchars', first
, last
)
155 def dtag(self
, tagToDelete
=None):
156 self
._do
('dtag', tagToDelete
)
160 return self
.canvas
.tk
.splitlist(self
._do
('gettags', self
.tag
))
161 def icursor(self
, index
):
162 return self
._do
('icursor', index
)
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
)
167 def config(self
, cnf
={}, **kw
):
168 return self
.canvas
.itemconfigure(self
.tag
, _cnfmerge((cnf
,kw
)))
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
)
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
))
185 return self
._do
('type')