2013-01-05 19 views
0

我在下面有一些代碼在圓上繪製線條,但在每次迭代過程中不會刪除線條。有誰知道如何從窗口中刪除對象?在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) 

回答

0

據我所知,通常我們借鑑的東西一定緩衝存儲器,然後繪製的東西,在這個緩衝區到屏幕上,你說什麼,對我來說,聽起來像你畫緩衝區到屏幕上,然後從緩衝區中刪除對象,我認爲這不會影響你的屏幕。 我想你可能需要用背景顏色重新繪製'上一行'的部分,或者只是用你真正想要的重新繪製整個屏幕。

我還沒有使用圖形模塊,但希望我的想法對你有幫助。

0

你的代碼不能運行張貼所以讓我們把它改寫成一個完整的解決方案,其中包含@ oglo's 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() 

這呈現出稍微飄渺的閃爍線條。我們可以通過繪畫和以相反的順序undrawing略好做:

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 

這給出了一個更厚尋找線和略少閃爍。