[英]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()
您可以在此處查看更多信息:
您的代碼無法按發布的方式運行,因此讓我們將其重新處理為包含@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.