Python 关闭屏幕上的乌龟错误 - 来自如何像计算机科学家一样思考的代码:与 Python 一起学习 3
Python turtle error on closing screen - code from How to Think Like a Computer Scientist: Learning with Python 3
当我运行这个代码
import turtle
import time
def show_poly():
try:
win = turtle.Screen()
tess = turtle.Turtle()
n = int(input("How many sides do you want in your polygon?"))
angle = 360 / n
for i in range(n):
tess.forward(10)
tess.left(angle)
time.sleep(3)
finally:
win.bye()
show_poly()
show_poly()
show_poly()
我得到的第一个调用比我得到的这个错误更正常
Traceback (most recent call last):
File "/home/turte.py", line 19,
in show_poly()
File "/home/turte.py", line 8, in show_poly
tess = turtle.Turtle()
File "/usr/lib/python3.5/turtle.py", line 3816, in init
visible=visible)
File "/usr/lib/python3.5/turtle.py", line 2557, in init
self._update()
File "/usr/lib/python3.5/turtle.py", line 2660, in _update
self._update_data()
File "/usr/lib/python3.5/turtle.py", line 2646, in _update_data
self.screen._incrementudc()
File "/usr/lib/python3.5/turtle.py", line 1292, in _incrementudc
raise Terminator turtle.Terminator
如果我理解这个问题,即使关闭最后一个屏幕,我也无法创建新屏幕。
我运行python3.5
turtle.Screen()
返回的对象旨在成为单例,因此您的代码正在积极对抗模块设计。根据 the docs,您应该在应用程序中使用 RawTurtle
的实例。
import turtle
import time
import tkinter as tk
def show_poly():
try:
n = int(input("How many sides do you want in your polygon?"))
angle = 360 / n
root = tk.Tk()
canvas = turtle.ScrolledCanvas(root)
canvas.pack(expand=True, fill='both')
tess = turtle.RawTurtle(canvas)
for i in range(n):
tess.forward(10)
tess.left(angle)
time.sleep(3)
finally:
root.destroy()
show_poly()
show_poly()
show_poly()
另一种方法是在 turtle 中工作,并尽可能避免使用 tkinter。在下面的解决方案中,我们不是销毁 window 并创建一个新的,而是简单地清除它并重新绘制:
from turtle import Turtle, Screen
from time import sleep
def show_poly(turtle):
n = 0
while n < 3:
try:
n = int(input("How many sides do you want in your polygon? "))
except ValueError:
pass
angle = 360 / n
for _ in range(n):
turtle.forward(50)
turtle.left(angle)
sleep(3)
turtle.clear()
window = Screen()
tess = Turtle()
show_poly(tess)
show_poly(tess)
show_poly(tess)
window.bye()
这也应该与 Python 2.7 和 Python 3
兼容
当我运行这个代码
import turtle
import time
def show_poly():
try:
win = turtle.Screen()
tess = turtle.Turtle()
n = int(input("How many sides do you want in your polygon?"))
angle = 360 / n
for i in range(n):
tess.forward(10)
tess.left(angle)
time.sleep(3)
finally:
win.bye()
show_poly()
show_poly()
show_poly()
我得到的第一个调用比我得到的这个错误更正常
Traceback (most recent call last): File "/home/turte.py", line 19, in show_poly()
File "/home/turte.py", line 8, in show_poly tess = turtle.Turtle()
File "/usr/lib/python3.5/turtle.py", line 3816, in init visible=visible)
File "/usr/lib/python3.5/turtle.py", line 2557, in init self._update()
File "/usr/lib/python3.5/turtle.py", line 2660, in _update self._update_data()
File "/usr/lib/python3.5/turtle.py", line 2646, in _update_data self.screen._incrementudc()
File "/usr/lib/python3.5/turtle.py", line 1292, in _incrementudc
raise Terminator turtle.Terminator
如果我理解这个问题,即使关闭最后一个屏幕,我也无法创建新屏幕。 我运行python3.5
turtle.Screen()
返回的对象旨在成为单例,因此您的代码正在积极对抗模块设计。根据 the docs,您应该在应用程序中使用 RawTurtle
的实例。
import turtle
import time
import tkinter as tk
def show_poly():
try:
n = int(input("How many sides do you want in your polygon?"))
angle = 360 / n
root = tk.Tk()
canvas = turtle.ScrolledCanvas(root)
canvas.pack(expand=True, fill='both')
tess = turtle.RawTurtle(canvas)
for i in range(n):
tess.forward(10)
tess.left(angle)
time.sleep(3)
finally:
root.destroy()
show_poly()
show_poly()
show_poly()
另一种方法是在 turtle 中工作,并尽可能避免使用 tkinter。在下面的解决方案中,我们不是销毁 window 并创建一个新的,而是简单地清除它并重新绘制:
from turtle import Turtle, Screen
from time import sleep
def show_poly(turtle):
n = 0
while n < 3:
try:
n = int(input("How many sides do you want in your polygon? "))
except ValueError:
pass
angle = 360 / n
for _ in range(n):
turtle.forward(50)
turtle.left(angle)
sleep(3)
turtle.clear()
window = Screen()
tess = Turtle()
show_poly(tess)
show_poly(tess)
show_poly(tess)
window.bye()
这也应该与 Python 2.7 和 Python 3
兼容