Question

I have some code below that draws lines on a circle but the lines aren't deleted during each iteration. Does anyone know how to delete object from the window?

I tried win.delete(l) but it didn't work. Thanks.

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)

No correct solution

OTHER TIPS

As far as I know, normally we draw things to some buffer memory, then draw the stuff in this buffer to the screen, what you said, to me, sounds like you draw the buffer to the screen, then delete the object from the buffer, I think this won't affect your screen. I think you may need to redraw the part of the 'previous' line with background color, or just redraw the whole screen with what you really want.

I haven't used the graphics module, but hope my idea helpful to you.

Yeah I was in the same position, I found a good solution:

l.undraw()

You can check for more information here:

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

Your code doesn't run as posted so let's rework it into a complete solution incorporating @oglox's undraw() suggestion:

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()

This presents a somewhat wispy, flickering line. We can do slightly better by drawing and undrawing in the reverse order:

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

This gives a thicker looking line and slightly less flicker.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top