我正在编写一个简单的控制台应用程序,您可以在其中键入命令。这样的命令之一包括: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()
附加到每个函数的末尾,但是仍然给我这个异常。请帮忙!我仍然需要它才能绘制图形,这不是我想要在代码中包含的错误。
答案 0 :(得分:0)
我知道了!您必须在代码中使用turtle.TurtleScreen._RUNNING = True
。这是我的新代码(主代码):
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:
if commands[1].lower() == "square":
t = turtle.Turtle()
fn.draw_rect(t)
elif commands[1].lower() == "circle":
t = turtle.Turtle()
fn.draw_circle(t)
elif commands[1].lower() == "triangle":
t = turtle.Turtle()
fn.draw_triangle(t)
else:
txt.print_red("Invalid shape!")
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()
t.screen.exitonclick()
turtle.TurtleScreen._RUNNING = True
def draw_circle(t):
t.circle(50)
time.sleep(3)
turtle.clearscreen()
t.screen.exitonclick()
turtle.TurtleScreen._RUNNING = True
def draw_triangle(t):
t.forward(100)
for i in range(0, 2):
t.left(120)
t.forward(100)
time.sleep(3)
turtle.clearscreen()
t.screen.exitonclick()
turtle.TurtleScreen._RUNNING = True
现在,如果您在输入draw square
或其他内容后关闭了龟窗口,然后重新输入
draw
命令,它不会抱怨。