从Python Zelle Graphics中的窗口中删除一行

时间:2013-01-05 01:29:14

标签: python graphics zelle-graphics

下面有一些代码在圆圈上绘制线条,但在每次迭代过程中都不会删除这些线条。有谁知道如何从窗口删除对象?

我试过了win.delete(l),但它没有用。谢谢。

import graphics
import math

win.setBackground("yellow")

x=0
y=0

x1=0
y1=0

P=graphics.Point(x,y)

r=150

win.setCoords(-250, -250, 250, 250)

for theta in range (360):

        angle=math.radians(theta)

        x1=r*math.cos(angle)
        y1=r*math.sin(angle)

        Q=graphics.Point(x1,y1)

        l=graphics.Line(P,Q)
        l.draw(win)

3 个答案:

答案 0 :(得分:0)

据我所知,通常我们将东西绘制到一些缓冲存储器中,然后将这个缓冲区中的东西绘制到屏幕上,你对我说的话,听起来就像你将缓冲区绘制到屏幕上,然后删除对象从缓冲区,我认为这不会影响你的屏幕。 我想你可能需要用背景颜色重绘“前一行”的部分,或者只是用你真正想要的东西重绘整个屏幕。

我没有使用过图形模块,但希望我的想法对你有所帮助。

答案 1 :(得分:0)

是的,我处于相同的位置,我找到了一个很好的解决方案:

l.undraw()

您可以在此处查看更多信息:

http://mcsp.wartburg.edu/zelle/python/graphics/graphics.pdf

答案 2 :(得分:0)

您的代码没有按照发布的方式运行,因此请将其重新整理为包含@ oglo&x undraw()建议的完整解决方案:

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

这样可以提供更粗的线条和更少的闪烁。

相关问题