Mercurial > cpython-lru_cache-weakref
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 |
| rev | line source |
|---|---|
| 9585 | 1 # LogoMation-like turtle graphics |
| 2 | |
| 3 from math import * # Also for export | |
| 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 | 8 |
| 9 class RawPen: | |
| 10 | |
| 11 def __init__(self, canvas): | |
| 12 self._canvas = canvas | |
| 13 self._items = [] | |
| 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 | 16 self.degrees() |
| 17 self.reset() | |
| 18 | |
| 19 def degrees(self, fullcircle=360.0): | |
| 20 self._fullcircle = fullcircle | |
| 21 self._invradian = pi / (fullcircle * 0.5) | |
| 22 | |
| 23 def radians(self): | |
| 24 self.degrees(2.0*pi) | |
| 25 | |
| 26 def reset(self): | |
| 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 | 29 width = canvas.winfo_width() |
| 30 height = canvas.winfo_height() | |
| 31 if width <= 1: | |
| 32 width = canvas['width'] | |
| 33 if height <= 1: | |
| 34 height = canvas['height'] | |
| 35 self._origin = float(width)/2.0, float(height)/2.0 | |
| 36 self._position = self._origin | |
| 37 self._angle = 0.0 | |
| 38 self._drawing = 1 | |
| 39 self._width = 1 | |
| 40 self._color = "black" | |
| 41 self._filling = 0 | |
| 42 self._path = [] | |
| 43 self._tofill = [] | |
| 44 self.clear() | |
| 45 canvas._root().tkraise() | |
| 46 | |
| 47 def clear(self): | |
| 48 self.fill(0) | |
| 49 canvas = self._canvas | |
| 50 items = self._items | |
| 51 self._items = [] | |
| 52 for item in items: | |
| 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 | 57 def tracer(self, flag): |
| 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 | 62 |
| 63 def forward(self, distance): | |
| 64 x0, y0 = start = self._position | |
| 65 x1 = x0 + distance * cos(self._angle*self._invradian) | |
| 66 y1 = y0 - distance * sin(self._angle*self._invradian) | |
| 67 self._goto(x1, y1) | |
| 68 | |
| 69 def backward(self, distance): | |
| 70 self.forward(-distance) | |
| 71 | |
| 72 def left(self, angle): | |
| 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 | 75 |
| 76 def right(self, angle): | |
| 77 self.left(-angle) | |
| 78 | |
| 79 def up(self): | |
| 80 self._drawing = 0 | |
| 81 | |
| 82 def down(self): | |
| 83 self._drawing = 1 | |
| 84 | |
| 85 def width(self, width): | |
| 86 self._width = float(width) | |
| 87 | |
| 88 def color(self, *args): | |
| 89 if not args: | |
| 90 raise Error, "no color arguments" | |
| 91 if len(args) == 1: | |
| 92 color = args[0] | |
| 93 if type(color) == type(""): | |
| 94 # Test the color first | |
| 95 try: | |
| 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 | 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 | 100 return |
| 101 try: | |
| 102 r, g, b = color | |
| 103 except: | |
| 104 raise Error, "bad color sequence: %s" % `color` | |
| 105 else: | |
| 106 try: | |
| 107 r, g, b = args | |
| 108 except: | |
| 109 raise Error, "bad color arguments: %s" % `args` | |
| 110 assert 0 <= r <= 1 | |
| 111 assert 0 <= g <= 1 | |
| 112 assert 0 <= b <= 1 | |
| 113 x = 255.0 | |
| 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 | 121 def write(self, arg, move=0): |
| 122 x, y = start = self._position | |
| 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 | 125 text=str(arg), anchor="sw", |
| 126 fill=self._color) | |
| 127 self._items.append(item) | |
| 128 if move: | |
| 129 x0, y0, x1, y1 = self._canvas.bbox(item) | |
| 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 | 132 |
| 133 def fill(self, flag): | |
| 134 if self._filling: | |
| 135 path = tuple(self._path) | |
| 136 smooth = self._filling < 0 | |
| 137 if len(path) > 2: | |
| 138 item = self._canvas._create('polygon', path, | |
| 139 {'fill': self._color, | |
| 140 'smooth': smooth}) | |
| 141 self._items.append(item) | |
| 142 self._canvas.lower(item) | |
| 143 if self._tofill: | |
| 144 for item in self._tofill: | |
| 145 self._canvas.itemconfigure(item, fill=self._color) | |
| 146 self._items.append(item) | |
| 147 self._path = [] | |
| 148 self._tofill = [] | |
| 149 self._filling = flag | |
| 150 if flag: | |
| 151 self._path.append(self._position) | |
| 152 | |
| 153 def circle(self, radius, extent=None): | |
| 154 if extent is None: | |
| 155 extent = self._fullcircle | |
| 156 x0, y0 = self._position | |
| 157 xc = x0 - radius * sin(self._angle * self._invradian) | |
| 158 yc = y0 - radius * cos(self._angle * self._invradian) | |
| 159 if radius >= 0.0: | |
| 160 start = self._angle - 90.0 | |
| 161 else: | |
| 162 start = self._angle + 90.0 | |
| 163 extent = -extent | |
| 164 if self._filling: | |
| 165 if abs(extent) >= self._fullcircle: | |
| 166 item = self._canvas.create_oval(xc-radius, yc-radius, | |
| 167 xc+radius, yc+radius, | |
| 168 width=self._width, | |
| 169 outline="") | |
| 170 self._tofill.append(item) | |
| 171 item = self._canvas.create_arc(xc-radius, yc-radius, | |
| 172 xc+radius, yc+radius, | |
| 173 style="chord", | |
| 174 start=start, | |
| 175 extent=extent, | |
| 176 width=self._width, | |
| 177 outline="") | |
| 178 self._tofill.append(item) | |
| 179 if self._drawing: | |
| 180 if abs(extent) >= self._fullcircle: | |
| 181 item = self._canvas.create_oval(xc-radius, yc-radius, | |
| 182 xc+radius, yc+radius, | |
| 183 width=self._width, | |
| 184 outline=self._color) | |
| 185 self._items.append(item) | |
| 186 item = self._canvas.create_arc(xc-radius, yc-radius, | |
| 187 xc+radius, yc+radius, | |
| 188 style="arc", | |
| 189 start=start, | |
| 190 extent=extent, | |
| 191 width=self._width, | |
| 192 outline=self._color) | |
| 193 self._items.append(item) | |
| 194 angle = start + extent | |
| 195 x1 = xc + abs(radius) * cos(angle * self._invradian) | |
| 196 y1 = yc - abs(radius) * sin(angle * self._invradian) | |
| 197 self._angle = (self._angle + extent) % self._fullcircle | |
| 198 self._position = x1, y1 | |
| 199 if self._filling: | |
| 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 | 236 |
| 237 def goto(self, *args): | |
| 238 if len(args) == 1: | |
| 239 try: | |
| 240 x, y = args[0] | |
| 241 except: | |
| 242 raise Error, "bad point argument: %s" % `args[0]` | |
| 243 else: | |
| 244 try: | |
| 245 x, y = args | |
| 246 except: | |
| 247 raise Error, "bad coordinates: %s" % `args[0]` | |
| 248 x0, y0 = self._origin | |
| 249 self._goto(x0+x, y0-y) | |
| 250 | |
| 251 def _goto(self, x1, y1): | |
| 252 x0, y0 = start = self._position | |
| 253 self._position = map(float, (x1, y1)) | |
| 254 if self._filling: | |
| 255 self._path.append(self._position) | |
| 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 | 258 dx = float(x1 - x0) |
| 259 dy = float(y1 - y0) | |
| 260 distance = hypot(dx, dy) | |
| 261 nhops = int(distance) | |
| 262 item = self._canvas.create_line(x0, y0, x0, y0, | |
| 263 width=self._width, | |
| 264 capstyle="round", | |
| 265 fill=self._color) | |
| 266 try: | |
| 267 for i in range(1, 1+nhops): | |
| 268 x, y = x0 + dx*i/nhops, y0 + dy*i/nhops | |
| 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 | 271 self._canvas.update() |
| 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 | 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 | 277 # Probably the window was closed! |
| 278 return | |
| 279 else: | |
| 280 item = self._canvas.create_line(x0, y0, x1, y1, | |
| 281 width=self._width, | |
| 282 capstyle="round", | |
| 283 fill=self._color) | |
| 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 | 309 |
| 310 | |
| 311 _root = None | |
| 312 _canvas = None | |
| 313 _pen = None | |
| 314 | |
| 315 class Pen(RawPen): | |
| 316 | |
| 317 def __init__(self): | |
| 318 global _root, _canvas | |
| 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 | 321 _root.wm_protocol("WM_DELETE_WINDOW", self._destroy) |
| 322 if _canvas is None: | |
| 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 | 325 _canvas.pack(expand=1, fill="both") |
| 326 RawPen.__init__(self, _canvas) | |
| 327 | |
| 328 def _destroy(self): | |
| 329 global _root, _canvas, _pen | |
| 330 root = self._canvas._root() | |
| 331 if root is _root: | |
| 332 _pen = None | |
| 333 _root = None | |
| 334 _canvas = None | |
| 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 | 337 |
| 338 def _getpen(): | |
| 339 global _pen | |
| 340 pen = _pen | |
| 341 if not pen: | |
| 342 _pen = pen = Pen() | |
| 343 return pen | |
| 344 | |
| 345 def degrees(): _getpen().degrees() | |
| 346 def radians(): _getpen().radians() | |
| 347 def reset(): _getpen().reset() | |
| 348 def clear(): _getpen().clear() | |
| 349 def tracer(flag): _getpen().tracer(flag) | |
| 350 def forward(distance): _getpen().forward(distance) | |
| 351 def backward(distance): _getpen().backward(distance) | |
| 352 def left(angle): _getpen().left(angle) | |
| 353 def right(angle): _getpen().right(angle) | |
| 354 def up(): _getpen().up() | |
| 355 def down(): _getpen().down() | |
| 356 def width(width): _getpen().width(width) | |
| 357 def color(*args): apply(_getpen().color, args) | |
| 358 def write(arg, move=0): _getpen().write(arg, move) | |
| 359 def fill(flag): _getpen().fill(flag) | |
| 360 def circle(radius, extent=None): _getpen().circle(radius, extent) | |
| 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 | 369 |
| 370 def demo(): | |
| 371 reset() | |
| 372 tracer(1) | |
| 373 up() | |
| 374 backward(100) | |
| 375 down() | |
| 376 # draw 3 squares; the last filled | |
| 377 width(3) | |
| 378 for i in range(3): | |
| 379 if i == 2: | |
| 380 fill(1) | |
| 381 for j in range(4): | |
| 382 forward(20) | |
| 383 left(90) | |
| 384 if i == 2: | |
| 385 color("maroon") | |
| 386 fill(0) | |
| 387 up() | |
| 388 forward(30) | |
| 389 down() | |
| 390 width(1) | |
| 391 color("black") | |
| 392 # move out of the way | |
| 393 tracer(0) | |
| 394 up() | |
| 395 right(90) | |
| 396 forward(100) | |
| 397 right(90) | |
| 398 forward(100) | |
| 399 right(180) | |
| 400 down() | |
| 401 # some text | |
| 402 write("startstart", 1) | |
| 403 write("start", 1) | |
| 404 color("red") | |
| 405 # staircase | |
| 406 for i in range(5): | |
| 407 forward(20) | |
| 408 left(90) | |
| 409 forward(20) | |
| 410 right(90) | |
| 411 # filled staircase | |
| 412 fill(1) | |
| 413 for i in range(5): | |
| 414 forward(20) | |
| 415 left(90) | |
| 416 forward(20) | |
| 417 right(90) | |
| 418 fill(0) | |
| 419 # more text | |
| 420 write("end") | |
| 421 if __name__ == '__main__': | |
| 422 _root.mainloop() | |
| 423 | |
| 424 if __name__ == '__main__': | |
| 425 demo() |
