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