Open In App

Python Turtle – Graphics Keyboard Commands

Last Updated : 16 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Python Turtle module is a graphical tool that can be used to draw simple graphics on the screen using a cursor. Python Turtle was a part of Logo programming language which had similar purpose of letting the users draw graphics on the screen with the help of simple commands. Turtle is a pre-installed module and has inbuilt commands and features that can be used to draw pictures on the screen. This article will be primarily focused on creating a graphic using keyboard commands along with how the same methodology can be used to add or change color to the graphic.

Functions Used:

  • Screen() – used to create a canvas for drawing
  • Turtle Motion:
    • forward(distance) | fd(distance) : move the turtle forward
    • backward(distance) | back(distance) | bk(distance) : move the turtle backwards
    • right(distance) | rt(distance) : move the turtle right
    • left(distance) | lt(distance) : move the turtle left
    • circle(radius) : draws a circle with a given radius
  • Coloring:
    • color() : set the colors
    • begin_fill() : this method is called before drawing a shape that is to be filled
    • end_fill() : Fills the shape drawn after the call to begin_fill().

Given below are two approaches that deal and discuss how to create a graphics keyboard

Method 1

Approach

  • Import module and submodules
  • Create setup- The setup() method sets up a window of size 500×500.
  • Create window- The Screen() method creates a canvas for drawing.
  • Instantiate turtle object
  • Set turtle speed to 0 which is maximum
  • Set visibility- The showturtle() method sets the visibility of the turtle.
  • In order to capture the keystrokes we need to define few functions namely up, down, left, right. By default, the turtle points to the right.
    • The setheading() method changes the orientation of the turtle to the given angle.
    • The forward() method moves the turtle to the specified distance.
    • The listen() method sets focus on the turtle screen to capture events.
    • The onkey() method invokes the method specific to the captured keystroke. The first argument of onkey() is the function to be called and the second argument is the key.
    • The Up,Down,Left and Right are the corresponding arrow keys on the keyboard.
  • Add mainloop() command, it prevents the application from terminating before the user actually clicks the exit option.

Program

Python3




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

Method 2: changing color

This is similar to the previous example with the addition of few more keys. Now we have added keys to change the color of the line. 

  • If the user presses r it turns red,
  • If g it turns green and if b it turns blue.
  • For resetting the line color to black the user must press z.

Also, the thickness of the line is increased by setting the width o the turtle to 5px using the width() method.

Program

Python3




import turtle
from turtle import *
 
setup(500, 500)
Screen()
turtle = turtle.Turtle()
turtle.speed(0)
turtle.width(5)
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)
 
 
def r():
    turtle.color("red")
 
 
def g():
    turtle.color("green")
 
 
def b():
    turtle.color("blue")
 
 
def z():
    turtle.color("black")
 
 
listen()
onkey(up, 'Up')
onkey(down, 'Down')
onkey(left, 'Left')
onkey(right, 'Right')
onkey(z, "z")
onkey(r, 'r')
onkey(g, 'g')
onkey(b, 'b')
 
mainloop()


Output



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads