当我编写脚本并运行它时。 Python Terminal starts doing it, but when it comes to prompting a color my program skips this step.
目标是:Modify this program so that before it creates the window, it prompts the user to enter the desired background
color. It should store the user’s responses in a variable, and modify the color of the window according to the
user’s wishes.
(提示:您可以在http://www.tcl.tk/man/tcl8.4/TkCmd/colors处找到允许的颜色名称列表。
htm。它包括一些非常不寻常的内容,例如“桃子粉扑”和“ HotPink”。)`
I mean that I want to run all this script in one click and when it comes to prompt me for this color it have to stop and wait for my input.
在Powershell中执行
color = str(input(“背景色:”))
It thinks that input is the next line ---> Background color: window = turtle.Screen()
import turtle
color = str(input("Background color: "))
window = turtle.Screen()
window.bgcolor(color)
window.title("Hello, Tess!")
tess = turtle.Turtle()
tess.color("blue")
tess.pensize(3)
tess.forward(50)
tess.left(120)
tess.forward(50)
window.mainloop()
答案 0 :(得分:1)
您如何尝试运行此代码?控制+ V? 如果是这样,那么它肯定会认为下一行是“输入”
尝试将其复制到文件(例如:turtle_test.py),然后在Powershell中运行refresh tokens
。我只是这样做了,它正常运行了(考虑到最后一行是错误的)。
最后一件事(对于python2):应该使用python turtle_test.py
代替turtle.mainloop()
。不过在python3中是正确的
答案 1 :(得分:0)
它成功提示我输入颜色,并使用Bash shell成功实现了各种选项。
➜ python3 stackoverflow_question.py
Background color: peach puff
答案 2 :(得分:0)
我想一键运行所有这些脚本,并在出现提示时 我为这个颜色
在这种情况下,我建议您这样做:
from turtle import Screen, Turtle
window = Screen()
color = None
while color is None:
color = window.textinput("Choose a background color", "Color:")
window.bgcolor(color)
window.title("Hello, Tess!")
tess = Turtle()
tess.color("blue")
tess.pensize(3)
tess.forward(50)
tess.left(120)
tess.forward(50)
window.mainloop()
textinput()
方法是Python 3中引入的,使控制台脱离了交互。在我的Unix系统上,如果我添加魔术注释第一行(例如#! /usr/local/bin/python3
)并将文件设置为可执行文件,则可以(双击)单击它并提示输入背景色。