home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / lib-tk / turtle.py < prev   
Encoding:
Python Source  |  2003-07-18  |  11.9 KB  |  386 lines

  1. # LogoMation-like turtle graphics
  2.  
  3. from math import * # Also for export
  4. import Tkinter
  5. class Error(Exception):
  6.     pass
  7.  
  8. class RawPen:
  9.  
  10.     def __init__(self, canvas):
  11.         self._canvas = canvas
  12.         self._items = []
  13.         self._tracing = 1
  14.         self._arrow = 0
  15.         self.degrees()
  16.         self.reset()
  17.  
  18.     def degrees(self, fullcircle=360.0):
  19.         self._fullcircle = fullcircle
  20.         self._invradian = pi / (fullcircle * 0.5)
  21.  
  22.     def radians(self):
  23.         self.degrees(2.0*pi)
  24.  
  25.     def reset(self):
  26.         canvas = self._canvas
  27.         self._canvas.update()
  28.         width = canvas.winfo_width()
  29.         height = canvas.winfo_height()
  30.         if width <= 1:
  31.             width = canvas['width']
  32.         if height <= 1:
  33.             height = canvas['height']
  34.         self._origin = float(width)/2.0, float(height)/2.0
  35.         self._position = self._origin
  36.         self._angle = 0.0
  37.         self._drawing = 1
  38.         self._width = 1
  39.         self._color = "black"
  40.         self._filling = 0
  41.         self._path = []
  42.         self._tofill = []
  43.         self.clear()
  44.         canvas._root().tkraise()
  45.  
  46.     def clear(self):
  47.         self.fill(0)
  48.         canvas = self._canvas
  49.         items = self._items
  50.         self._items = []
  51.         for item in items:
  52.             canvas.delete(item)
  53.         self._delete_turtle()
  54.         self._draw_turtle()
  55.  
  56.  
  57.     def tracer(self, flag):
  58.         self._tracing = flag
  59.         if not self._tracing:
  60.             self._delete_turtle()
  61.         self._draw_turtle()
  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
  74.         self._draw_turtle()
  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)
  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)))
  116.  
  117.     def _set_color(self,color):
  118.         self._color = color
  119.         self._draw_turtle()
  120.  
  121.  
  122.     def write(self, arg, move=0):
  123.         x, y = start = self._position
  124.         x = x-1 # correction -- calibrated for Windows
  125.         item = self._canvas.create_text(x, y,
  126.                                         text=str(arg), anchor="sw",
  127.                                         fill=self._color)
  128.         self._items.append(item)
  129.         if move:
  130.             x0, y0, x1, y1 = self._canvas.bbox(item)
  131.             self._goto(x1, y1)
  132.         self._draw_turtle()
  133.  
  134.     def fill(self, flag):
  135.         if self._filling:
  136.             path = tuple(self._path)
  137.             smooth = self._filling < 0
  138.             if len(path) > 2:
  139.                 item = self._canvas._create('polygon', path,
  140.                                             {'fill': self._color,
  141.                                              'smooth': smooth})
  142.                 self._items.append(item)
  143.                 self._canvas.lower(item)
  144.                 if self._tofill:
  145.                     for item in self._tofill:
  146.                         self._canvas.itemconfigure(item, fill=self._color)
  147.                         self._items.append(item)
  148.         self._path = []
  149.         self._tofill = []
  150.         self._filling = flag
  151.         if flag:
  152.             self._path.append(self._position)
  153.  
  154.     def circle(self, radius, extent=None):
  155.         if extent is None:
  156.             extent = self._fullcircle
  157.         x0, y0 = self._position
  158.         xc = x0 - radius * sin(self._angle * self._invradian)
  159.         yc = y0 - radius * cos(self._angle * self._invradian)
  160.         if radius >= 0.0:
  161.             start = self._angle - 90.0
  162.         else:
  163.             start = self._angle + 90.0
  164.             extent = -extent
  165.         if self._filling:
  166.             if abs(extent) >= self._fullcircle:
  167.                 item = self._canvas.create_oval(xc-radius, yc-radius,
  168.                                                 xc+radius, yc+radius,
  169.                                                 width=self._width,
  170.                                                 outline="")
  171.                 self._tofill.append(item)
  172.             item = self._canvas.create_arc(xc-radius, yc-radius,
  173.                                            xc+radius, yc+radius,
  174.                                            style="chord",
  175.                                            start=start,
  176.                                            extent=extent,
  177.                                            width=self._width,
  178.                                            outline="")
  179.             self._tofill.append(item)
  180.         if self._drawing:
  181.             if abs(extent) >= self._fullcircle:
  182.                 item = self._canvas.create_oval(xc-radius, yc-radius,
  183.                                                 xc+radius, yc+radius,
  184.                                                 width=self._width,
  185.                                                 outline=self._color)
  186.                 self._items.append(item)
  187.             item = self._canvas.create_arc(xc-radius, yc-radius,
  188.                                            xc+radius, yc+radius,
  189.                                            style="arc",
  190.                                            start=start,
  191.                                            extent=extent,
  192.                                            width=self._width,
  193.                                            outline=self._color)
  194.             self._items.append(item)
  195.         angle = start + extent
  196.         x1 = xc + abs(radius) * cos(angle * self._invradian)
  197.         y1 = yc - abs(radius) * sin(angle * self._invradian)
  198.         self._angle = (self._angle + extent) % self._fullcircle
  199.         self._position = x1, y1
  200.         if self._filling:
  201.             self._path.append(self._position)
  202.         self._draw_turtle()
  203.  
  204.     def goto(self, *args):
  205.         if len(args) == 1:
  206.             try:
  207.                 x, y = args[0]
  208.             except:
  209.                 raise Error, "bad point argument: %s" % `args[0]`
  210.         else:
  211.             try:
  212.                 x, y = args
  213.             except:
  214.                 raise Error, "bad coordinates: %s" % `args[0]`
  215.         x0, y0 = self._origin
  216.         self._goto(x0+x, y0-y)
  217.  
  218.     def _goto(self, x1, y1):
  219.         x0, y0 = start = self._position
  220.         self._position = map(float, (x1, y1))
  221.         if self._filling:
  222.             self._path.append(self._position)
  223.         if self._drawing:
  224.             if self._tracing:                
  225.                 dx = float(x1 - x0)
  226.                 dy = float(y1 - y0)
  227.                 distance = hypot(dx, dy)
  228.                 nhops = int(distance)
  229.                 item = self._canvas.create_line(x0, y0, x0, y0,
  230.                                                 width=self._width,
  231.                                                 capstyle="round",
  232.                                                 fill=self._color)
  233.                 try:
  234.                     for i in range(1, 1+nhops):
  235.                         x, y = x0 + dx*i/nhops, y0 + dy*i/nhops
  236.                         self._canvas.coords(item, x0, y0, x, y)
  237.                         self._draw_turtle((x,y))
  238.                         self._canvas.update()
  239.                         self._canvas.after(10)
  240.                     # in case nhops==0
  241.                     self._canvas.coords(item, x0, y0, x1, y1)
  242.                     self._canvas.itemconfigure(item, arrow="none")
  243.                 except Tkinter.TclError:
  244.                     # Probably the window was closed!
  245.                     return
  246.             else:
  247.                 item = self._canvas.create_line(x0, y0, x1, y1,
  248.                                                 width=self._width,
  249.                                                 capstyle="round",
  250.                                                 fill=self._color)
  251.             self._items.append(item)
  252.         self._draw_turtle()
  253.  
  254.     def _draw_turtle(self,position=[]):
  255.         if not self._tracing:
  256.             return
  257.         if position == []:
  258.             position = self._position
  259.         x,y = position
  260.         distance = 8
  261.         dx = distance * cos(self._angle*self._invradian)
  262.         dy = distance * sin(self._angle*self._invradian)
  263.         self._delete_turtle()
  264.         self._arrow = self._canvas.create_line(x-dx,y+dy,x,y,
  265.                                           width=self._width,
  266.                                           arrow="last",
  267.                                           capstyle="round",
  268.                                           fill=self._color)
  269.         self._canvas.update()
  270.  
  271.     def _delete_turtle(self):
  272.         if self._arrow != 0:
  273.             self._canvas.delete(self._arrow)
  274.         self._arrow = 0
  275.  
  276.  
  277.  
  278. _root = None
  279. _canvas = None
  280. _pen = None
  281.  
  282. class Pen(RawPen):
  283.  
  284.     def __init__(self):
  285.         global _root, _canvas
  286.         if _root is None:
  287.             _root = Tkinter.Tk()
  288.             _root.wm_protocol("WM_DELETE_WINDOW", self._destroy)
  289.         if _canvas is None:
  290.             # XXX Should have scroll bars
  291.             _canvas = Tkinter.Canvas(_root, background="white")
  292.             _canvas.pack(expand=1, fill="both")
  293.         RawPen.__init__(self, _canvas)
  294.  
  295.     def _destroy(self):
  296.         global _root, _canvas, _pen
  297.         root = self._canvas._root()
  298.         if root is _root:
  299.             _pen = None
  300.             _root = None
  301.             _canvas = None
  302.         root.destroy()
  303.  
  304.  
  305. def _getpen():
  306.     global _pen
  307.     pen = _pen
  308.     if not pen:
  309.         _pen = pen = Pen()
  310.     return pen
  311.  
  312. def degrees(): _getpen().degrees()
  313. def radians(): _getpen().radians()
  314. def reset(): _getpen().reset()
  315. def clear(): _getpen().clear()
  316. def tracer(flag): _getpen().tracer(flag)
  317. def forward(distance): _getpen().forward(distance)
  318. def backward(distance): _getpen().backward(distance)
  319. def left(angle): _getpen().left(angle)
  320. def right(angle): _getpen().right(angle)
  321. def up(): _getpen().up()
  322. def down(): _getpen().down()
  323. def width(width): _getpen().width(width)
  324. def color(*args): apply(_getpen().color, args)
  325. def write(arg, move=0): _getpen().write(arg, move)
  326. def fill(flag): _getpen().fill(flag)
  327. def circle(radius, extent=None): _getpen().circle(radius, extent)
  328. def goto(*args): apply(_getpen().goto, args)
  329.  
  330. def demo():
  331.     reset()
  332.     tracer(1)
  333.     up()
  334.     backward(100)
  335.     down()
  336.     # draw 3 squares; the last filled
  337.     width(3)
  338.     for i in range(3):
  339.         if i == 2:
  340.             fill(1)
  341.         for j in range(4):
  342.             forward(20)
  343.             left(90)
  344.         if i == 2:
  345.             color("maroon")
  346.             fill(0)
  347.         up()
  348.         forward(30)
  349.         down()
  350.     width(1)
  351.     color("black")
  352.     # move out of the way
  353.     tracer(0)
  354.     up()
  355.     right(90)
  356.     forward(100)
  357.     right(90)
  358.     forward(100)
  359.     right(180)
  360.     down()
  361.     # some text
  362.     write("startstart", 1)
  363.     write("start", 1)
  364.     color("red")
  365.     # staircase
  366.     for i in range(5):
  367.         forward(20)
  368.         left(90)
  369.         forward(20)
  370.         right(90)
  371.     # filled staircase
  372.     fill(1)
  373.     for i in range(5):
  374.         forward(20)
  375.         left(90)
  376.         forward(20)
  377.         right(90)
  378.     fill(0)
  379.     # more text
  380.     write("end")
  381.     if __name__ == '__main__':
  382.         _root.mainloop()
  383.  
  384. if __name__ == '__main__':
  385.     demo()
  386.