您的代码没有按照发布的方式运行,所以让我们将其重新编写成一个包含@ogloxundraw()
建议的完整解决方案:
import math
import graphics
win = graphics.GraphWin(width=500, height=500)
win.setCoords(-250, -250, 250, 250)
win.setBackground("yellow")
CENTER = graphics.Point(0, 0)
RADIUS = 150
line = None
for theta in range(360):
angle = math.radians(theta)
x = RADIUS * math.cos(angle)
y = RADIUS * math.sin(angle)
point = graphics.Point(x, y)
if line: # None is False in a boolean context
line.undraw()
line = graphics.Line(CENTER, point)
line.draw(win)
win.close()
这呈现出一条有点纤细、闪烁的线条。通过以相反的顺序绘制和取消绘制,我们可以做得更好:
old_line = None
for theta in range(360):
angle = math.radians(theta)
x = RADIUS * math.cos(angle)
y = RADIUS * math.sin(angle)
point = graphics.Point(x, y)
new_line = graphics.Line(CENTER, point)
new_line.draw(win)
if old_line: # None is False in a boolean context
old_line.undraw()
old_line = new_line
这使线条看起来更粗,闪烁略少。