简体   繁体   English

为什么我不断收到 Python Turtle 的终结者错误?

[英]Why do I keep getting a Terminator error for Python Turtle?

I'm writing a simple console application where you type commands.我正在编写一个简单的控制台应用程序,您可以在其中键入命令。 One such command includes: draw .一个这样的命令包括: draw draw uses the Python turtle module and some functions from my own module. draw使用 Python 龟模块和我自己模块的一些功能。 Here's my main code (some of it's blocked out because it doesn't handle the draw command):这是我的主要代码(其中一些被屏蔽了,因为它不处理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...]

And here's the module where my functions are defined:这是定义我的函数的模块:

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()

When I run it, the console runs it just fine, and when I type draw square to open Turtle and draw a square, it runs just perfect.当我运行它时,控制台运行得很好,当我输入draw square打开 Turtle 并绘制一个正方形时,它运行得非常完美。 However, when I close Turtle and retype draw square , I get this big fat error:但是,当我关闭 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

I've tried appending turtle.done() to the end of each function, but yet it still gives me this exception.我尝试将turtle.done()附加到每个function 的末尾,但它仍然给了我这个例外。 Please help.请帮忙。 I still need it to be able to draw and this is not a bug I want to have in my code.我仍然需要它才能绘图,这不是我想在我的代码中出现的错误。

I figured it out!我想到了! You have to use turtle.TurtleScreen._RUNNING = True in the code.您必须在代码中使用turtle.TurtleScreen._RUNNING = True Here's my new code (main code):这是我的新代码(主代码):

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...]

And here's my new module 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

Now, if you close the turtle window after typing draw square or something, and retype a draw command, it does it without complaining.现在,如果您在输入draw square或其他内容后关闭海龟 window,然后重新输入draw命令,它会毫无怨言地执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM