1 # This module exports classes for the various canvas item types
3 # NOTE: This module was an experiment and is now obsolete.
4 # It's best to use the Tkinter.Canvas class directly.
6 from Tkinter
import Canvas
, _cnfmerge
, _flatten
10 def __init__(self
, canvas
, itemType
, *args
, **kw
):
12 self
.id = canvas
._create
(itemType
, args
, kw
)
13 if not hasattr(canvas
, 'items'):
15 canvas
.items
[self
.id] = self
19 return '<%s, id=%d>' % (self
.__class
__.__name
__, self
.id)
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',
29 def __setitem__(self
, key
, value
):
30 self
.canvas
.itemconfig(self
.id, {key
: value
})
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(
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)
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
= ()):
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
)
62 self
.canvas
.dtag(self
.id, ttd
)
64 self
.canvas
.focus(self
.id)
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
)
83 return self
.canvas
.type(self
.id)
85 class Arc(CanvasItem
):
86 def __init__(self
, canvas
, *args
, **kw
):
87 CanvasItem
.__init
__(self
, canvas
, 'arc', *args
, **kw
)
89 class Bitmap(CanvasItem
):
90 def __init__(self
, canvas
, *args
, **kw
):
91 CanvasItem
.__init
__(self
, canvas
, 'bitmap', *args
, **kw
)
93 class ImageItem(CanvasItem
):
94 def __init__(self
, canvas
, *args
, **kw
):
95 CanvasItem
.__init
__(self
, canvas
, 'image', *args
, **kw
)
97 class Line(CanvasItem
):
98 def __init__(self
, canvas
, *args
, **kw
):
99 CanvasItem
.__init
__(self
, canvas
, 'line', *args
, **kw
)
101 class Oval(CanvasItem
):
102 def __init__(self
, canvas
, *args
, **kw
):
103 CanvasItem
.__init
__(self
, canvas
, 'oval', *args
, **kw
)
105 class Polygon(CanvasItem
):
106 def __init__(self
, canvas
, *args
, **kw
):
107 CanvasItem
.__init
__(self
, canvas
, 'polygon', *args
, **kw
)
109 class Rectangle(CanvasItem
):
110 def __init__(self
, canvas
, *args
, **kw
):
111 CanvasItem
.__init
__(self
, canvas
, 'rectangle', *args
, **kw
)
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
)
118 class Window(CanvasItem
):
119 def __init__(self
, canvas
, *args
, **kw
):
120 CanvasItem
.__init
__(self
, canvas
, 'window', *args
, **kw
)
123 def __init__(self
, canvas
, tag
=None):
125 tag
= 'Group%d' % id(self
)
126 self
.tag
= self
.id = tag
128 self
.canvas
.dtag(self
.tag
)
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
)
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
)
160 def dtag(self
, tagToDelete
=None):
161 self
._do
('dtag', tagToDelete
)
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
)
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
))
190 return self
._do
('type')