[英]Why do I keep getting a Terminator error for Python Turtle?
我正在編寫一個簡單的控制台應用程序,您可以在其中鍵入命令。 一個這樣的命令包括: draw
。 draw
使用 Python 龜模塊和我自己模塊的一些功能。 這是我的主要代碼(其中一些被屏蔽了,因為它不處理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()
附加到每個function 的末尾,但它仍然給了我這個例外。 請幫忙。 我仍然需要它才能繪圖,這不是我想在我的代碼中出現的錯誤。
我想到了! 您必須在代碼中使用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
或其他內容后關閉海龜 window,然后重新輸入draw
命令,它會毫無怨言地執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.