修改此程序,使其在创建 window 之前提示用户输入所需的背景颜色

Modify this program so that before it creates the window, it prompts the user to enter the desired background color

当我编写脚本并 运行 它时。 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 找到允许的颜色名称列表。 嗯。它包括一些非常不寻常的,比如“peach puff”和“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("Background color: ")) 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() 

它成功提示我输入颜色并使用 Bash shell.

成功实现了各种选项
➜ python3 Whosebug_question.py
  Background color: peach puff

您如何尝试 运行 此代码?控制+V? 如果是这样,那么它肯定会认为下一行是 "input"

尝试将其复制到文件(例如:turtle_test.py)并在您的 powershell 中 运行ning python turtle_test.py。我只是这样做了,它通常 运行(有点,考虑到最后一行是错误的)。

最后一件事(对于 python2):应该使用 turtle.mainloop() 而不是 window.mainloop()。在 python3 中是正确的,但是

I want to run all this script in one click and when it comes to prompt me for this color

在这种情况下,我建议你这样做:

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()方法是在Python3中引入的,将控制台从交互中取出来。在我的 Unix 系统上,如果我在第一行添加魔法注释(例如 #! /usr/local/bin/python3)并将文件设置为可执行文件,我可以(双击)单击它并得到背景颜色提示。

import turtle 

wn = turtle.Screen()     
bkgnd_colour = str(input("Enter your background colour"))  
wn.bgcolor(bkgnd_colour) 


   
tess = turtle.Turtle()  
t_colour = str(input("Enter your desired colour of the Turtle"))  
tess.color(t_colour)

s_ize = str(input("Enter the width of your pen"))  
tess.pensize(int(s_ize))
         
tess.forward(50)  
tess.left(120)  
tess.forward(50)  
tess.left(120)  
tess.forward(50)

wn.exitonclick()                
#it solves the both problems with input and the other instances of the 
#turtles.
#It take the input from the user for the first turtle name 'anish' and the  
#other instance 'nainesh' have his own attributes.

from tkinter import mainloop
import turtle
wn = turtle.Screen()
bg_color = str(input("Enter your desire color for your background: "))
wn.bgcolor(bg_color)

title_left = str(input("Enter your desire title for the window: "))
wn.title(title_left)
anish = turtle.Turtle()
anish_color = str(input("Enter the color of the turtle: "))
anish.color(anish_color)
anish_pen_size = str(input("Enter the pen size of the turtle: "))
anish.pensize(anish_pen_size)

anish.forward(50)
anish.left(90)
anish.forward(30)
anish.left(90)
anish.forward(100)
anish.left(90)
anish.forward(30)
anish.left(90)
anish.forward(50)

nainesh = turtle.Turtle()
nainesh.color("black")
nainesh.pensize(7)

nainesh.forward(100)
nainesh.left(90)
nainesh.forward(100)
wn.mainloop()