我如何保持 turtle.Screen() 运行 并接受移动/转动乌龟的命令?

how do i keep turtle.Screen() running AND take commands to move/ turn turtle?

我基本上是在尝试制作 LOGO 的复制版本,但采用我的风格(对我来说放轻松)。 与 LOGO 如此相似(但不完全相似)我想继续在 powershell 中输入并同时查看屏幕上的变化。 但是在这段代码中,屏幕只是打开而已,没有提示输入。帮我。 这是代码:

#----------------
# turtle paint
#----------------

import turtle


def main():
    
    bg_color = input("What backgroud color would you like?")    # taking input for bg_color
    tur_color = input("What color of turtle will you like?")

    wn = turtle.Screen()        # creates a screen for turtle drawing
    wn.bgcolor(bg_color)
    tur = turtle.Turtle()           # tur is assigned Turtle
    
    move()

    print("Thank you for using Turtle paint.")

def move():
    wn.mainloop()
    print("Turtle is facing 'RIGHT'.")
    while True:
        
        try:                # to eliminate unnecessarily errors from popping up
            q = input("Turn or move forward?\nturn/forward> ")      # turn or move forward prompt
            
            # based on the answer take input of degree and forward movement length
            if q.lower() == "turn":
                turn = input("Turn 'Right' or 'Left'?")
                degree = int(input(f"Turn {turn} by how much degree"))
                if turn.lower() == 'right':
                    tur.right(degree)
                elif turn.lower() =='left':
                    tur.left(degree)
            
            elif q.lower() == 'forward':
                movement = int(input("How much forward? type in number\n> "))
                tur.forward(movement)
        
        except:
            print('Turtle didnt catch that, try again!')
        
        # if the user want to continue or not
        # there is semantic error probably
        print("Wanna continue?")
            ans = input('> ')       
            if ans in ['yes', 'Yes', "YES", 'Y' ,'y']:
                pass
            else:
                break

if __name__ == '__main__':
    main()

尝试这样的事情:

#----------------
# turtle paint
#----------------

import turtle
from threading import Thread


def main():
    global wn, tur

    wn = turtle.Screen()
    tur = turtle.Turtle()

    move()

def move_asker():
    print("Turtle is facing \"RIGHT\".")
    while True:
        try:
            q = input("Turn or move forward?\nturn/forward/exit> ")

            if q.lower() == "turn":
                turn = input("Turn right or left? ")
                degree = int(input(f"Turn {turn} by how much degree"))
                if turn.lower() == "right":
                    tur.right(degree)
                elif turn.lower() == "left":
                    tur.left(degree)

            elif q.lower() == "forward":
                movement = int(input("How much forward? type in number\n> "))
                tur.forward(movement)

            elif q.lower() == "exit":
                wn.destroy()
                break

        except RuntimeError:
            # The user might have closed the window
            break

        except Exception as error:
            print("Error = " + repr(error))
            print("Turtle didnt catch that, try again!")

def move():
    thread = Thread(target=move_asker, daemon=True)
    thread.start()
    wn.mainloop()

if __name__ == "__main__":
    main()

move函数中,它告诉python开始执行move_asker()并继续执行wn.mainloop()

但是这种方法存在问题。其中之一是 turtle 可能会崩溃,甚至不会给您一个错误。这就是为什么不建议对线程使用 turtle 的原因。我现在测试了程序中的所有命令,它们应该可以工作。

如果你从使用 turtle 切换到使用 tkinter 之类的东西会更好(turtle 是在 tkinter 之上构建的,所以它不应该这么难)。如果这样做,则使用 Entry 而不是 input(...)。它将解决上述问题。