微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

我正在尝试基本上制作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(...)。它将解决上述问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。