annotate Lib/lib-tk/turtle.py @ 27998:e27f58a50737 legacy-trunk

SF bug #690377: Dud footnote reference in distutils docs
author Raymond Hettinger <python@rcn.com>
date Fri, 21 Feb 2003 02:47:09 +0000
parents bbf8cf5ee9f9
children 90c77b8af1a4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # LogoMation-like turtle graphics
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 from math import * # Also for export
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 import Tkinter
25360
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
5
14957
db1cce1ffb60 Don't rename Tkinter to Tk; closes bug 115714
Martin v. Löwis <martin@v.loewis.de>
parents: 9585
diff changeset
6 class Error(Exception):
db1cce1ffb60 Don't rename Tkinter to Tk; closes bug 115714
Martin v. Löwis <martin@v.loewis.de>
parents: 9585
diff changeset
7 pass
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 class RawPen:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
10
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 def __init__(self, canvas):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 self._canvas = canvas
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 self._items = []
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 self._tracing = 1
19100
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
15 self._arrow = 0
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 self.degrees()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 self.reset()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
18
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 def degrees(self, fullcircle=360.0):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 self._fullcircle = fullcircle
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 self._invradian = pi / (fullcircle * 0.5)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
22
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 def radians(self):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 self.degrees(2.0*pi)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 def reset(self):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 canvas = self._canvas
25340
27ff694c8c05 Update Canvas before computing width. Draw turtle at end of drawing circle.
Martin v. Löwis <martin@v.loewis.de>
parents: 22842
diff changeset
28 self._canvas.update()
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 width = canvas.winfo_width()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 height = canvas.winfo_height()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 if width <= 1:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 width = canvas['width']
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 if height <= 1:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 height = canvas['height']
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 self._origin = float(width)/2.0, float(height)/2.0
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 self._position = self._origin
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 self._angle = 0.0
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 self._drawing = 1
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 self._width = 1
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 self._color = "black"
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 self._filling = 0
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 self._path = []
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 self._tofill = []
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 self.clear()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 canvas._root().tkraise()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
46
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 def clear(self):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 self.fill(0)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 canvas = self._canvas
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 items = self._items
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 self._items = []
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 for item in items:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 canvas.delete(item)
19100
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
54 self._delete_turtle()
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
55 self._draw_turtle()
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
56
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 def tracer(self, flag):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 self._tracing = flag
19100
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
59 if not self._tracing:
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
60 self._delete_turtle()
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
61 self._draw_turtle()
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
62
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 def forward(self, distance):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 x0, y0 = start = self._position
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 x1 = x0 + distance * cos(self._angle*self._invradian)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
66 y1 = y0 - distance * sin(self._angle*self._invradian)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 self._goto(x1, y1)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 def backward(self, distance):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 self.forward(-distance)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
71
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 def left(self, angle):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 self._angle = (self._angle + angle) % self._fullcircle
19100
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
74 self._draw_turtle()
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
75
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 def right(self, angle):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 self.left(-angle)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
78
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
79 def up(self):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 self._drawing = 0
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
81
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 def down(self):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 self._drawing = 1
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
84
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 def width(self, width):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 self._width = float(width)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
87
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 def color(self, *args):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 if not args:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 raise Error, "no color arguments"
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 if len(args) == 1:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 color = args[0]
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 if type(color) == type(""):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 # Test the color first
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 try:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 id = self._canvas.create_line(0, 0, 0, 0, fill=color)
14957
db1cce1ffb60 Don't rename Tkinter to Tk; closes bug 115714
Martin v. Löwis <martin@v.loewis.de>
parents: 9585
diff changeset
97 except Tkinter.TclError:
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 raise Error, "bad color string: %s" % `color`
19100
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
99 self._set_color(color)
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 return
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 try:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
102 r, g, b = color
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
103 except:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
104 raise Error, "bad color sequence: %s" % `color`
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
105 else:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
106 try:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
107 r, g, b = args
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
108 except:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
109 raise Error, "bad color arguments: %s" % `args`
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
110 assert 0 <= r <= 1
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 assert 0 <= g <= 1
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 assert 0 <= b <= 1
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
113 x = 255.0
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
114 y = 0.5
19100
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
115 self._set_color("#%02x%02x%02x" % (int(r*x+y), int(g*x+y), int(b*x+y)))
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
116
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
117 def _set_color(self,color):
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
118 self._color = color
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
119 self._draw_turtle()
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
120
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
121 def write(self, arg, move=0):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
122 x, y = start = self._position
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
123 x = x-1 # correction -- calibrated for Windows
15308
847ae40074d4 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 14957
diff changeset
124 item = self._canvas.create_text(x, y,
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
125 text=str(arg), anchor="sw",
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
126 fill=self._color)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
127 self._items.append(item)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
128 if move:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 x0, y0, x1, y1 = self._canvas.bbox(item)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
130 self._goto(x1, y1)
19100
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
131 self._draw_turtle()
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
132
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
133 def fill(self, flag):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
134 if self._filling:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
135 path = tuple(self._path)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
136 smooth = self._filling < 0
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 if len(path) > 2:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 item = self._canvas._create('polygon', path,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 {'fill': self._color,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 'smooth': smooth})
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 self._items.append(item)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
142 self._canvas.lower(item)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 if self._tofill:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
144 for item in self._tofill:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
145 self._canvas.itemconfigure(item, fill=self._color)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
146 self._items.append(item)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
147 self._path = []
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 self._tofill = []
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 self._filling = flag
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 if flag:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 self._path.append(self._position)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
152
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
153 def circle(self, radius, extent=None):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
154 if extent is None:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 extent = self._fullcircle
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 x0, y0 = self._position
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
157 xc = x0 - radius * sin(self._angle * self._invradian)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 yc = y0 - radius * cos(self._angle * self._invradian)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 if radius >= 0.0:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 start = self._angle - 90.0
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 else:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 start = self._angle + 90.0
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 extent = -extent
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 if self._filling:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
165 if abs(extent) >= self._fullcircle:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 item = self._canvas.create_oval(xc-radius, yc-radius,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 xc+radius, yc+radius,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 width=self._width,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
169 outline="")
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
170 self._tofill.append(item)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
171 item = self._canvas.create_arc(xc-radius, yc-radius,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
172 xc+radius, yc+radius,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
173 style="chord",
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 start=start,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
175 extent=extent,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 width=self._width,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
177 outline="")
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
178 self._tofill.append(item)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
179 if self._drawing:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 if abs(extent) >= self._fullcircle:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 item = self._canvas.create_oval(xc-radius, yc-radius,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 xc+radius, yc+radius,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 width=self._width,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 outline=self._color)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 self._items.append(item)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
186 item = self._canvas.create_arc(xc-radius, yc-radius,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 xc+radius, yc+radius,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 style="arc",
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 start=start,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
190 extent=extent,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
191 width=self._width,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 outline=self._color)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 self._items.append(item)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 angle = start + extent
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 x1 = xc + abs(radius) * cos(angle * self._invradian)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 y1 = yc - abs(radius) * sin(angle * self._invradian)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 self._angle = (self._angle + extent) % self._fullcircle
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
198 self._position = x1, y1
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 if self._filling:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 self._path.append(self._position)
25340
27ff694c8c05 Update Canvas before computing width. Draw turtle at end of drawing circle.
Martin v. Löwis <martin@v.loewis.de>
parents: 22842
diff changeset
201 self._draw_turtle()
25479
bbf8cf5ee9f9 Whitespace normalization (get rid of tabs).
Guido van Rossum <guido@python.org>
parents: 25360
diff changeset
202
25360
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
203 def heading(self):
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
204 return self._angle
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
205
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
206 def setheading(self, angle):
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
207 self._angle = angle
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
208 self._draw_turtle()
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
209
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
210 def window_width(self):
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
211 width = self._canvas.winfo_width()
25479
bbf8cf5ee9f9 Whitespace normalization (get rid of tabs).
Guido van Rossum <guido@python.org>
parents: 25360
diff changeset
212 if width <= 1: # the window isn't managed by a geometry manager
25360
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
213 width = self._canvas['width']
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
214 return width
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
215
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
216 def window_height(self):
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
217 height = self._canvas.winfo_height()
25479
bbf8cf5ee9f9 Whitespace normalization (get rid of tabs).
Guido van Rossum <guido@python.org>
parents: 25360
diff changeset
218 if height <= 1: # the window isn't managed by a geometry manager
25360
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
219 height = self._canvas['height']
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
220 return height
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
221
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
222 def position(self):
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
223 x0, y0 = self._origin
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
224 x1, y1 = self._position
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
225 return [x1-x0, -y1+y0]
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
226
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
227 def setx(self, xpos):
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
228 x0, y0 = self._origin
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
229 x1, y1 = self._position
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
230 self._goto(x0+xpos, y1)
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
231
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
232 def sety(self, ypos):
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
233 x0, y0 = self._origin
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
234 x1, y1 = self._position
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
235 self._goto(x1, y0-ypos)
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
236
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
237 def goto(self, *args):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 if len(args) == 1:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
239 try:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
240 x, y = args[0]
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
241 except:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
242 raise Error, "bad point argument: %s" % `args[0]`
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
243 else:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
244 try:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
245 x, y = args
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
246 except:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
247 raise Error, "bad coordinates: %s" % `args[0]`
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
248 x0, y0 = self._origin
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
249 self._goto(x0+x, y0-y)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
250
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
251 def _goto(self, x1, y1):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
252 x0, y0 = start = self._position
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
253 self._position = map(float, (x1, y1))
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
254 if self._filling:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 self._path.append(self._position)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 if self._drawing:
25479
bbf8cf5ee9f9 Whitespace normalization (get rid of tabs).
Guido van Rossum <guido@python.org>
parents: 25360
diff changeset
257 if self._tracing:
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 dx = float(x1 - x0)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 dy = float(y1 - y0)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 distance = hypot(dx, dy)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 nhops = int(distance)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 item = self._canvas.create_line(x0, y0, x0, y0,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 width=self._width,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
264 capstyle="round",
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 fill=self._color)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 try:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 for i in range(1, 1+nhops):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 x, y = x0 + dx*i/nhops, y0 + dy*i/nhops
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 self._canvas.coords(item, x0, y0, x, y)
19100
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
270 self._draw_turtle((x,y))
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 self._canvas.update()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 self._canvas.after(10)
15681
3ca702caa61a Patch by kragen@pobox.com: When tracing is turned on, lines shorter
Guido van Rossum <guido@python.org>
parents: 15308
diff changeset
273 # in case nhops==0
3ca702caa61a Patch by kragen@pobox.com: When tracing is turned on, lines shorter
Guido van Rossum <guido@python.org>
parents: 15308
diff changeset
274 self._canvas.coords(item, x0, y0, x1, y1)
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
275 self._canvas.itemconfigure(item, arrow="none")
14957
db1cce1ffb60 Don't rename Tkinter to Tk; closes bug 115714
Martin v. Löwis <martin@v.loewis.de>
parents: 9585
diff changeset
276 except Tkinter.TclError:
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
277 # Probably the window was closed!
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
278 return
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
279 else:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
280 item = self._canvas.create_line(x0, y0, x1, y1,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
281 width=self._width,
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
282 capstyle="round",
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
283 fill=self._color)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
284 self._items.append(item)
19100
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
285 self._draw_turtle()
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
286
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
287 def _draw_turtle(self,position=[]):
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
288 if not self._tracing:
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
289 return
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
290 if position == []:
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
291 position = self._position
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
292 x,y = position
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
293 distance = 8
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
294 dx = distance * cos(self._angle*self._invradian)
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
295 dy = distance * sin(self._angle*self._invradian)
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
296 self._delete_turtle()
22842
079abe05e8bc backport loewis' checkin of
Michael W. Hudson <mwh@python.net>
parents: 19100
diff changeset
297 self._arrow = self._canvas.create_line(x-dx,y+dy,x,y,
19100
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
298 width=self._width,
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
299 arrow="last",
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
300 capstyle="round",
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
301 fill=self._color)
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
302 self._canvas.update()
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
303
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
304 def _delete_turtle(self):
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
305 if self._arrow != 0:
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
306 self._canvas.delete(self._arrow)
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
307 self._arrow = 0
c238745e0aae Applied SF patch #438424 by Josh Cogliati:
Guido van Rossum <guido@python.org>
parents: 15681
diff changeset
308
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
309
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
310
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
311 _root = None
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
312 _canvas = None
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
313 _pen = None
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
314
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
315 class Pen(RawPen):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
316
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
317 def __init__(self):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
318 global _root, _canvas
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
319 if _root is None:
14957
db1cce1ffb60 Don't rename Tkinter to Tk; closes bug 115714
Martin v. Löwis <martin@v.loewis.de>
parents: 9585
diff changeset
320 _root = Tkinter.Tk()
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 _root.wm_protocol("WM_DELETE_WINDOW", self._destroy)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
322 if _canvas is None:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 # XXX Should have scroll bars
14957
db1cce1ffb60 Don't rename Tkinter to Tk; closes bug 115714
Martin v. Löwis <martin@v.loewis.de>
parents: 9585
diff changeset
324 _canvas = Tkinter.Canvas(_root, background="white")
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
325 _canvas.pack(expand=1, fill="both")
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
326 RawPen.__init__(self, _canvas)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
327
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
328 def _destroy(self):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
329 global _root, _canvas, _pen
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
330 root = self._canvas._root()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
331 if root is _root:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
332 _pen = None
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
333 _root = None
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
334 _canvas = None
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
335 root.destroy()
15308
847ae40074d4 Make reindent.py happy (convert everything to 4-space indents!).
Fred Drake <fdrake@acm.org>
parents: 14957
diff changeset
336
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
337
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
338 def _getpen():
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
339 global _pen
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
340 pen = _pen
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
341 if not pen:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
342 _pen = pen = Pen()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
343 return pen
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
344
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
345 def degrees(): _getpen().degrees()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
346 def radians(): _getpen().radians()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
347 def reset(): _getpen().reset()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
348 def clear(): _getpen().clear()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
349 def tracer(flag): _getpen().tracer(flag)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
350 def forward(distance): _getpen().forward(distance)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
351 def backward(distance): _getpen().backward(distance)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
352 def left(angle): _getpen().left(angle)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
353 def right(angle): _getpen().right(angle)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
354 def up(): _getpen().up()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
355 def down(): _getpen().down()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
356 def width(width): _getpen().width(width)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
357 def color(*args): apply(_getpen().color, args)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
358 def write(arg, move=0): _getpen().write(arg, move)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
359 def fill(flag): _getpen().fill(flag)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
360 def circle(radius, extent=None): _getpen().circle(radius, extent)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
361 def goto(*args): apply(_getpen().goto, args)
25360
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
362 def heading(): return _getpen().heading()
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
363 def setheading(angle): _getpen().setheading(angle)
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
364 def position(): return _getpen().position()
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
365 def window_width(): return _getpen().window_width()
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
366 def window_height(): return _getpen().window_height()
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
367 def setx(xpos): _getpen().setx(xpos)
5255b45fddc2 Add the bulk of SF patch 595111 by Attila Babo.
Guido van Rossum <guido@python.org>
parents: 25340
diff changeset
368 def sety(ypos): _getpen().sety(ypos)
9585
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
369
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
370 def demo():
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
371 reset()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
372 tracer(1)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
373 up()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
374 backward(100)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
375 down()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
376 # draw 3 squares; the last filled
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
377 width(3)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
378 for i in range(3):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
379 if i == 2:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
380 fill(1)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
381 for j in range(4):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
382 forward(20)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
383 left(90)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
384 if i == 2:
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
385 color("maroon")
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
386 fill(0)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
387 up()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
388 forward(30)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
389 down()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
390 width(1)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
391 color("black")
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
392 # move out of the way
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
393 tracer(0)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
394 up()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
395 right(90)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
396 forward(100)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
397 right(90)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
398 forward(100)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
399 right(180)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
400 down()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
401 # some text
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
402 write("startstart", 1)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
403 write("start", 1)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
404 color("red")
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
405 # staircase
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
406 for i in range(5):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
407 forward(20)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
408 left(90)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
409 forward(20)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
410 right(90)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
411 # filled staircase
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
412 fill(1)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
413 for i in range(5):
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
414 forward(20)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
415 left(90)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
416 forward(20)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
417 right(90)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
418 fill(0)
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
419 # more text
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
420 write("end")
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
421 if __name__ == '__main__':
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
422 _root.mainloop()
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
423
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
424 if __name__ == '__main__':
3c297a6ad4e8 Turtle graphics
Guido van Rossum <guido@python.org>
parents:
diff changeset
425 demo()