我正在编写一个简单的控制台应用程序,您可以在其中键入命令。一个这样的命令包括:draw
. draw
使用 Python turtle 模块和我自己的模块中的一些函数。这是我的主要代码(其中一些因不处理draw
命令而被屏蔽):
import os
import datetime
import webbrowser
import random
import turtle
import functions as fn # grabs functions from functions.py
import text_colors as txt
is_running = True
while is_running:
[some code...]
elif commands[0].lower() == "draw":
try:
t = turtle.Turtle()
if commands[1].lower() == "square":
fn.draw_rect(t)
elif commands[1].lower() == "circle":
fn.draw_circle(t)
elif commands[1].lower() == "triangle":
fn.draw_triangle(t)
else:
txt.print_red("Invalid shape!")
turtle.done()
except IndexError:
txt.print_red("No shape was provided!")
[some more code...]
这是定义我的函数的模块:
import turtle
import time
def draw_rect(t):
for i in range(0, 4):
t.forward(100)
t.right(90)
time.sleep(3)
turtle.clearscreen()
def draw_circle(t):
t.circle(50)
time.sleep(3)
turtle.clearscreen()
def draw_triangle(t):
t.forward(100)
for i in range(0, 2):
t.left(120)
t.forward(100)
time.sleep(3)
turtle.clearscreen()
当我运行它时,控制台运行得很好,当我键入draw square
打开 Turtle 并绘制一个正方形时,它运行得很好。但是,当我关闭 Turtle 并重新输入时draw square
,我得到了这个大错误:
Traceback (most recent call last):
File "kshell_base.py", line 209, in <module>
t = turtle.Turtle()
File "C:\Users\keega\AppData\Local\Programs\Python\Python38\lib\turtle.py", line 3813, in __init__
RawTurtle.__init__(self, Turtle._screen,
File "C:\Users\keega\AppData\Local\Programs\Python\Python38\lib\turtle.py", line 2557, in __init__
self._update()
File "C:\Users\keega\AppData\Local\Programs\Python\Python38\lib\turtle.py", line 2660, in _update
self._update_data()
File "C:\Users\keega\AppData\Local\Programs\Python\Python38\lib\turtle.py", line 2646, in _update_data
self.screen._incrementudc()
File "C:\Users\keega\AppData\Local\Programs\Python\Python38\lib\turtle.py", line 1292, in _incrementudc
raise Terminator
turtle.Terminator
我试过附加turtle.done()
到每个函数的末尾,但它仍然给了我这个例外。请帮忙!我仍然需要它才能绘图,这不是我想在我的代码中出现的错误。