Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Lib / lib-tk / turtle.py
blob16e5735a845b2cb259f686b7f89491d59c87e1b4
1 # LogoMation-like turtle graphics
3 from math import * # Also for export
4 import Tkinter
6 class Error(Exception):
7 pass
9 class RawPen:
11 def __init__(self, canvas):
12 self._canvas = canvas
13 self._items = []
14 self._tracing = 1
15 self._arrow = 0
16 self.degrees()
17 self.reset()
19 def degrees(self, fullcircle=360.0):
20 self._fullcircle = fullcircle
21 self._invradian = pi / (fullcircle * 0.5)
23 def radians(self):
24 self.degrees(2.0*pi)
26 def reset(self):
27 canvas = self._canvas
28 self._canvas.update()
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()
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)
54 self._delete_turtle()
55 self._draw_turtle()
57 def tracer(self, flag):
58 self._tracing = flag
59 if not self._tracing:
60 self._delete_turtle()
61 self._draw_turtle()
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)
69 def backward(self, distance):
70 self.forward(-distance)
72 def left(self, angle):
73 self._angle = (self._angle + angle) % self._fullcircle
74 self._draw_turtle()
76 def right(self, angle):
77 self.left(-angle)
79 def up(self):
80 self._drawing = 0
82 def down(self):
83 self._drawing = 1
85 def width(self, width):
86 self._width = float(width)
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)
97 except Tkinter.TclError:
98 raise Error, "bad color string: %s" % `color`
99 self._set_color(color)
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
115 self._set_color("#%02x%02x%02x" % (int(r*x+y), int(g*x+y), int(b*x+y)))
117 def _set_color(self,color):
118 self._color = color
119 self._draw_turtle()
121 def write(self, arg, move=0):
122 x, y = start = self._position
123 x = x-1 # correction -- calibrated for Windows
124 item = self._canvas.create_text(x, y,
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)
131 self._draw_turtle()
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)
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)
201 self._draw_turtle()
203 def heading(self):
204 return self._angle
206 def setheading(self, angle):
207 self._angle = angle
208 self._draw_turtle()
210 def window_width(self):
211 width = self._canvas.winfo_width()
212 if width <= 1: # the window isn't managed by a geometry manager
213 width = self._canvas['width']
214 return width
216 def window_height(self):
217 height = self._canvas.winfo_height()
218 if height <= 1: # the window isn't managed by a geometry manager
219 height = self._canvas['height']
220 return height
222 def position(self):
223 x0, y0 = self._origin
224 x1, y1 = self._position
225 return [x1-x0, -y1+y0]
227 def setx(self, xpos):
228 x0, y0 = self._origin
229 x1, y1 = self._position
230 self._goto(x0+xpos, y1)
232 def sety(self, ypos):
233 x0, y0 = self._origin
234 x1, y1 = self._position
235 self._goto(x1, y0-ypos)
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)
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:
257 if self._tracing:
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)
270 self._draw_turtle((x,y))
271 self._canvas.update()
272 self._canvas.after(10)
273 # in case nhops==0
274 self._canvas.coords(item, x0, y0, x1, y1)
275 self._canvas.itemconfigure(item, arrow="none")
276 except Tkinter.TclError:
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)
285 self._draw_turtle()
287 def _draw_turtle(self,position=[]):
288 if not self._tracing:
289 return
290 if position == []:
291 position = self._position
292 x,y = position
293 distance = 8
294 dx = distance * cos(self._angle*self._invradian)
295 dy = distance * sin(self._angle*self._invradian)
296 self._delete_turtle()
297 self._arrow = self._canvas.create_line(x-dx,y+dy,x,y,
298 width=self._width,
299 arrow="last",
300 capstyle="round",
301 fill=self._color)
302 self._canvas.update()
304 def _delete_turtle(self):
305 if self._arrow != 0:
306 self._canvas.delete(self._arrow)
307 self._arrow = 0
311 _root = None
312 _canvas = None
313 _pen = None
315 class Pen(RawPen):
317 def __init__(self):
318 global _root, _canvas
319 if _root is None:
320 _root = Tkinter.Tk()
321 _root.wm_protocol("WM_DELETE_WINDOW", self._destroy)
322 if _canvas is None:
323 # XXX Should have scroll bars
324 _canvas = Tkinter.Canvas(_root, background="white")
325 _canvas.pack(expand=1, fill="both")
326 RawPen.__init__(self, _canvas)
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()
338 def _getpen():
339 global _pen
340 pen = _pen
341 if not pen:
342 _pen = pen = Pen()
343 return pen
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)
362 def heading(): return _getpen().heading()
363 def setheading(angle): _getpen().setheading(angle)
364 def position(): return _getpen().position()
365 def window_width(): return _getpen().window_width()
366 def window_height(): return _getpen().window_height()
367 def setx(xpos): _getpen().setx(xpos)
368 def sety(ypos): _getpen().sety(ypos)
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()
424 if __name__ == '__main__':
425 demo()