home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 May / maximum-cd-2010-05.iso / DiscContents / boxee-0.9.20.10711.exe / system / python / Lib / lib-tk / turtle.pyo (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-07-20  |  14.6 KB  |  541 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.4)
  3.  
  4. from math import *
  5. import Tkinter
  6.  
  7. class Error(Exception):
  8.     pass
  9.  
  10.  
  11. class RawPen:
  12.     
  13.     def __init__(self, canvas):
  14.         self._canvas = canvas
  15.         self._items = []
  16.         self._tracing = 1
  17.         self._arrow = 0
  18.         self.degrees()
  19.         self.reset()
  20.  
  21.     
  22.     def degrees(self, fullcircle = 360.0):
  23.         self._fullcircle = fullcircle
  24.         self._invradian = pi / (fullcircle * 0.5)
  25.  
  26.     
  27.     def radians(self):
  28.         self.degrees(2.0 * pi)
  29.  
  30.     
  31.     def reset(self):
  32.         canvas = self._canvas
  33.         self._canvas.update()
  34.         width = canvas.winfo_width()
  35.         height = canvas.winfo_height()
  36.         if width <= 1:
  37.             width = canvas['width']
  38.         
  39.         if height <= 1:
  40.             height = canvas['height']
  41.         
  42.         self._origin = (float(width) / 2.0, float(height) / 2.0)
  43.         self._position = self._origin
  44.         self._angle = 0.0
  45.         self._drawing = 1
  46.         self._width = 1
  47.         self._color = 'black'
  48.         self._filling = 0
  49.         self._path = []
  50.         self._tofill = []
  51.         self.clear()
  52.         canvas._root().tkraise()
  53.  
  54.     
  55.     def clear(self):
  56.         self.fill(0)
  57.         canvas = self._canvas
  58.         items = self._items
  59.         self._items = []
  60.         for item in items:
  61.             canvas.delete(item)
  62.         
  63.         self._delete_turtle()
  64.         self._draw_turtle()
  65.  
  66.     
  67.     def tracer(self, flag):
  68.         self._tracing = flag
  69.         if not self._tracing:
  70.             self._delete_turtle()
  71.         
  72.         self._draw_turtle()
  73.  
  74.     
  75.     def forward(self, distance):
  76.         (x0, y0) = start = self._position
  77.         x1 = x0 + distance * cos(self._angle * self._invradian)
  78.         y1 = y0 - distance * sin(self._angle * self._invradian)
  79.         self._goto(x1, y1)
  80.  
  81.     
  82.     def backward(self, distance):
  83.         self.forward(-distance)
  84.  
  85.     
  86.     def left(self, angle):
  87.         self._angle = (self._angle + angle) % self._fullcircle
  88.         self._draw_turtle()
  89.  
  90.     
  91.     def right(self, angle):
  92.         self.left(-angle)
  93.  
  94.     
  95.     def up(self):
  96.         self._drawing = 0
  97.  
  98.     
  99.     def down(self):
  100.         self._drawing = 1
  101.  
  102.     
  103.     def width(self, width):
  104.         self._width = float(width)
  105.  
  106.     
  107.     def color(self, *args):
  108.         if not args:
  109.             raise Error, 'no color arguments'
  110.         
  111.         if len(args) == 1:
  112.             color = args[0]
  113.             if type(color) == type(''):
  114.                 
  115.                 try:
  116.                     id = self._canvas.create_line(0, 0, 0, 0, fill = color)
  117.                 except Tkinter.TclError:
  118.                     raise Error, 'bad color string: %r' % (color,)
  119.  
  120.                 self._set_color(color)
  121.                 return None
  122.             
  123.             
  124.             try:
  125.                 (r, g, b) = color
  126.             raise Error, 'bad color sequence: %r' % (color,)
  127.  
  128.         else:
  129.             
  130.             try:
  131.                 (r, g, b) = args
  132.             except:
  133.                 raise Error, 'bad color arguments: %r' % (args,)
  134.  
  135.         x = 255.0
  136.         y = 0.5
  137.         self._set_color('#%02x%02x%02x' % (int(r * x + y), int(g * x + y), int(b * x + y)))
  138.  
  139.     
  140.     def _set_color(self, color):
  141.         self._color = color
  142.         self._draw_turtle()
  143.  
  144.     
  145.     def write(self, arg, move = 0):
  146.         (x, y) = start = self._position
  147.         x = x - 1
  148.         item = self._canvas.create_text(x, y, text = str(arg), anchor = 'sw', fill = self._color)
  149.         self._items.append(item)
  150.         if move:
  151.             (x0, y0, x1, y1) = self._canvas.bbox(item)
  152.             self._goto(x1, y1)
  153.         
  154.         self._draw_turtle()
  155.  
  156.     
  157.     def fill(self, flag):
  158.         if self._filling:
  159.             path = tuple(self._path)
  160.             smooth = self._filling < 0
  161.             if len(path) > 2:
  162.                 item = self._canvas._create('polygon', path, {
  163.                     'fill': self._color,
  164.                     'smooth': smooth })
  165.                 self._items.append(item)
  166.                 self._canvas.lower(item)
  167.                 if self._tofill:
  168.                     for item in self._tofill:
  169.                         self._canvas.itemconfigure(item, fill = self._color)
  170.                         self._items.append(item)
  171.                     
  172.                 
  173.             
  174.         
  175.         self._path = []
  176.         self._tofill = []
  177.         self._filling = flag
  178.         if flag:
  179.             self._path.append(self._position)
  180.         
  181.         self.forward(0)
  182.  
  183.     
  184.     def circle(self, radius, extent = None):
  185.         if extent is None:
  186.             extent = self._fullcircle
  187.         
  188.         (x0, y0) = self._position
  189.         xc = x0 - radius * sin(self._angle * self._invradian)
  190.         yc = y0 - radius * cos(self._angle * self._invradian)
  191.         if radius >= 0.0:
  192.             start = self._angle - 90.0
  193.         else:
  194.             start = self._angle + 90.0
  195.             extent = -extent
  196.         if self._filling:
  197.             if abs(extent) >= self._fullcircle:
  198.                 item = self._canvas.create_oval(xc - radius, yc - radius, xc + radius, yc + radius, width = self._width, outline = '')
  199.                 self._tofill.append(item)
  200.             
  201.             item = self._canvas.create_arc(xc - radius, yc - radius, xc + radius, yc + radius, style = 'chord', start = start, extent = extent, width = self._width, outline = '')
  202.             self._tofill.append(item)
  203.         
  204.         if self._drawing:
  205.             if abs(extent) >= self._fullcircle:
  206.                 item = self._canvas.create_oval(xc - radius, yc - radius, xc + radius, yc + radius, width = self._width, outline = self._color)
  207.                 self._items.append(item)
  208.             
  209.             item = self._canvas.create_arc(xc - radius, yc - radius, xc + radius, yc + radius, style = 'arc', start = start, extent = extent, width = self._width, outline = self._color)
  210.             self._items.append(item)
  211.         
  212.         angle = start + extent
  213.         x1 = xc + abs(radius) * cos(angle * self._invradian)
  214.         y1 = yc - abs(radius) * sin(angle * self._invradian)
  215.         self._angle = (self._angle + extent) % self._fullcircle
  216.         self._position = (x1, y1)
  217.         if self._filling:
  218.             self._path.append(self._position)
  219.         
  220.         self._draw_turtle()
  221.  
  222.     
  223.     def heading(self):
  224.         return self._angle
  225.  
  226.     
  227.     def setheading(self, angle):
  228.         self._angle = angle
  229.         self._draw_turtle()
  230.  
  231.     
  232.     def window_width(self):
  233.         width = self._canvas.winfo_width()
  234.         if width <= 1:
  235.             width = self._canvas['width']
  236.         
  237.         return width
  238.  
  239.     
  240.     def window_height(self):
  241.         height = self._canvas.winfo_height()
  242.         if height <= 1:
  243.             height = self._canvas['height']
  244.         
  245.         return height
  246.  
  247.     
  248.     def position(self):
  249.         (x0, y0) = self._origin
  250.         (x1, y1) = self._position
  251.         return [
  252.             x1 - x0,
  253.             -y1 + y0]
  254.  
  255.     
  256.     def setx(self, xpos):
  257.         (x0, y0) = self._origin
  258.         (x1, y1) = self._position
  259.         self._goto(x0 + xpos, y1)
  260.  
  261.     
  262.     def sety(self, ypos):
  263.         (x0, y0) = self._origin
  264.         (x1, y1) = self._position
  265.         self._goto(x1, y0 - ypos)
  266.  
  267.     
  268.     def goto(self, *args):
  269.         if len(args) == 1:
  270.             
  271.             try:
  272.                 (x, y) = args[0]
  273.             raise Error, 'bad point argument: %r' % (args[0],)
  274.  
  275.         else:
  276.             
  277.             try:
  278.                 (x, y) = args
  279.             except:
  280.                 raise Error, 'bad coordinates: %r' % (args[0],)
  281.  
  282.         (x0, y0) = self._origin
  283.         self._goto(x0 + x, y0 - y)
  284.  
  285.     
  286.     def _goto(self, x1, y1):
  287.         (x0, y0) = start = self._position
  288.         self._position = map(float, (x1, y1))
  289.         if self._filling:
  290.             self._path.append(self._position)
  291.         
  292.         if self._drawing:
  293.             if self._tracing:
  294.                 dx = float(x1 - x0)
  295.                 dy = float(y1 - y0)
  296.                 distance = hypot(dx, dy)
  297.                 nhops = int(distance)
  298.                 item = self._canvas.create_line(x0, y0, x0, y0, width = self._width, capstyle = 'round', fill = self._color)
  299.                 
  300.                 try:
  301.                     for i in range(1, 1 + nhops):
  302.                         x = x0 + dx * i / nhops
  303.                         y = y0 + dy * i / nhops
  304.                         self._canvas.coords(item, x0, y0, x, y)
  305.                         self._draw_turtle((x, y))
  306.                         self._canvas.update()
  307.                         self._canvas.after(10)
  308.                     
  309.                     self._canvas.coords(item, x0, y0, x1, y1)
  310.                     self._canvas.itemconfigure(item, arrow = 'none')
  311.                 except Tkinter.TclError:
  312.                     return None
  313.                 except:
  314.                     None<EXCEPTION MATCH>Tkinter.TclError
  315.                 
  316.  
  317.             None<EXCEPTION MATCH>Tkinter.TclError
  318.             item = self._canvas.create_line(x0, y0, x1, y1, width = self._width, capstyle = 'round', fill = self._color)
  319.             self._items.append(item)
  320.         
  321.         self._draw_turtle()
  322.  
  323.     
  324.     def _draw_turtle(self, position = []):
  325.         if not self._tracing:
  326.             return None
  327.         
  328.         if position == []:
  329.             position = self._position
  330.         
  331.         (x, y) = position
  332.         distance = 8
  333.         dx = distance * cos(self._angle * self._invradian)
  334.         dy = distance * sin(self._angle * self._invradian)
  335.         self._delete_turtle()
  336.         self._arrow = self._canvas.create_line(x - dx, y + dy, x, y, width = self._width, arrow = 'last', capstyle = 'round', fill = self._color)
  337.         self._canvas.update()
  338.  
  339.     
  340.     def _delete_turtle(self):
  341.         if self._arrow != 0:
  342.             self._canvas.delete(self._arrow)
  343.         
  344.         self._arrow = 0
  345.  
  346.  
  347. _root = None
  348. _canvas = None
  349. _pen = None
  350.  
  351. class Pen(RawPen):
  352.     
  353.     def __init__(self):
  354.         global _root, _canvas
  355.         if _root is None:
  356.             _root = Tkinter.Tk()
  357.             _root.wm_protocol('WM_DELETE_WINDOW', self._destroy)
  358.         
  359.         if _canvas is None:
  360.             _canvas = Tkinter.Canvas(_root, background = 'white')
  361.             _canvas.pack(expand = 1, fill = 'both')
  362.         
  363.         RawPen.__init__(self, _canvas)
  364.  
  365.     
  366.     def _destroy(self):
  367.         global _pen, _root, _canvas
  368.         root = self._canvas._root()
  369.         if root is _root:
  370.             _pen = None
  371.             _root = None
  372.             _canvas = None
  373.         
  374.         root.destroy()
  375.  
  376.  
  377.  
  378. def _getpen():
  379.     global _pen
  380.     pen = _pen
  381.     if not pen:
  382.         _pen = pen = Pen()
  383.     
  384.     return pen
  385.  
  386.  
  387. def degrees():
  388.     _getpen().degrees()
  389.  
  390.  
  391. def radians():
  392.     _getpen().radians()
  393.  
  394.  
  395. def reset():
  396.     _getpen().reset()
  397.  
  398.  
  399. def clear():
  400.     _getpen().clear()
  401.  
  402.  
  403. def tracer(flag):
  404.     _getpen().tracer(flag)
  405.  
  406.  
  407. def forward(distance):
  408.     _getpen().forward(distance)
  409.  
  410.  
  411. def backward(distance):
  412.     _getpen().backward(distance)
  413.  
  414.  
  415. def left(angle):
  416.     _getpen().left(angle)
  417.  
  418.  
  419. def right(angle):
  420.     _getpen().right(angle)
  421.  
  422.  
  423. def up():
  424.     _getpen().up()
  425.  
  426.  
  427. def down():
  428.     _getpen().down()
  429.  
  430.  
  431. def width(width):
  432.     _getpen().width(width)
  433.  
  434.  
  435. def color(*args):
  436.     _getpen().color(*args)
  437.  
  438.  
  439. def write(arg, move = 0):
  440.     _getpen().write(arg, move)
  441.  
  442.  
  443. def fill(flag):
  444.     _getpen().fill(flag)
  445.  
  446.  
  447. def circle(radius, extent = None):
  448.     _getpen().circle(radius, extent)
  449.  
  450.  
  451. def goto(*args):
  452.     _getpen().goto(*args)
  453.  
  454.  
  455. def heading():
  456.     return _getpen().heading()
  457.  
  458.  
  459. def setheading(angle):
  460.     _getpen().setheading(angle)
  461.  
  462.  
  463. def position():
  464.     return _getpen().position()
  465.  
  466.  
  467. def window_width():
  468.     return _getpen().window_width()
  469.  
  470.  
  471. def window_height():
  472.     return _getpen().window_height()
  473.  
  474.  
  475. def setx(xpos):
  476.     _getpen().setx(xpos)
  477.  
  478.  
  479. def sety(ypos):
  480.     _getpen().sety(ypos)
  481.  
  482.  
  483. def demo():
  484.     reset()
  485.     tracer(1)
  486.     up()
  487.     backward(100)
  488.     down()
  489.     width(3)
  490.     for i in range(3):
  491.         if i == 2:
  492.             fill(1)
  493.         
  494.         for j in range(4):
  495.             forward(20)
  496.             left(90)
  497.         
  498.         if i == 2:
  499.             color('maroon')
  500.             fill(0)
  501.         
  502.         up()
  503.         forward(30)
  504.         down()
  505.     
  506.     width(1)
  507.     color('black')
  508.     tracer(0)
  509.     up()
  510.     right(90)
  511.     forward(100)
  512.     right(90)
  513.     forward(100)
  514.     right(180)
  515.     down()
  516.     write('startstart', 1)
  517.     write('start', 1)
  518.     color('red')
  519.     for i in range(5):
  520.         forward(20)
  521.         left(90)
  522.         forward(20)
  523.         right(90)
  524.     
  525.     fill(1)
  526.     for i in range(5):
  527.         forward(20)
  528.         left(90)
  529.         forward(20)
  530.         right(90)
  531.     
  532.     fill(0)
  533.     write('end')
  534.     if __name__ == '__main__':
  535.         _root.mainloop()
  536.     
  537.  
  538. if __name__ == '__main__':
  539.     demo()
  540.  
  541.