Draw lines at the respective positions clicked by the mouse using Turtle Last Updated : 10 May, 2024 Comments Improve Suggest changes Like Article Like Report In this article, we will learn how to draw lines at any position which is clicked by the mouse using a turtle module. Turtle graphics: turtle.listen(): Using this then we can give keyboard inputsturtle.onscreenclick(func,1 or 3): This function is used to bind function to a mouse-click event on canvas. 1 means left click and 3 means to right-click.pen.goto(x coordinate , y coordinate): Moves the turtle from the current position to the location x, y along the shortest linear path between the two locations (i.e. a direct line between the current position and (x,y))pen.color( colour ): This method is used to change the color of the turtlepen.shape(shape): This method is used to give the shape to the turtlehead.penup: Picks the pen up so the turtle does not draw a line as it moveshead.hideturtle: This method is used to make the turtle invisible. It’s a good idea to do this while you’re in the middle of a complicated drawing because hiding the turtle speeds up the drawing observably. This method does not require any argument.head.clear: This function is used to delete the turtle's drawings from the screenhead.write: This function is used to write text at the current turtle position.Approach: Import the turtle modules.Define two instances for the turtle pen and head.Head is to tell currently at which coordinate mouse is clicked.Give shape and color to the instanceDefine the function btnclick which takes two parameters x and y coordinates, now use the function goto() inside this function to take the turtle to x and y coordinates passed in the parameter.Use onscreenclick to bind btnclick function with the mouse click on the screen.Use function listens to perform the above specified task.Below is the Python implementation of the above approach: Python # python program # import for turtle module import turtle # defining instance of turtle pen=turtle.Turtle() head=turtle.Turtle() head.penup() head.hideturtle() head.goto(0, 260) head.write("This is to display the coordinates of the position where mouse is clicked", align = "center", font = ("courier", 12, "normal")) # giving circle shape to pen i.e. instance of turtle pen.shape("circle") # giving colour to turtle pen.color("red") # define function to make the turtle move # to the position which is clicked by the mouse def btnclick(x, y): pen.goto(x, y) head.clear() head.write(f"x coordinate = {x}, y coordinate = {y}", align = "center", font = ("courier", 24, "normal")) # this function will call btnclick whenever mouse is clicked turtle.onscreenclick(btnclick, 1) turtle.listen() turtle.mainloop() Output: Comment More infoAdvertise with us Next Article Interview Preparation For Software Developers K kashishmishra9911 Follow Improve Article Tags : Python 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