1 # This module exports classes for the various canvas item types
3 from Tkinter
import Canvas
, _isfunctype
, _flatten
6 DictionaryType
= type({})
9 def __init__(self
, canvas
, itemType
, *args
):
11 self
.id = canvas
._create
(itemType
, args
)
12 if not hasattr(canvas
, 'items'):
14 canvas
.items
[self
.id] = self
18 return '<%s, id=%d>' % (self
.__class
__.__name
__, self
.id)
20 del self
.canvas
.items
[self
.id]
21 self
.canvas
.delete(self
.id)
22 def __getitem__(self
, key
):
23 v
= self
.canvas
.tk
.split(self
.canvas
.tk
.call(
24 self
.canvas
._w
, 'itemconfigure',
27 def __setitem__(self
, key
, value
):
28 self
.canvas
._itemconfig
(self
.id, {key
: value
})
30 if not hasattr(self
, '_keys'):
31 self
._keys
= map(lambda x
, tk
=self
.canvas
.tk
:
32 tk
.splitlist(x
)[0][1:],
33 self
.canvas
.tk
.splitlist(
38 def has_key(self
, key
):
39 return key
in self
.keys()
40 def addtag(self
, tag
, option
='withtag'):
41 self
.canvas
.addtag(tag
, option
, self
.id)
43 x1
, y1
, x2
, y2
= self
.canvas
.bbox(self
.id)
44 return (x1
, y1
), (x2
, y2
)
45 def bind(self
, sequence
=None, command
=None):
46 return self
.canvas
.bind(self
.id, sequence
, command
)
47 def config(self
, cnf
=None):
48 return self
.canvas
.itemconfig(self
.id, cnf
)
49 def coords(self
, pts
= ()):
51 for x
, y
in pts
: flat
= flat
+ (x
, y
)
52 return apply(self
.canvas
.coords
, (self
.id,) + flat
)
53 def dchars(self
, first
, last
=None):
54 self
.canvas
.dchars(self
.id, first
, last
)
56 self
.canvas
.dtag(self
.id, ttd
)
58 self
.canvas
.focus(self
.id)
60 return self
.canvas
.gettags(self
.id)
62 self
.canvas
.icursor(self
.id)
64 return self
.canvas
.index(self
.id)
65 def insert(self
, beforethis
, string
):
66 self
.canvas
.insert(self
.id, beforethis
, string
)
67 def lower(self
, belowthis
=None):
68 self
.canvas
.lower(self
.id, belowthis
)
69 def move(self
, xamount
, yamount
):
70 self
.canvas
.move(self
.id, xamount
, yamount
)
71 def raise_(self
, abovethis
=None):
72 self
.canvas
.raise_(self
.id, abovethis
)
73 def scale(self
, xorigin
, yorigin
, xscale
, yscale
):
74 self
.canvas
.scale(self
.id, xorigin
, yorigin
, xscale
, yscale
)
76 return self
.canvas
.type(self
.id)
78 class Arc(CanvasItem
):
79 def __init__(self
, canvas
, *args
):
80 CanvasItem
.__init
__(self
, canvas
, 'arc', args
)
82 class Bitmap(CanvasItem
):
83 def __init__(self
, canvas
, *args
):
84 CanvasItem
.__init
__(self
, canvas
, 'bitmap', args
)
86 class Line(CanvasItem
):
87 def __init__(self
, canvas
, *args
):
88 CanvasItem
.__init
__(self
, canvas
, 'line', args
)
90 class Oval(CanvasItem
):
91 def __init__(self
, canvas
, *args
):
92 CanvasItem
.__init
__(self
, canvas
, 'oval', args
)
94 class Polygon(CanvasItem
):
95 def __init__(self
, canvas
, *args
):
96 CanvasItem
.__init
__(self
, canvas
, 'polygon', args
)
98 class Rectangle(CanvasItem
):
99 def __init__(self
, canvas
, *args
):
100 CanvasItem
.__init
__(self
, canvas
, 'rectangle', args
)
102 # XXX "Text" is taken by the Text widget...
103 class CanvasText(CanvasItem
):
104 def __init__(self
, canvas
, *args
):
105 CanvasItem
.__init
__(self
, canvas
, 'text', args
)
107 class Window(CanvasItem
):
108 def __init__(self
, canvas
, *args
):
109 CanvasItem
.__init
__(self
, canvas
, 'window', args
)
112 def __init__(self
, canvas
, tag
=None):
114 tag
= 'Group%d' % id(self
)
115 self
.tag
= self
.id = tag
117 self
.canvas
.dtag(self
.tag
)
120 def _do(self
, cmd
, *args
):
121 return self
.canvas
._do
(cmd
, (self
.tag
,) + _flatten(args
))
122 def addtag_above(self
, tagOrId
):
123 self
._do
('addtag', 'above', tagOrId
)
124 def addtag_all(self
):
125 self
._do
('addtag', 'all')
126 def addtag_below(self
, tagOrId
):
127 self
._do
('addtag', 'below', tagOrId
)
128 def addtag_closest(self
, x
, y
, halo
=None, start
=None):
129 self
._do
('addtag', 'closest', x
, y
, halo
, start
)
130 def addtag_enclosed(self
, x1
, y1
, x2
, y2
):
131 self
._do
('addtag', 'enclosed', x1
, y1
, x2
, y2
)
132 def addtag_overlapping(self
, x1
, y1
, x2
, y2
):
133 self
._do
('addtag', 'overlapping', x1
, y1
, x2
, y2
)
134 def addtag_withtag(self
, tagOrId
):
135 self
._do
('addtag', 'withtag', tagOrId
)
137 return self
._getints
(self
._do
('bbox'))
138 def bind(self
, sequence
=None, command
=None): # XXX args?
139 return self
._do
('bind', sequence
, command
)
140 def coords(self
, *pts
):
141 return self
._do
('coords', pts
)
142 def dchars(self
, first
, last
=None):
143 self
._do
('dchars', first
, last
)
146 def dtag(self
, tagToDelete
=None):
147 self
._do
('dtag', tagToDelete
)
151 return self
.canvas
.tk
.splitlist(self
._do
('gettags', self
.tag
))
152 def icursor(self
, index
):
153 return self
._do
('icursor')
154 def index(self
, index
):
155 return self
.canvas
.tk
.getint(self
._do
('index', index
))
156 def insert(self
, beforeThis
, string
):
157 self
._do
('insert', beforeThis
, string
)
158 def config(self
, cnf
=None):
159 return self
.canvas
.itemconfigure(self
.tag
, cnf
)
160 def lower(self
, belowThis
=None):
161 self
._do
('lower', belowThis
)
162 def move(self
, xAmount
, yAmount
):
163 self
._do
('move', xAmount
, yAmount
)
164 def tkraise(self
, aboveThis
=None):
165 self
._do
('raise', aboveThis
)
167 def scale(self
, xOrigin
, yOrigin
, xScale
, yScale
):
168 self
._do
('scale', xOrigin
, yOrigin
, xScale
, yScale
)
169 def select_adjust(self
, index
):
170 self
.canvas
._do
('select', ('adjust', self
.tag
, index
))
171 def select_from(self
, index
):
172 self
.canvas
._do
('select', ('from', self
.tag
, index
))
173 def select_to(self
, index
):
174 self
.canvas
._do
('select', ('to', self
.tag
, index
))
176 return self
._do
('type')