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 infoAdvertise with us Next Article Interview Preparation For Software Developers S Shreyasi_Chakraborty Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2020 Python-turtle Practice Tags : python Similar Reads Interview PreparationInterview Preparation For Software DevelopersMust Coding Questions - Company-wise Must Do Coding Questions - Topic-wiseCompany-wise Practice ProblemsCompany PreparationCompetitive ProgrammingSoftware Design-PatternsCompany-wise Interview ExperienceExperienced - Interview ExperiencesInternship - Interview ExperiencesPractice @GeeksforgeeksProblem of the DayTopic-wise PracticeDifficulty Level - SchoolDifficulty Level - BasicDifficulty Level - EasyDifficulty Level - MediumDifficulty Level - HardLeaderboard !!Explore More...Data StructuresArraysLinked ListStackQueueBinary TreeBinary Search TreeHeapHashingGraphAdvance Data StructuresMatrixStringAll Data StructuresAlgorithmsAnalysis of AlgorithmsSearching AlgorithmsSorting AlgorithmsPattern SearchingGeometric AlgorithmsMathematical AlgorithmsRandomized AlgorithmsGreedy AlgorithmsDynamic ProgrammingDivide & ConquerBacktrackingBranch & BoundAll AlgorithmsProgramming LanguagesCC++JavaPythonC#Go LangSQLPHPScalaPerlKotlinWeb TechnologiesHTMLCSSJavaScriptBootstrapTailwind CSSAngularJSReactJSjQueryNodeJSPHPWeb DesignWeb BrowserFile FormatsComputer Science SubjectsOperating SystemsDBMSComputer NetworkComputer Organization & ArchitectureTOCCompiler DesignDigital Elec. & Logic DesignSoftware EngineeringEngineering MathematicsData Science & MLComplete Data Science CourseData Science TutorialMachine Learning TutorialDeep Learning TutorialNLP TutorialMachine Learning ProjectsData Analysis TutorialTutorial LibraryPython TutorialDjango TutorialPandas TutorialKivy TutorialTkinter TutorialOpenCV TutorialSelenium TutorialGATE CSGATE CS NotesGate CornerPrevious Year GATE PapersLast Minute Notes (LMNs)Important Topic For GATE CSGATE CoursePrevious Year Paper: CS exams Like