0

我正在编写一个简单的控制台应用程序,您可以在其中键入命令。一个这样的命令包括: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()到每个函数的末尾,但它仍然给了我这个例外。请帮忙!我仍然需要它才能绘图,这不是我想在我的代码中出现的错误。

4

1 回答 1

1

我想到了!您必须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命令,它会毫无怨言地执行此操作。

于 2020-05-22T21:58:56.677 回答