为什么升华文本3在完成后会自动关闭输出窗口

2024-10-03 09:12:24 发布

您现在位置:Python中文网/ 问答频道 /正文

# ColorSpiralInput.py

import turtle
t = turtle.Pen()
turtle.bgcolor('black')
# Set up a list of any 8 valid Python color names
colors = ['red', 'yellow', 'blue', 'green', 'orange', 'purple', 'white', 'gray']
# Ask the user for the number of sides, between 1 and 8, with a default of 4
sides = int(turtle.numinput('Number of sides', 'How many sides do you want (1-8)?', 
4, 1, 8))
# Draw a colorful spiral with the user-specified number of sides
for x in range(360):
    t.pencolor(colors[x%sides]) # Only use the right number of colors
    t.forward(x*3/sides+x)      # Change the size to match number of sides
    t.left(360/sides+1)         # Turn 360 degrees / number of sides, plus 1
    t.width(x*sides/200)        # Make the pen larger as it goes outward

当我在sublime text 3中运行此代码时,程序运行后输出窗口立即关闭。我无法查看结果,然后手动关闭窗口

我已经检查了我的代码,但我没有解释。我写过其他脚本,但我没有遇到编辑器的相同行为

可能是我必须更改一些设置吗


Tags: ofthe代码pyimportnumberforwith
1条回答
网友
1楼 · 发布于 2024-10-03 09:12:24

对于在控制台中运行的通用Python turtle,执行的最终语句通常是mainloop()或其变体之一done()exitonclick()。这将控制权移交给底层tkinter事件处理程序

但是,当在IDE(如IDLE、PyCharm、SublimateText等)下运行时,规则会发生变化,并且不一致。有些人需要最终的mainloop(),有些人不想要,有些人把它变成了禁止操作。下面是我如何编写在控制台下运行的程序:

# ColorSpiralInputurtle.py

from turtle import Screen, Turtle

# Set up a list of any 8 valid Python color names
COLORS = ['red', 'yellow', 'blue', 'green', 'orange', 'purple', 'white', 'gray']

screen = Screen()
screen.bgcolor('black')

# Ask the user for the number of sides, between 1 and 8, with a default of 4
sides = int(screen.numinput('Number of sides', 'How many sides do you want (1-8)?', 4, 1, 8))

turtle = Turtle()
turtle.speed('fastest')  # because I have no patience

# Draw a colorful spiral with the user-specified number of sides
for x in range(360):
    turtle.pencolor(COLORS[x % sides])  # Only use the right number of colors
    turtle.forward(x * 3 / sides + x)  # Change the size to match number of sides
    turtle.left(360 / sides + 1)  # Turn 360 degrees / number of sides, plus 1
    turtle.width(x * sides / 200)  # Make the pen larger as it goes outward

screen.exitonclick()

如果您的IDE拒绝mainloop()done()和/或exitonclick(),您可以始终使用“告诉用户在查看完您的输出后键入”,并在代码中放入最后的input()调用

相关问题 更多 >