Turtle graph

Thread Solved
Reply

Join Date: Oct 2007
Posts: 6
Reputation: Agentbob is an unknown quantity at this point 
Solved Threads: 0
Agentbob Agentbob is offline Offline
Newbie Poster

Turtle graph

 
0
  #1
Dec 7th, 2007
I'm trying to change the font for the turtle graph.
def goto(x,y):
    turtle.up()
    turtle.goto(x,y)
    turtle.down

import turtle
turtle.tracer(False)
turtle.width(3)
goto(-270,270)
turtle.right(90)
turtle.forward(540)
turtle.left(90)
turtle.forward(540)
turtle.left(90)
turtle.forward(540)
turtle.left(90)
turtle.forward(540)
goto(50,50)
turtle.write('testing')
turtle.tracer(True)

That's a box I created and I want to add text to it. I looked around the Python documentation but can't find any argument for it. I wan to increase the size of the text. Is there a way?

I tried turtle.write('testing',font=('arial',8,'normal')) but to no avail.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,856
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 866
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Turtle graph

 
0
  #2
Dec 8th, 2007
The easiest way to do this is to modify turtle.py (usually found in C:\Python25\Lib\lib-tk\turtle.py) by adding a method write_font() and saving the modified module as turtle1.py (in this case in C:\Python25\Lib\lib-tk\turtle1.py). Here is turtle1.py (click on "Toggle Plain Text", select, copy and paste into your editor, then save) ...
  1. # LogoMation-like turtle graphics
  2. # write_font() method added to class RawPen
  3. # original C:\Python25\Lib\lib-tk\turtle.py
  4. # saved as C:\Python25\Lib\lib-tk\turtle1.py
  5.  
  6. """
  7. Turtle graphics is a popular way for introducing programming to
  8. kids. It was part of the original Logo programming language developed
  9. by Wally Feurzeig and Seymour Papert in 1966.
  10.  
  11. Imagine a robotic turtle starting at (0, 0) in the x-y plane. Give it
  12. the command turtle.forward(15), and it moves (on-screen!) 15 pixels in
  13. the direction it is facing, drawing a line as it moves. Give it the
  14. command turtle.left(25), and it rotates in-place 25 degrees clockwise.
  15.  
  16. By combining together these and similar commands, intricate shapes and
  17. pictures can easily be drawn.
  18. """
  19.  
  20. from math import * # Also for export
  21. import Tkinter
  22.  
  23. speeds = ['fastest', 'fast', 'normal', 'slow', 'slowest']
  24.  
  25. class Error(Exception):
  26. pass
  27.  
  28. class RawPen:
  29.  
  30. def __init__(self, canvas):
  31. self._canvas = canvas
  32. self._items = []
  33. self._tracing = 1
  34. self._arrow = 0
  35. self._delay = 10 # default delay for drawing
  36. self._angle = 0.0
  37. self.degrees()
  38. self.reset()
  39.  
  40. def degrees(self, fullcircle=360.0):
  41. """ Set angle measurement units to degrees.
  42.  
  43. Example:
  44. >>> turtle.degrees()
  45. """
  46. # Don't try to change _angle if it is 0, because
  47. # _fullcircle might not be set, yet
  48. if self._angle:
  49. self._angle = (self._angle / self._fullcircle) * fullcircle
  50. self._fullcircle = fullcircle
  51. self._invradian = pi / (fullcircle * 0.5)
  52.  
  53. def radians(self):
  54. """ Set the angle measurement units to radians.
  55.  
  56. Example:
  57. >>> turtle.radians()
  58. """
  59. self.degrees(2.0*pi)
  60.  
  61. def reset(self):
  62. """ Clear the screen, re-center the pen, and set variables to
  63. the default values.
  64.  
  65. Example:
  66. >>> turtle.position()
  67. [0.0, -22.0]
  68. >>> turtle.heading()
  69. 100.0
  70. >>> turtle.reset()
  71. >>> turtle.position()
  72. [0.0, 0.0]
  73. >>> turtle.heading()
  74. 0.0
  75. """
  76. canvas = self._canvas
  77. self._canvas.update()
  78. width = canvas.winfo_width()
  79. height = canvas.winfo_height()
  80. if width <= 1:
  81. width = canvas['width']
  82. if height <= 1:
  83. height = canvas['height']
  84. self._origin = float(width)/2.0, float(height)/2.0
  85. self._position = self._origin
  86. self._angle = 0.0
  87. self._drawing = 1
  88. self._width = 1
  89. self._color = "black"
  90. self._filling = 0
  91. self._path = []
  92. self.clear()
  93. canvas._root().tkraise()
  94.  
  95. def clear(self):
  96. """ Clear the screen. The turtle does not move.
  97.  
  98. Example:
  99. >>> turtle.clear()
  100. """
  101. self.fill(0)
  102. canvas = self._canvas
  103. items = self._items
  104. self._items = []
  105. for item in items:
  106. canvas.delete(item)
  107. self._delete_turtle()
  108. self._draw_turtle()
  109.  
  110. def tracer(self, flag):
  111. """ Set tracing on if flag is True, and off if it is False.
  112. Tracing means line are drawn more slowly, with an
  113. animation of an arrow along the line.
  114.  
  115. Example:
  116. >>> turtle.tracer(False) # turns off Tracer
  117. """
  118. self._tracing = flag
  119. if not self._tracing:
  120. self._delete_turtle()
  121. self._draw_turtle()
  122.  
  123. def forward(self, distance):
  124. """ Go forward distance steps.
  125.  
  126. Example:
  127. >>> turtle.position()
  128. [0.0, 0.0]
  129. >>> turtle.forward(25)
  130. >>> turtle.position()
  131. [25.0, 0.0]
  132. >>> turtle.forward(-75)
  133. >>> turtle.position()
  134. [-50.0, 0.0]
  135. """
  136. x0, y0 = start = self._position
  137. x1 = x0 + distance * cos(self._angle*self._invradian)
  138. y1 = y0 - distance * sin(self._angle*self._invradian)
  139. self._goto(x1, y1)
  140.  
  141. def backward(self, distance):
  142. """ Go backwards distance steps.
  143.  
  144. The turtle's heading does not change.
  145.  
  146. Example:
  147. >>> turtle.position()
  148. [0.0, 0.0]
  149. >>> turtle.backward(30)
  150. >>> turtle.position()
  151. [-30.0, 0.0]
  152. """
  153. self.forward(-distance)
  154.  
  155. def left(self, angle):
  156. """ Turn left angle units (units are by default degrees,
  157. but can be set via the degrees() and radians() functions.)
  158.  
  159. When viewed from above, the turning happens in-place around
  160. its front tip.
  161.  
  162. Example:
  163. >>> turtle.heading()
  164. 22
  165. >>> turtle.left(45)
  166. >>> turtle.heading()
  167. 67.0
  168. """
  169. self._angle = (self._angle + angle) % self._fullcircle
  170. self._draw_turtle()
  171.  
  172. def right(self, angle):
  173. """ Turn right angle units (units are by default degrees,
  174. but can be set via the degrees() and radians() functions.)
  175.  
  176. When viewed from above, the turning happens in-place around
  177. its front tip.
  178.  
  179. Example:
  180. >>> turtle.heading()
  181. 22
  182. >>> turtle.right(45)
  183. >>> turtle.heading()
  184. 337.0
  185. """
  186. self.left(-angle)
  187.  
  188. def up(self):
  189. """ Pull the pen up -- no drawing when moving.
  190.  
  191. Example:
  192. >>> turtle.up()
  193. """
  194. self._drawing = 0
  195.  
  196. def down(self):
  197. """ Put the pen down -- draw when moving.
  198.  
  199. Example:
  200. >>> turtle.down()
  201. """
  202. self._drawing = 1
  203.  
  204. def width(self, width):
  205. """ Set the line to thickness to width.
  206.  
  207. Example:
  208. >>> turtle.width(10)
  209. """
  210. self._width = float(width)
  211.  
  212. def color(self, *args):
  213. """ Set the pen color.
  214.  
  215. Three input formats are allowed:
  216.  
  217. color(s)
  218. s is a Tk specification string, such as "red" or "yellow"
  219.  
  220. color((r, g, b))
  221. *a tuple* of r, g, and b, which represent, an RGB color,
  222. and each of r, g, and b are in the range [0..1]
  223.  
  224. color(r, g, b)
  225. r, g, and b represent an RGB color, and each of r, g, and b
  226. are in the range [0..1]
  227.  
  228. Example:
  229.  
  230. >>> turtle.color('brown')
  231. >>> tup = (0.2, 0.8, 0.55)
  232. >>> turtle.color(tup)
  233. >>> turtle.color(0, .5, 0)
  234. """
  235. if not args:
  236. raise Error, "no color arguments"
  237. if len(args) == 1:
  238. color = args[0]
  239. if type(color) == type(""):
  240. # Test the color first
  241. try:
  242. id = self._canvas.create_line(0, 0, 0, 0, fill=color)
  243. except Tkinter.TclError:
  244. raise Error, "bad color string: %r" % (color,)
  245. self._set_color(color)
  246. return
  247. try:
  248. r, g, b = color
  249. except:
  250. raise Error, "bad color sequence: %r" % (color,)
  251. else:
  252. try:
  253. r, g, b = args
  254. except:
  255. raise Error, "bad color arguments: %r" % (args,)
  256. assert 0 <= r <= 1
  257. assert 0 <= g <= 1
  258. assert 0 <= b <= 1
  259. x = 255.0
  260. y = 0.5
  261. self._set_color("#%02x%02x%02x" % (int(r*x+y), int(g*x+y), int(b*x+y)))
  262.  
  263. def _set_color(self,color):
  264. self._color = color
  265. self._draw_turtle()
  266.  
  267. def write(self, text, move=False):
  268. """ Write text at the current pen position.
  269.  
  270. If move is true, the pen is moved to the bottom-right corner
  271. of the text. By default, move is False.
  272.  
  273. Example:
  274. >>> turtle.write('The race is on!')
  275. >>> turtle.write('Home = (0, 0)', True)
  276. """
  277. x, y = self._position
  278. x = x-1 # correction -- calibrated for Windows
  279. item = self._canvas.create_text(x, y,
  280. text=str(text),
  281. anchor="sw",
  282. fill=self._color)
  283. self._items.append(item)
  284. if move:
  285. x0, y0, x1, y1 = self._canvas.bbox(item)
  286. self._goto(x1, y1)
  287. self._draw_turtle()
  288.  
  289. def write_font(self, text, font=('times', 10), move=False):
  290. """ added this modified method ----->
  291. Write text at the current pen position using the
  292. specified font, default is ('times', 10).
  293.  
  294. If move is true, the pen is moved to the bottom-right corner
  295. of the text. By default, move is False.
  296.  
  297. Example:
  298. >>> turtle.write_font('The race is on!', ('times', 30, 'bold'))
  299. >>> turtle.write('Home = (0, 0)', ('times', 30, 'bold'), True)
  300. """
  301. x, y = self._position
  302. x = x-1 # correction -- calibrated for Windows
  303. item = self._canvas.create_text(x, y,
  304. text=str(text),
  305. anchor="sw",
  306. font=font, # <-------
  307. fill=self._color)
  308. self._items.append(item)
  309. if move:
  310. x0, y0, x1, y1 = self._canvas.bbox(item)
  311. self._goto(x1, y1)
  312. self._draw_turtle()
  313.  
  314. def fill(self, flag):
  315. """ Call fill(1) before drawing the shape you
  316. want to fill, and fill(0) when done.
  317.  
  318. Example:
  319. >>> turtle.fill(1)
  320. >>> turtle.forward(100)
  321. >>> turtle.left(90)
  322. >>> turtle.forward(100)
  323. >>> turtle.left(90)
  324. >>> turtle.forward(100)
  325. >>> turtle.left(90)
  326. >>> turtle.forward(100)
  327. >>> turtle.fill(0)
  328. """
  329. if self._filling:
  330. path = tuple(self._path)
  331. smooth = self._filling < 0
  332. if len(path) > 2:
  333. item = self._canvas._create('polygon', path,
  334. {'fill': self._color,
  335. 'smooth': smooth})
  336. self._items.append(item)
  337. self._path = []
  338. self._filling = flag
  339. if flag:
  340. self._path.append(self._position)
  341.  
  342. def begin_fill(self):
  343. """ Called just before drawing a shape to be filled.
  344. Must eventually be followed by a corresponding end_fill() call.
  345. Otherwise it will be ignored.
  346.  
  347. Example:
  348. >>> turtle.begin_fill()
  349. >>> turtle.forward(100)
  350. >>> turtle.left(90)
  351. >>> turtle.forward(100)
  352. >>> turtle.left(90)
  353. >>> turtle.forward(100)
  354. >>> turtle.left(90)
  355. >>> turtle.forward(100)
  356. >>> turtle.end_fill()
  357. """
  358. self._path = [self._position]
  359. self._filling = 1
  360.  
  361. def end_fill(self):
  362. """ Called after drawing a shape to be filled.
  363.  
  364. Example:
  365. >>> turtle.begin_fill()
  366. >>> turtle.forward(100)
  367. >>> turtle.left(90)
  368. >>> turtle.forward(100)
  369. >>> turtle.left(90)
  370. >>> turtle.forward(100)
  371. >>> turtle.left(90)
  372. >>> turtle.forward(100)
  373. >>> turtle.end_fill()
  374. """
  375. self.fill(0)
  376.  
  377. def circle(self, radius, extent = None):
  378. """ Draw a circle with given radius.
  379. The center is radius units left of the turtle; extent
  380. determines which part of the circle is drawn. If not given,
  381. the entire circle is drawn.
  382.  
  383. If extent is not a full circle, one endpoint of the arc is the
  384. current pen position. The arc is drawn in a counter clockwise
  385. direction if radius is positive, otherwise in a clockwise
  386. direction. In the process, the direction of the turtle is
  387. changed by the amount of the extent.
  388.  
  389. >>> turtle.circle(50)
  390. >>> turtle.circle(120, 180) # half a circle
  391. """
  392. if extent is None:
  393. extent = self._fullcircle
  394. frac = abs(extent)/self._fullcircle
  395. steps = 1+int(min(11+abs(radius)/6.0, 59.0)*frac)
  396. w = 1.0 * extent / steps
  397. w2 = 0.5 * w
  398. l = 2.0 * radius * sin(w2*self._invradian)
  399. if radius < 0:
  400. l, w, w2 = -l, -w, -w2
  401. self.left(w2)
  402. for i in range(steps):
  403. self.forward(l)
  404. self.left(w)
  405. self.right(w2)
  406.  
  407. def heading(self):
  408. """ Return the turtle's current heading.
  409.  
  410. Example:
  411. >>> turtle.heading()
  412. 67.0
  413. """
  414. return self._angle
  415.  
  416. def setheading(self, angle):
  417. """ Set the turtle facing the given angle.
  418.  
  419. Here are some common directions in degrees:
  420.  
  421. 0 - east
  422. 90 - north
  423. 180 - west
  424. 270 - south
  425.  
  426. Example:
  427. >>> turtle.setheading(90)
  428. >>> turtle.heading()
  429. 90
  430. >>> turtle.setheading(128)
  431. >>> turtle.heading()
  432. 128
  433. """
  434. self._angle = angle
  435. self._draw_turtle()
  436.  
  437. def window_width(self):
  438. """ Returns the width of the turtle window.
  439.  
  440. Example:
  441. >>> turtle.window_width()
  442. 640
  443. """
  444. width = self._canvas.winfo_width()
  445. if width <= 1: # the window isn't managed by a geometry manager
  446. width = self._canvas['width']
  447. return width
  448.  
  449. def window_height(self):
  450. """ Return the height of the turtle window.
  451.  
  452. Example:
  453. >>> turtle.window_height()
  454. 768
  455. """
  456. height = self._canvas.winfo_height()
  457. if height <= 1: # the window isn't managed by a geometry manager
  458. height = self._canvas['height']
  459. return height
  460.  
  461. def position(self):
  462. """ Return the current (x, y) location of the turtle.
  463.  
  464. Example:
  465. >>> turtle.position()
  466. [0.0, 240.0]
  467. """
  468. x0, y0 = self._origin
  469. x1, y1 = self._position
  470. return [x1-x0, -y1+y0]
  471.  
  472. def setx(self, xpos):
  473. """ Set the turtle's x coordinate to be xpos.
  474.  
  475. Example:
  476. >>> turtle.position()
  477. [10.0, 240.0]
  478. >>> turtle.setx(10)
  479. >>> turtle.position()
  480. [10.0, 240.0]
  481. """
  482. x0, y0 = self._origin
  483. x1, y1 = self._position
  484. self._goto(x0+xpos, y1)
  485.  
  486. def sety(self, ypos):
  487. """ Set the turtle's y coordinate to be ypos.
  488.  
  489. Example:
  490. >>> turtle.position()
  491. [0.0, 0.0]
  492. >>> turtle.sety(-22)
  493. >>> turtle.position()
  494. [0.0, -22.0]
  495. """
  496. x0, y0 = self._origin
  497. x1, y1 = self._position
  498. self._goto(x1, y0-ypos)
  499.  
  500. def towards(self, *args):
  501. """Returs the angle, which corresponds to the line
  502. from turtle-position to point (x,y).
  503.  
  504. Argument can be two coordinates or one pair of coordinates
  505. or a RawPen/Pen instance.
  506.  
  507. Example:
  508. >>> turtle.position()
  509. [10.0, 10.0]
  510. >>> turtle.towards(0,0)
  511. 225.0
  512. """
  513. if len(args) == 2:
  514. x, y = args
  515. else:
  516. arg = args[0]
  517. if isinstance(arg, RawPen):
  518. x, y = arg.position()
  519. else:
  520. x, y = arg
  521. x0, y0 = self.position()
  522. dx = x - x0
  523. dy = y - y0
  524. return (atan2(dy,dx) / self._invradian) % self._fullcircle
  525.  
  526. def goto(self, *args):
  527. """ Go to the given point.
  528.  
  529. If the pen is down, then a line will be drawn. The turtle's
  530. orientation does not change.
  531.  
  532. Two input formats are accepted:
  533.  
  534. goto(x, y)
  535. go to point (x, y)
  536.  
  537. goto((x, y))
  538. go to point (x, y)
  539.  
  540. Example:
  541. >>> turtle.position()
  542. [0.0, 0.0]
  543. >>> turtle.goto(50, -45)
  544. >>> turtle.position()
  545. [50.0, -45.0]
  546. """
  547. if len(args) == 1:
  548. try:
  549. x, y = args[0]
  550. except:
  551. raise Error, "bad point argument: %r" % (args[0],)
  552. else:
  553. try:
  554. x, y = args
  555. except:
  556. raise Error, "bad coordinates: %r" % (args[0],)
  557. x0, y0 = self._origin
  558. self._goto(x0+x, y0-y)
  559.  
  560. def _goto(self, x1, y1):
  561. x0, y0 = self._position
  562. self._position = map(float, (x1, y1))
  563. if self._filling:
  564. self._path.append(self._position)
  565. if self._drawing:
  566. if self._tracing:
  567. dx = float(x1 - x0)
  568. dy = float(y1 - y0)
  569. distance = hypot(dx, dy)
  570. nhops = int(distance)
  571. item = self._canvas.create_line(x0, y0, x0, y0,
  572. width=self._width,
  573. capstyle="round",
  574. fill=self._color)
  575. try:
  576. for i in range(1, 1+nhops):
  577. x, y = x0 + dx*i/nhops, y0 + dy*i/nhops
  578. self._canvas.coords(item, x0, y0, x, y)
  579. self._draw_turtle((x,y))
  580. self._canvas.update()
  581. self._canvas.after(self._delay)
  582. # in case nhops==0
  583. self._canvas.coords(item, x0, y0, x1, y1)
  584. self._canvas.itemconfigure(item, arrow="none")
  585. except Tkinter.TclError:
  586. # Probably the window was closed!
  587. return
  588. else:
  589. item = self._canvas.create_line(x0, y0, x1, y1,
  590. width=self._width,
  591. capstyle="round",
  592. fill=self._color)
  593. self._items.append(item)
  594. self._draw_turtle()
  595.  
  596. def speed(self, speed):
  597. """ Set the turtle's speed.
  598.  
  599. speed must one of these five strings:
  600.  
  601. 'fastest' is a 0 ms delay
  602. 'fast' is a 5 ms delay
  603. 'normal' is a 10 ms delay
  604. 'slow' is a 15 ms delay
  605. 'slowest' is a 20 ms delay
  606.  
  607. Example:
  608. >>> turtle.speed('slow')
  609. """
  610. try:
  611. speed = speed.strip().lower()
  612. self._delay = speeds.index(speed) * 5
  613. except:
  614. raise ValueError("%r is not a valid speed. speed must be "
  615. "one of %s" % (speed, speeds))
  616.  
  617.  
  618. def delay(self, delay):
  619. """ Set the drawing delay in milliseconds.
  620.  
  621. This is intended to allow finer control of the drawing speed
  622. than the speed() method
  623.  
  624. Example:
  625. >>> turtle.delay(15)
  626. """
  627. if int(delay) < 0:
  628. raise ValueError("delay must be greater than or equal to 0")
  629. self._delay = int(delay)
  630.  
  631. def _draw_turtle(self, position=[]):
  632. if not self._tracing:
  633. self._canvas.update()
  634. return
  635. if position == []:
  636. position = self._position
  637. x,y = position
  638. distance = 8
  639. dx = distance * cos(self._angle*self._invradian)
  640. dy = distance * sin(self._angle*self._invradian)
  641. self._delete_turtle()
  642. self._arrow = self._canvas.create_line(x-dx,y+dy,x,y,
  643. width=self._width,
  644. arrow="last",
  645. capstyle="round",
  646. fill=self._color)
  647. self._canvas.update()
  648.  
  649. def _delete_turtle(self):
  650. if self._arrow != 0:
  651. self._canvas.delete(self._arrow)
  652. self._arrow = 0
  653.  
  654.  
  655. _root = None
  656. _canvas = None
  657. _pen = None
  658. _width = 0.50 # 50% of window width
  659. _height = 0.75 # 75% of window height
  660. _startx = None
  661. _starty = None
  662. _title = "Turtle Graphics" # default title
  663.  
  664. class Pen(RawPen):
  665.  
  666. def __init__(self):
  667. global _root, _canvas
  668. if _root is None:
  669. _root = Tkinter.Tk()
  670. _root.wm_protocol("WM_DELETE_WINDOW", self._destroy)
  671. _root.title(_title)
  672.  
  673. if _canvas is None:
  674. # XXX Should have scroll bars
  675. _canvas = Tkinter.Canvas(_root, background="white")
  676. _canvas.pack(expand=1, fill="both")
  677.  
  678. setup(width=_width, height= _height, startx=_startx, starty=_starty)
  679.  
  680. RawPen.__init__(self, _canvas)
  681.  
  682. def _destroy(self):
  683. global _root, _canvas, _pen
  684. root = self._canvas._root()
  685. if root is _root:
  686. _pen = None
  687. _root = None
  688. _canvas = None
  689. root.destroy()
  690.  
  691. def _getpen():
  692. global _pen
  693. if not _pen:
  694. _pen = Pen()
  695. return _pen
  696.  
  697. class Turtle(Pen):
  698. pass
  699.  
  700. """For documentation of the following functions see
  701. the RawPen methods with the same names
  702. """
  703.  
  704. def degrees(): _getpen().degrees()
  705. def radians(): _getpen().radians()
  706. def reset(): _getpen().reset()
  707. def clear(): _getpen().clear()
  708. def tracer(flag): _getpen().tracer(flag)
  709. def forward(distance): _getpen().forward(distance)
  710. def backward(distance): _getpen().backward(distance)
  711. def left(angle): _getpen().left(angle)
  712. def right(angle): _getpen().right(angle)
  713. def up(): _getpen().up()
  714. def down(): _getpen().down()
  715. def width(width): _getpen().width(width)
  716. def color(*args): _getpen().color(*args)
  717. def write(arg, move=0): _getpen().write(arg, move)
  718.  
  719. def write_font(arg, font=('times', 10), move=0): _getpen().write_font(arg, font, move)
  720.  
  721. def fill(flag): _getpen().fill(flag)
  722. def begin_fill(): _getpen().begin_fill()
  723. def end_fill(): _getpen().end_fill()
  724. def circle(radius, extent=None): _getpen().circle(radius, extent)
  725. def goto(*args): _getpen().goto(*args)
  726. def heading(): return _getpen().heading()
  727. def setheading(angle): _getpen().setheading(angle)
  728. def position(): return _getpen().position()
  729. def window_width(): return _getpen().window_width()
  730. def window_height(): return _getpen().window_height()
  731. def setx(xpos): _getpen().setx(xpos)
  732. def sety(ypos): _getpen().sety(ypos)
  733. def towards(*args): return _getpen().towards(*args)
  734.  
  735. def done(): _root.mainloop()
  736. def delay(delay): return _getpen().delay(delay)
  737. def speed(speed): return _getpen().speed(speed)
  738.  
  739. for methodname in dir(RawPen):
  740. """ copies RawPen docstrings to module functions of same name """
  741. if not methodname.startswith("_"):
  742. eval(methodname).__doc__ = RawPen.__dict__[methodname].__doc__
  743.  
  744.  
  745. def setup(**geometry):
  746. """ Sets the size and position of the main window.
  747.  
  748. Keywords are width, height, startx and starty:
  749.  
  750. width: either a size in pixels or a fraction of the screen.
  751. Default is 50% of screen.
  752. height: either the height in pixels or a fraction of the screen.
  753. Default is 75% of screen.
  754.  
  755. Setting either width or height to None before drawing will force
  756. use of default geometry as in older versions of turtle.py
  757.  
  758. startx: starting position in pixels from the left edge of the screen.
  759. Default is to center window. Setting startx to None is the default
  760. and centers window horizontally on screen.
  761.  
  762. starty: starting position in pixels from the top edge of the screen.
  763. Default is to center window. Setting starty to None is the default
  764. and centers window vertically on screen.
  765.  
  766. Examples:
  767. >>> setup (width=200, height=200, startx=0, starty=0)
  768.  
  769. sets window to 200x200 pixels, in upper left of screen
  770.  
  771. >>> setup(width=.75, height=0.5, startx=None, starty=None)
  772.  
  773. sets window to 75% of screen by 50% of screen and centers
  774.  
  775. >>> setup(width=None)
  776.  
  777. forces use of default geometry as in older versions of turtle.py
  778. """
  779.  
  780. global _width, _height, _startx, _starty
  781.  
  782. width = geometry.get('width',_width)
  783. if width >= 0 or width == None:
  784. _width = width
  785. else:
  786. raise ValueError, "width can not be less than 0"
  787.  
  788. height = geometry.get('height',_height)
  789. if height >= 0 or height == None:
  790. _height = height
  791. else:
  792. raise ValueError, "height can not be less than 0"
  793.  
  794. startx = geometry.get('startx', _startx)
  795. if startx >= 0 or startx == None:
  796. _startx = _startx
  797. else:
  798. raise ValueError, "startx can not be less than 0"
  799.  
  800. starty = geometry.get('starty', _starty)
  801. if starty >= 0 or starty == None:
  802. _starty = starty
  803. else:
  804. raise ValueError, "startx can not be less than 0"
  805.  
  806.  
  807. if _root and _width and _height:
  808. if 0 < _width <= 1:
  809. _width = _root.winfo_screenwidth() * +width
  810. if 0 < _height <= 1:
  811. _height = _root.winfo_screenheight() * _height
  812.  
  813. # center window on screen
  814. if _startx is None:
  815. _startx = (_root.winfo_screenwidth() - _width) / 2
  816.  
  817. if _starty is None:
  818. _starty = (_root.winfo_screenheight() - _height) / 2
  819.  
  820. _root.geometry("%dx%d+%d+%d" % (_width, _height, _startx, _starty))
  821.  
  822. def title(title):
  823. """Set the window title.
  824.  
  825. By default this is set to 'Turtle Graphics'
  826.  
  827. Example:
  828. >>> title("My Window")
  829. """
  830.  
  831. global _title
  832. _title = title
  833.  
  834. def demo():
  835. reset()
  836. tracer(1)
  837. up()
  838. backward(100)
  839. down()
  840. # draw 3 squares; the last filled
  841. width(3)
  842. for i in range(3):
  843. if i == 2:
  844. fill(1)
  845. for j in range(4):
  846. forward(20)
  847. left(90)
  848. if i == 2:
  849. color("maroon")
  850. fill(0)
  851. up()
  852. forward(30)
  853. down()
  854. width(1)
  855. color("black")
  856. # move out of the way
  857. tracer(0)
  858. up()
  859. right(90)
  860. forward(100)
  861. right(90)
  862. forward(100)
  863. right(180)
  864. down()
  865. # some text
  866. write("startstart", 1)
  867. write("start", 1)
  868. color("red")
  869. # staircase
  870. for i in range(5):
  871. forward(20)
  872. left(90)
  873. forward(20)
  874. right(90)
  875. # filled staircase
  876. fill(1)
  877. for i in range(5):
  878. forward(20)
  879. left(90)
  880. forward(20)
  881. right(90)
  882. fill(0)
  883. tracer(1)
  884. # more text
  885. write("end")
  886.  
  887. def demo2():
  888. # exercises some new and improved features
  889. speed('fast')
  890. width(3)
  891.  
  892. # draw a segmented half-circle
  893. setheading(towards(0,0))
  894. x,y = position()
  895. r = (x**2+y**2)**.5/2.0
  896. right(90)
  897. pendown = True
  898. for i in range(18):
  899. if pendown:
  900. up()
  901. pendown = False
  902. else:
  903. down()
  904. pendown = True
  905. circle(r,10)
  906. sleep(2)
  907.  
  908. reset()
  909. left(90)
  910.  
  911. # draw a series of triangles
  912. l = 10
  913. color("green")
  914. width(3)
  915. left(180)
  916. sp = 5
  917. for i in range(-2,16):
  918. if i > 0:
  919. color(1.0-0.05*i,0,0.05*i)
  920. fill(1)
  921. color("green")
  922. for j in range(3):
  923. forward(l)
  924. left(120)
  925. l += 10
  926. left(15)
  927. if sp > 0:
  928. sp = sp-1
  929. speed(speeds[sp])
  930. color(0.25,0,0.75)
  931. fill(0)
  932.  
  933. # draw and fill a concave shape
  934. left(120)
  935. up()
  936. forward(70)
  937. right(30)
  938. down()
  939. color("red")
  940. speed("fastest")
  941. fill(1)
  942. for i in range(4):
  943. circle(50,90)
  944. right(90)
  945. forward(30)
  946. right(90)
  947. color("yellow")
  948. fill(0)
  949. left(90)
  950. up()
  951. forward(30)
  952. down();
  953.  
  954. color("red")
  955.  
  956. # create a second turtle and make the original pursue and catch it
  957. turtle=Turtle()
  958. turtle.reset()
  959. turtle.left(90)
  960. turtle.speed('normal')
  961. turtle.up()
  962. turtle.goto(280,40)
  963. turtle.left(24)
  964. turtle.down()
  965. turtle.speed('fast')
  966. turtle.color("blue")
  967. turtle.width(2)
  968. speed('fastest')
  969.  
  970. # turn default turtle towards new turtle object
  971. setheading(towards(turtle))
  972. while ( abs(position()[0]-turtle.position()[0])>4 or
  973. abs(position()[1]-turtle.position()[1])>4):
  974. turtle.forward(3.5)
  975. turtle.left(0.6)
  976. # turn default turtle towards new turtle object
  977. setheading(towards(turtle))
  978. forward(4)
  979.  
  980. write_font("CAUGHT! ", font=('times', 30), move=True) # <-----
  981.  
  982.  
  983.  
  984. if __name__ == '__main__':
  985. from time import sleep
  986. demo()
  987. sleep(3)
  988. demo2()
  989. done()
Then make this modification to your code ...
  1. # note modified module turtle1.py has method write_font()
  2.  
  3. import turtle1 as turtle
  4.  
  5. def goto(x,y):
  6. turtle.up()
  7. turtle.goto(x,y)
  8. turtle.down
  9.  
  10.  
  11. turtle.tracer(False)
  12. turtle.width(3)
  13. goto(-270,270)
  14. turtle.right(90)
  15. turtle.forward(540)
  16. turtle.left(90)
  17. turtle.forward(540)
  18. turtle.left(90)
  19. turtle.forward(540)
  20. turtle.left(90)
  21. turtle.forward(540)
  22. goto(50,50)
  23. turtle.write_font('testing', font=('times', 30, 'bold'))
  24. turtle.tracer(True)
  25.  
  26. turtle.done()
Last edited by vegaseat; Dec 8th, 2007 at 2:05 am.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 6
Reputation: Agentbob is an unknown quantity at this point 
Solved Threads: 0
Agentbob Agentbob is offline Offline
Newbie Poster

Re: Turtle graph

 
0
  #3
Dec 9th, 2007
So the feature isn't implemented by default? It's a project and I doubt my professor has his files edited.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,856
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 866
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: Turtle graph

 
0
  #4
Dec 9th, 2007
Originally Posted by Agentbob View Post
So the feature isn't implemented by default? It's a project and I doubt my professor has his files edited.
You are correct. Since turtle.py imports Tkinter you may be able to use the Tkinter canvas.create_text() function, but it would be harder to implement, since you have to get the matching canvas and location.
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC