Python Turtle - Graphics Keyboard Commands Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like 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 drawingTurtle Motion:forward(distance) | fd(distance) : move the turtle forwardbackward(distance) | back(distance) | bk(distance) : move the turtle backwardsright(distance) | rt(distance) : move the turtle rightleft(distance) | lt(distance) : move the turtle leftcircle(radius) : draws a circle with a given radiusColoring:color() : set the colorsbegin_fill() : this method is called before drawing a shape that is to be filledend_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 submodulesCreate setup- The setup() method sets up a window of size 500x500.Create window- The Screen() method creates a canvas for drawing.Instantiate turtle objectSet turtle speed to 0 which is maximumSet 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 Comment More info S Shreyasi_Chakraborty Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2020 Python-turtle Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 5 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 7 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 2 min read Python MySQL 9 min read Python Packages 12 min read Python Modules 7 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 6 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 15+ min read StatsModel Library- Tutorial 4 min read Learning Model Building in Scikit-learn 8 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 7 min read Python | Build a REST API using Flask 3 min read How to Create a basic API using Django Rest Framework ? 4 min read Python PracticePython Quiz 3 min read Python Coding Practice 1 min read Python Interview Questions and Answers (2025) 3 min read Like