blob: 4d21928cb8cc51a2c709f54833f7002e6dece43d [file] [log] [blame]
Guido van Rossum18468821994-06-20 07:49:28 +00001# This module exports classes for the various canvas item types
2
3# vi:set tabsize=4:
4
5from Tkinter import Canvas, _isfunctype
6
7class CanvasItem:
8 def __init__(self, canvas, itemType, args = (), cnf={}):
9 self.canvas = canvas
10 self.id = canvas._create(itemType, args + (cnf,))
11 def __str__(self):
12 return str(self.id)
13 def __repr__(self):
14 return '<%s, id=%d>' % (self.__class__.__name__, self.id)
15 def __del__(self):
16 self.canvas.delete(self.id)
17 delete = __del__
18 def __getitem__(self, key):
19 v = self.canvas.tk.split(self.canvas.tk.call(self.canvas.pathName,
20 'itemconfigure',
21 str(self.id),
22 '-' + key))
23 return v[4]
24 def __setitem__(self, key, value):
25 self.canvas._itemconfig(self.id, {key: value})
26 def keys(self):
27 if not hasattr(self, '_keys'):
28 self._keys = map(lambda x, tk=self.canvas.tk:
29 tk.splitlist(x)[0][1:],
30 self.canvas._splitlist(
31 self.canvas.cmd('itemconfigure', self.id)))
32 return self._keys
33 def has_key(self, key):
34 return key in self.keys()
35 def addtag(self, tag, option='withtag'):
36 self.canvas.addtag(tag, option, self.id)
37 def bbox(self):
38 x1, y1, x2, y2 = self.canvas.bbox(self.id)
39 return (x1, y1), (x2, y2)
40 def bind(self, sequence=None, command=None):
41 return self.canvas.bind(self.id, sequence, command)
42 def coords(self, pts = ()):
43 flat = ()
44 for x, y in pts: flat = flat + (x, y)
45 return apply(self.canvas.coords, (self.id,) + flat)
46 def dchars(self, first, last=None):
47 self.canvas.dchars(self.id, first, last)
48 def dtag(self, ttd):
49 self.canvas.dtag(self.id, ttd)
50 def focus(self):
51 self.canvas.focus(self.id)
52 def gettags(self):
53 return self.canvas.gettags(self.id)
54 def icursor(self):
55 self.canvas.icursor(self.id)
56 def index(self):
57 return self.canvas.index(self.id)
58 def insert(self, beforethis, string):
59 self.canvas.insert(self.id, beforethis, string)
60 def lower(self, belowthis=None):
61 self.canvas.lower(self.id, belowthis)
62 def move(self, xamount, yamount):
63 self.canvas.move(self.id, xamount, yamount)
64 def raise_(self, abovethis=None):
65 self.canvas.raise_(self.id, abovethis)
66 def scale(self, xorigin, yorigin, xscale, yscale):
67 self.canvas.scale(self.id, xorigin, yorigin, xscale, yscale)
68 def type(self):
69 return self.canvas.type(self.id)
70
71class Arc(CanvasItem):
72 def __init__(self, canvas, (x1, y1), (x2, y2), cnf={}):
73 CanvasItem.__init__(self, canvas, 'arc',
74 (str(x1), str(y1), str(x2), str(y2)), cnf)
75
76class Bitmap(CanvasItem):
77 def __init__(self, canvas, (x1, y1), cnf={}):
78 CanvasItem.__init__(self, canvas, 'bitmap', (str(x1), str(y1)), cnf)
79
80class Line(CanvasItem):
81 def __init__(self, canvas, pts, cnf={}):
82 pts = reduce(lambda a, b: a+b,
83 map(lambda pt: (str(pt[0]), str(pt[1])), pts))
84 CanvasItem.__init__(self, canvas, 'line', pts, cnf)
85
86class Oval(CanvasItem):
87 def __init__(self, canvas, (x1, y1), (x2, y2), cnf={}):
88 CanvasItem.__init__(self, canvas, 'oval',
89 (str(x1), str(y1), str(x2), str(y2)), cnf)
90
91class Polygon(CanvasItem):
92 def __init__(self, canvas, pts, cnf={}):
93 pts = reduce(lambda a, b: a+b,
94 map(lambda pt: (str(pt[0]), str(pt[1])), pts))
95 CanvasItem.__init__(self, canvas, 'polygon', pts, cnf)
96
97class Curve(Polygon):
98 def __init__(self, canvas, pts, cnf={}):
99 cnf['smooth'] = 'yes'
100 Polygon.__init__(self, canvas, pts, cnf)
101
102class Rectangle(CanvasItem):
103 def __init__(self, canvas, (x1, y1), (x2, y2), cnf={}):
104 CanvasItem.__init__(self, canvas, 'rectangle',
105 (str(x1), str(y1), str(x2), str(y2)), cnf)
106
107# XXX Can't use name "Text" since that is already taken by the Text widget...
108class String(CanvasItem):
109 def __init__(self, canvas, (x1, y1), cnf={}):
110 CanvasItem.__init__(self, canvas, 'text', (str(x1), str(y1)), cnf)
111
112class Window(CanvasItem):
113 def __init__(self, canvas, where, cnf={}):
114 CanvasItem.__init__(self, canvas, 'window',
115 (str(where[0]), str(where[1])), cnf)