簡體   English   中英

修改此程序,使其在創建 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. 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找到允許的顏色名稱列表。它包括一些非常不尋常的名稱,如“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() 

您如何嘗試運行此代碼? 控制+ V? 如果是這樣,那么它肯定會認為下一行是“輸入”

嘗試將其復制到文件(例如:turtle_test.py), python turtle_test.py在powershell中運行python turtle_test.py 我只是這樣做了,它正常運行了(考慮到最后一行是錯誤的)。

最后一件事(對於python2):應該使用turtle.mainloop()代替window.mainloop() 雖然在python3中是正確的

它成功提示我輸入顏色,並使用Bash Shell成功實現了各種選項。

➜ python3 stackoverflow_question.py
  Background color: peach puff

我想一鍵運行所有這些腳本,然后提示我輸入這種顏色

在這種情況下,我建議您執行以下操作:

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 )並將文件設置為可執行文件,則可以(雙擊)單擊它並提示輸入背景色。

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM