繁体   English   中英

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

[英]Remove a line from the window in Python 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)

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

我尚未使用图形模块,但希望我的想法对您有所帮助。

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

l.undraw()

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

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

您的代码无法按发布的方式运行,因此让我们将其重新处理为包含@oglox的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

这样可以使线条看起来更粗,而闪烁也更少。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM