有没有办法可以在程序中途打开乌龟屏幕?

时间:2020-06-10 01:57:10

标签: python python-turtle

我有点菜鸟,并且制作了一个项目,在该项目中我需要乌龟图形屏幕弹出到程序结尾(我需要获得用户输入才能显示需要显示的内容)。在定义屏幕的设置之前,我尝试过先保留其余代码,但是乌龟屏幕从不弹出(这是我期望的)。有什么办法可以解决这个问题,或者有人对显示乌龟的功能有其他建议吗?我做了

conda env config vars set MPLCONFIGDIR=$HOME/.matplotlib/condaenv_main

然后是代码的第一部分,一些输入和其他内容,看起来像这样

import turtle

然后设置乌龟屏幕

while c == 1:
    if a == "just bring me to the composition viewer":
        print("k")
        break
# I did try using a goto(2) command and then putting line = 2 after doing
# def goto(linenum):
    # global line
    # line = linenum 
# over the turtle setup instead of break, but it was the same result
    if a == "+help":
        print()
        words3 = "What would you like help with? press +help again to clos"
        for char in words3:
            time.sleep(0.008)
            sys.stdout.write(char)
            sys.stdout.flush()
        howcanihelp = input("e ")
        if howcanihelp.find("code") == -1:
            codehelp = 1
            while codehelp == 1:
                words4 = "Separate the notes from one another with commas, separate note duration from pitch with a " \
                    "space, note length is determined by how many fit in a measure of 4/4."
    else:
        note_pitch = a.split(", ")
        note_duration = note_pitch[1::2]
        note_pitch = [v for i, v in enumerate(note_pitch) if i % 2 == 0]
        print(note_pitch)
        print(note_duration)
        note_one = [note_pitch[0] + " " + note_duration[0]]
        print(note_one)
        break

,它不会弹出。之后,我有了一些移动命令和典型的乌龟命令,这些命令在一切开始时就可以正常工作:

wn = turtle.Screen()
wn.title("Composition viewer")
wn.bgcolor("white")
wn.setup(width=800, height=1000, startx=1100, starty=0)
turtle.speed(0)

2 个答案:

答案 0 :(得分:0)

您要寻找的东西称为textinput()

import turtle

screen = turtle.Screen()

answer = screen.textinput("Text Input Popup", "Quit Now?")

if answer is None or answer.lower().startswith('y'):
    print("Goodbye!")
    screen.clear()
    screen.bye()
else:
    print("Return to game!")

答案 1 :(得分:0)

对于使用textinput()numinput(),我通常会同意@bashBedlam。忽略:

直到您执行第一个与乌龟相关的命令,乌龟屏幕才会弹出。您可以进行各种控制台输入和计算,当您准备就绪时,可以调用screen = turtle.Screen()之类的东西来使海龟爬行。

如果这种方法不起作用,请在您的问题中显示代码,以便我们查看出现问题的地方。