Skip to content
Advertisement

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

When I write a script and run it. Python Terminal starts doing it, but when it comes to prompting a color my program skips this step.

The goal is: 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.(Hint: you can find a list of permitted color names at http://www.tcl.tk/man/tcl8.4/TkCmd/colors. htm. It includes some quite unusual ones, like “peach puff” and “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.

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

Advertisement

Answer

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

In this case, I recommend you do:

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

The textinput() method was introduced in Python 3 and takes the console out of the interaction. On my Unix system, if I add magic comment first line (e.g. #! /usr/local/bin/python3) and set the file to executable, I can (double) click on it and get prompted for the background color.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement