【问题标题】:Remove a line from the window in Python Zelle Graphics从 Python Zelle Graphics 的窗口中删除一行
【发布时间】:2017-03-21 02:42:15
【问题描述】:

我在下面有一些代码在一个圆圈上画线,但在每次迭代期间这些线都不会被删除。有谁知道如何从窗口中删除对象?

我尝试了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)

【问题讨论】:

    标签: python graphics zelle-graphics


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

      l.undraw()
      

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

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

      【讨论】:

        【解决方案3】:

        您的代码没有按照发布的那样运行,所以让我们将其重新编写成一个完整的解决方案,并结合@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
        

        这会使线条看起来更粗,闪烁也稍微少一些。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2018-06-14
          • 2016-01-04
          • 1970-01-01
          • 1970-01-01
          • 2011-05-03
          • 1970-01-01
          • 2013-07-14
          相关资源
          最近更新 更多