Python turtle input with examples

In this Python tutorial, we will learn How to get input from the user in Python Turtle and we will also cover different examples related to Turtle Input. And we will cover these topics.

  • Python turtle input
  • Python turtle input box
  • Python turtle input color
  • Python turtle input function
  • Python turtle user input
  • Python turtle keyboard input
  • Python turtle screen input
  • Python turtle raw_input

Python turtle input

In this section, we will learn about how to get input from the user in Python turtle.

Before moving forward we should have a piece of knowledge about input.

Input is defined as a place where the information is entered and this information is stored or given back in the form of output.

Here we can get the input from the user and after getting the input we see the output on the screen.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle as tur.

  • tur.shape(‘turtle’) is used to give the turtle shape to pen.
  • tur.forward(50) is used to move the turtle in the forward direction.
  • tur.right(360/3) is used to move the turtle in the right direction.
  • answer = input(‘ choose the shape triangle, square or pentagon’) is used for getting the input from the user.
from turtle import *
import turtle as tur
tur.shape('turtle')

def triangle():
    for i in range(3):
        tur.forward(50)
        tur.right(360/3)


def square():
    for i in range(4):
        tur.forward(50)
        tur.right(360/4)

def pentagon():
    for i in range(5):
        tur.forward(50)
        tur.right(360/5)


answer = input(' choose the shape triangle, square or pentagon')
if answer ==('triangle'):
    triangle()

elif answer == ('square'):
    square()

elif answer == ('pentagon'):
    pentagon()

else:
    print ('Input is wrong')
tur.done()

Output:

After running the above code we get the following output in which we can see a command prompt where the user enters their input and see the result.

Python turtle input
Python turtle input

After getting the input from the user we can see a square shape is drawn on the screen with the help of a turtle.

Python turtle input Output
Python turtle input Output

Read Python turtle draw letters

Python turtle input box

In this section, we will learn about the turtle input box in python turtle.

The input box is similar to a dialog box that requests the user to enter the value, text in the input box that uses the info as necessary. An input also displays the message to show the request that is done by the user.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle.

  • ws = turtle.Screen() is used to show the inbox box.
  • turtle.textinput(“Python Guides”, “Enter your Name”) is used to enter the input in the input box.
from turtle import *

import turtle
 

ws = turtle.Screen()
ws.setup(400, 300)
turtle.textinput("Python Guides", "Enter your Name")

Output:

After running the above code we get the following output in which we can see the input box where the user can enter the value or text. After entering the text click on ok the value or text save and display it as a message on the screen.

Python turtle input box
Python turtle input box Output

Read Python Turtle Mouse

Python turtle input color

In this section, we will learn about the turtle input color in python turtle.

READ:  Python Turtle Cheat Sheet

As we know color is used for giving the attractive look to the object which attracts the user’s eye.

In turtle, the input color user can choose the color and enter the chosen color in the input box and see the output.

Code:

In the following code, we will import the turtle libraries from turtle import *, import turtle.

  • currentshape = “pentagon” is used to show the current shape that the user can already enter in this that will show on the screen.
  • step = {“triangle”: 3, “square”: 4, “pentagon”: 5, “hexagon”: 6} is used for chosen the shape by user.
  • tur.textinput(“Shape Selection”, “Enter a shape:”) is used for enter the text in the input box.
  • setcolor(currentcolor) is used to set the color of the shape.
  • tur.circle(40, None, step[shape]) is used to give the circle shape.
  • currentcolor = “purple” is used to give the color to the current shape.
  • tur.textinput(“Color Selection”, “Enter a color:”) is used give the color to the current shape.
from turtle import *
import turtle as tur

currentshape = "pentagon"

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

def onkeyshape():
    shape = tur.textinput("Shape Selection", "Enter a shape:")
    if shape.lower() in step:
        tur.reset()
        setcolor(currentcolor)
        setshape(shape.lower())
    tur.listen()  

def setshape(shape):
    global currentshape
    tur.circle(40, None, step[shape])
    currentshape = shape

currentcolor = "purple"

colors = {"pink", "cyan", "light blue", "red", "purple"}

def onkeycolor():
    color = tur.textinput("Color Selection", "Enter a color:")
    if color.lower() in colors:
        tur.reset()
        setcolor(color.lower())
        setshape(currentshape)
    tur.listen()  

def setcolor(color):
    global currentcolor
    tur.color(color)
    currentcolor = color

setcolor(currentcolor)
setshape(currentshape)

tur.onkey(onkeycolor, "c")
tur.onkey(onkeyshape, "s")

tur.listen()

tur.mainloop()

Output:

After running the above code we get the following output in which we can see the pentagon shape with purple color is shown on the screen.

Python turtle input color
Python turtle input color output

Read Python Turtle Draw Line

Python turtle input function

In this section, we will learn about how to create a turtle input function in python turtle.

Here we use turtle.textinput() function which is used to pop up a dialog box window where the user can enter the text or value and return the value after clicking the Ok button.

If the input box is canceled without entering any value or text it returns None.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle. The turtle() method is used to make objects.

  • turtle.Screen() is used to make a screen on which we can create our input box.
  • turtle.textinput(“Python Guides”, “information”) is used to popup a dialog box for entering the text or value inside the input box.
  • print(tur) is used for printing the text on the screen which the user enters in the input box.
from turtle import *

import turtle
 
turt = turtle.Screen()
turt.setup(400, 300)
tur=turtle.textinput("Python Guides", "information")
print(tur)

Output:

After running the above code we get the following output in which we can see the input box where the user can enter the text inside the input box.

Python turtle input function
Python turtle input function Output

Read Python Turtle Screen Size

Python turtle user input

In this section, we will learn about how to get the input from users in python turtle.

Here is the turtle.input() function which is used for getting the input box that gets the input from the user.

The user can enter the text or value in the input box and returns the value in the form of a string. If the input box is canceled it returns None.

READ:  Python Turtle Colors + Examples

Code:

In the following code, we will import the turtle module from turtle import *, import turtle. The turtle() method is used to make objects.

  • tur = turtle.textinput(“Enter your info”, “Name”) is used for taking input from the user.
  • print(tur) is used to print the user input value or text.
from turtle import *

import turtle
 

tur = turtle.textinput("Enter your info", "Name")
 

print(tur)

Output:

After running the above code we get the following output in which we can see the dialog box where the user can enter text or value.

Python turtle user input
Python turtle user input

After entering the value in the input box it returns the text or value as we seen in this picture.

Python turtle user input output
Python turtle user input output

Also read, Python Turtle Dot

Python turtle keyboard input

In this section, we will learn about how to get the turtle keyboard input in python turtle.

The input() function is used for entering the value into the input box. In turtle keyboard input the user can enter the input with the help of the keyboard. Here we use the up, down, left, right keys for the input.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle. The turtle() method is used to make objects.

  • turtle.speed(0) is used to give the fastest speed to the turtle.
  • turtle.forward(100) is used to move the turtle in the forward direction.
  • onkey(up, ‘Up’) is used when we press the up key the turtle move in the upward direction.
  • onkey(down, ‘Down’) is used when we press the down key the turtle move in the downward direction.
  • onkey(left, ‘Left’) is used when we press the left key the turtle move in the left direction.
  • onkey(right, ‘Right’) is used when we press the right key the turtle move in the right direction.
import turtle
from turtle import *
  
setup(500, 500)
Screen()
turtle = turtle.Turtle()
turtle.speed(0)
showturtle()

  
def up():
    turtle.setheading(90)
    turtle.forward(100)
  
def down():
    turtle.setheading(270)
    turtle.forward(100)
  
def left():
    turtle.setheading(180)
    turtle.forward(100)
  
def right():
    turtle.setheading(0)
    turtle.forward(100)
  

listen()
onkey(up, 'Up')
onkey(down, 'Down')
onkey(left, 'Left')
onkey(right, 'Right')
  
mainloop()

Output:

After running the above code we get the following output in which we can see the turtle move when we give the input from the keyboard.

python turtle keyboard input
python turtle keyboard input

Read Python turtle onclick

Python turtle screen input

In this section, we will learn about the turtle screen input in python turtle.

We get the input from the user with the help of the input() function. The user can enter the text in the input box which is present on the screen it returns the string displayed in the form of a message.

If the input box is canceled without entering any value it returns None.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle. the turtle() method is used to make objects.

  • tur.color(“cyan”) is used to give color to the turtle.
  • tur.shape(“turtle”) is used to give the turtle shape to pen.
  • tur.penup() is used to start drawing.
  • int(ws.textinput(“Python guides”, “Enter the values in pixels”)) is used to enter the value inside the input box.
  • tur.forward(ans) is used to move the turtle in the forward direction.
  • tur.backward(5) is used to move the turtle in the backward direction.
from turtle import *
import turtle as turt

ws = turt.Screen()

tur = turt.Turtle()
tur.color("cyan")
tur.shape("turtle")
tur.penup()

def goForward():
    ans = int(ws.textinput("Python guides", "Enter the values in pixels"))
    tur.forward(ans)

def goBackward():
    tur.backward(5)

turt.listen()
turt.onkey(goForward,"Up")
turt.onkey(goBackward, "Down")
turt.done()

Output:

READ:  How NumPy create nan array in Python [6 Methods]

After running the above code we get the following output in which we can see on pressing the up key the dialog box is shown on the screen. The user enters the value inside the dialog and clicks on ok.

After clicking on the Ok button we can see the turtle which is present on the screen moves 50 pixels in the forward direction.

Python turtle screen input
Python turtle screen input

Read Python Turtle Art

Python turtle raw_input

In this section, we will learn about the turtle raw_input in python turtle.

As we know input() function is used for entering trying the input from the user and returning the input in the form of a string.

The raw_input() function is similar to input() function.It reads the input and reads the string.

The raw_input() function was built in Python 2. But in python 3 we don’t have.

When we run the raw_input() function in python 3 we get an error ‘raw_input’ is not define. To fix this error replace all mention raw_input() with the input() function in your program because the raw_input() is similar to input() function.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle. The turtle() method is used to make objects.

  • tur.shape(“turtle”) is used to give the shape to the turtle.
  • input(“What color should you give the turtle be?”) is used to get the input from the user.
  • turt.Screen() is used to make the screen on which we can create our input box.
  • ws.bgcolor(backgroundcolor) is used for giving the color to background.
from turtle import *
import turtle as turt

tur = turt.Turtle()
tur.shape("turtle")

turtlecolor = input("What color should you give the turtle be?")
tur.color(turtlecolor)

ws = turt.Screen()
backgroundcolor = input("What the background color should be?")
ws.bgcolor(backgroundcolor)
turt.exitonclick()

Output:

After running the above code we get the following output in which we can see the question is mentioned on the command prompt and the answer to the following question is given by the user as in the form of input.

Python turtle raw_input
Python turtle raw_input

After getting the user input we can see the beautiful screen with black background color and a cyan color turtle is also placed on the screen.

Python turtle raw_input output
Python turtle raw_input output

You may also like the following Python turtle tutorials:

So, in this tutorial, we discussed Python Turtle Input and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.

  • Python turtle input
  • Python turtle input box
  • Python turtle input color
  • Python turtle input function
  • Python turtle user input
  • Python turtle keyboard input
  • Python turtle screen input
  • Python turtle raw_input