我有这个程序:
import graphics
import random
window = graphics.GraphWin("Dots", 700, 700)
while True:
x = random.randint(0, 700)
y = random.randint(0, 700)
p = graphics.Point(x, y)
p.draw(window)
window.close()
代码正常运行;绘制很多点,但是当我将其关闭以按" x"符号时,它显示了错误:
GraphicsError:无法绘制封闭的窗口
如何摆脱它使程序停止绘制点并进行常规关闭程序?
我也有同样的问题,我的解决方案是:
while True:
try:
# Code to run
x = random.randint(0, 700)
y = random.randint(0, 700)
p = graphics.Point(x, y)
p.draw(window)
except GraphicsError:
# When the cross is clicked and the exception occurs
break
win.close()
请注意,其他错误可能会陷入此try-except
。