修改此程序,以便在创建窗口之前提示用户输入所需的背景色



当我编写脚本并运行它时。 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。它包括一些非常不寻常的,如"桃子泡芙"和"热粉"。```

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中执行

颜色 = str(输入("背景颜色:"(( 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() 

你是如何尝试运行这段代码的? 控制+V?如果是这样,那么它肯定会认为下一行是"输入">

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

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

div class="one_answers">

我想一键运行所有这些脚本,当涉及到提示时 我为这个颜色

在这种情况下,我建议您执行以下操作:

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 (并将文件设置为可执行文件,我可以(双击(单击它并得到背景颜色的提示。

它成功地提示我输入颜色,并使用 Bash shell 成功实现各种选项。

➜ python3 stackoverflow_question.py
  Background color: peach puff
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()

相关内容

最新更新