Kaydet (Commit) 3c7a25a4 authored tarafından Guido van Rossum's avatar Guido van Rossum

Applied SF patch #438424 by Josh Cogliati:

Python's logolike module turtle.py did not display
the turtle except when actually drawing lines.
This patch changes the turtle.py module so that
it displays the turtle at all times when tracing is
on. This is similar to the the way that logo works.
When tracing is off the turtle will not be displayed.
üst 92e2d5c7
...@@ -11,6 +11,7 @@ class RawPen: ...@@ -11,6 +11,7 @@ class RawPen:
self._canvas = canvas self._canvas = canvas
self._items = [] self._items = []
self._tracing = 1 self._tracing = 1
self._arrow = 0
self.degrees() self.degrees()
self.reset() self.reset()
...@@ -48,9 +49,15 @@ class RawPen: ...@@ -48,9 +49,15 @@ class RawPen:
self._items = [] self._items = []
for item in items: for item in items:
canvas.delete(item) canvas.delete(item)
self._delete_turtle()
self._draw_turtle()
def tracer(self, flag): def tracer(self, flag):
self._tracing = flag self._tracing = flag
if not self._tracing:
self._delete_turtle()
self._draw_turtle()
def forward(self, distance): def forward(self, distance):
x0, y0 = start = self._position x0, y0 = start = self._position
...@@ -63,6 +70,7 @@ class RawPen: ...@@ -63,6 +70,7 @@ class RawPen:
def left(self, angle): def left(self, angle):
self._angle = (self._angle + angle) % self._fullcircle self._angle = (self._angle + angle) % self._fullcircle
self._draw_turtle()
def right(self, angle): def right(self, angle):
self.left(-angle) self.left(-angle)
...@@ -87,7 +95,7 @@ class RawPen: ...@@ -87,7 +95,7 @@ class RawPen:
id = self._canvas.create_line(0, 0, 0, 0, fill=color) id = self._canvas.create_line(0, 0, 0, 0, fill=color)
except Tkinter.TclError: except Tkinter.TclError:
raise Error, "bad color string: %s" % `color` raise Error, "bad color string: %s" % `color`
self._color = color self._set_color(color)
return return
try: try:
r, g, b = color r, g, b = color
...@@ -103,7 +111,12 @@ class RawPen: ...@@ -103,7 +111,12 @@ class RawPen:
assert 0 <= b <= 1 assert 0 <= b <= 1
x = 255.0 x = 255.0
y = 0.5 y = 0.5
self._color = "#%02x%02x%02x" % (int(r*x+y), int(g*x+y), int(b*x+y)) self._set_color("#%02x%02x%02x" % (int(r*x+y), int(g*x+y), int(b*x+y)))
def _set_color(self,color):
self._color = color
self._draw_turtle()
def write(self, arg, move=0): def write(self, arg, move=0):
x, y = start = self._position x, y = start = self._position
...@@ -115,6 +128,7 @@ class RawPen: ...@@ -115,6 +128,7 @@ class RawPen:
if move: if move:
x0, y0, x1, y1 = self._canvas.bbox(item) x0, y0, x1, y1 = self._canvas.bbox(item)
self._goto(x1, y1) self._goto(x1, y1)
self._draw_turtle()
def fill(self, flag): def fill(self, flag):
if self._filling: if self._filling:
...@@ -205,20 +219,20 @@ class RawPen: ...@@ -205,20 +219,20 @@ class RawPen:
if self._filling: if self._filling:
self._path.append(self._position) self._path.append(self._position)
if self._drawing: if self._drawing:
if self._tracing: if self._tracing:
dx = float(x1 - x0) dx = float(x1 - x0)
dy = float(y1 - y0) dy = float(y1 - y0)
distance = hypot(dx, dy) distance = hypot(dx, dy)
nhops = int(distance) nhops = int(distance)
item = self._canvas.create_line(x0, y0, x0, y0, item = self._canvas.create_line(x0, y0, x0, y0,
width=self._width, width=self._width,
arrow="last",
capstyle="round", capstyle="round",
fill=self._color) fill=self._color)
try: try:
for i in range(1, 1+nhops): for i in range(1, 1+nhops):
x, y = x0 + dx*i/nhops, y0 + dy*i/nhops x, y = x0 + dx*i/nhops, y0 + dy*i/nhops
self._canvas.coords(item, x0, y0, x, y) self._canvas.coords(item, x0, y0, x, y)
self._draw_turtle((x,y))
self._canvas.update() self._canvas.update()
self._canvas.after(10) self._canvas.after(10)
# in case nhops==0 # in case nhops==0
...@@ -233,6 +247,30 @@ class RawPen: ...@@ -233,6 +247,30 @@ class RawPen:
capstyle="round", capstyle="round",
fill=self._color) fill=self._color)
self._items.append(item) self._items.append(item)
self._draw_turtle()
def _draw_turtle(self,position=[]):
if not self._tracing:
return
if position == []:
position = self._position
x,y = position
distance = 8
dx = distance * cos(self._angle*self._invradian)
dy = distance * sin(self._angle*self._invradian)
self._delete_turtle()
self._arrow = _canvas.create_line(x-dx,y+dy,x,y,
width=self._width,
arrow="last",
capstyle="round",
fill=self._color)
self._canvas.update()
def _delete_turtle(self):
if self._arrow != 0:
self._canvas.delete(self._arrow)
self._arrow = 0
_root = None _root = None
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment