Michael Woodson
Michael Woodson

Reputation: 1

Python turtle user input for shape and color

I've been trying to write some code to allow for instantaneous results after someone inputs what color and what shape they want it to be in turtle. Basically, what I mean is that when you get prompted for a color and you say "orange", for example, then the color will instantly change to orange. Here's the code I've written:

def Turtle(shape):

if shape == "triangle":
    turtle.circle(40, steps=3)
elif shape == "square":
    turtle.circle(40, steps=4)
elif shape == "pentagon":
    turtle.circle(40, steps=5)
elif shape == "hexagon":
    turtle.circle(40, steps=6)

def Shape():

shape = eval(input("Enter a shape: "))
Turtle(shape)

def Turtle(color):

if color == "red":
    turtle.color("red")
elif color == "blue":
    turtle.color("blue")
elif color == "green":
    turtle.color("green")
elif color == "yellow":
    turtle.color("yellow")

def Color():

color = eval(input("Enter a color: "))
Turtle(color)

It works slightly. After one change is made, say the color turns to blue then it will refuse to do anything after that regardless of the entries made into the user prompts.

P.S. I'm running Python 3.5.2

Upvotes: 0

Views: 11052

Answers (1)

cdlane
cdlane

Reputation: 41872

The problem is that you really need to turn over control to the turtle listener using mainloop() and then you no longer can communicate with it via top level function calls like:

color = input("Enter a color: ")

However, since you're using Python 3, we can use the new input dialog feature to dynamically prompt for input and make changes to the current drawing:

import turtle

current_shape = "triangle"

steps = {"triangle": 3, "square": 4, "pentagon": 5, "hexagon": 6}

def onkey_shape():
    shape = turtle.textinput("Shape Selection", "Enter a shape:")
    if shape.lower() in steps:
        turtle.reset()
        set_color(current_color)
        set_shape(shape.lower())
    turtle.listen()  # grab focus back from dialog window

def set_shape(shape):
    global current_shape
    turtle.circle(40, None, steps[shape])
    current_shape = shape

current_color = "red"

colors = {"red", "blue", "green", "yellow", "orange"}

def onkey_color():
    color = turtle.textinput("Color Selection", "Enter a color:")
    if color.lower() in colors:
        turtle.reset()
        set_color(color.lower())
        set_shape(current_shape)
    turtle.listen()  # grab focus back from dialog window

def set_color(color):
    global current_color
    turtle.color(color)
    current_color = color

set_color(current_color)
set_shape(current_shape)

turtle.onkey(onkey_color, "c")
turtle.onkey(onkey_shape, "s")

turtle.listen()

turtle.mainloop()

Make the turtle window active (select it, aka give it focus) then if you press 'C' you'll get a dialog for a new color (from a fixed set) and if you press 'S' you'll get a dialog for a new shape. The code uses reset() to remove the previous drawing before making a new one with the changes.

Upvotes: 1

Related Questions