Python Turtle Get Position

In this tutorial, we will learn How to get a position in Python turtle and we will also cover different examples related to Get Position Turtle. And, we will cover these topics

  • Python turtle get position
  • Python turtle get X and Y position
  • Python turtle get mouse position

Python turtle get position

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

The position is able to describe the motion of the object on which position the object is placed or also describe its exact location or coordinate at any particular time.

Code:

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

  • print(tur.pos()) is used to print the default position of the turtle.
  • tur.forward(100) is used to move the turtle in the forward direction and get the position of this direction.
  • tur.right(90) is used to move the turtle in the right direction and get the position and get the position of this direction.
from turtle import *
import turtle as tur

print(tur.pos())
  
tur.forward(100)  

print(tur.pos())
  
tur.right(90)
tur.forward(100)
print(tur.pos())

Output:

After running the above code, we get the following output in which we can see the turtle is moving in the forward direction after that it takes a turn right by 90 degrees.

Python turtle get position
Python turtle get the position

As the turtle moves on the screen, there position is printing on the command prompt as we see the below output.

Python turtle get position Output
Python turtle get position Output

Also, read: Python Turtle Triangle + Examples

Python turtle get X and Y position

In this section, we will learn about how to get the and y position of turtle in python turtle.

Before moving forward we should have a piece of knowledge about the X and Y positions. X position is given a number of pixels along with horizontal axis and Y position is given a number of pixels along with vertical axis. Here we set the x and y position of the turtle and the turtle is shown at the exact location on the screen.

Code:

In the following output, we will import the turtle module from turtle import *, import turtle from which we get the x and y position of the turtle, and also import time to get an exact position at a particular time. The turtle() method is used to make objects.

  • tur_txt.goto(tur.position()[0], tur.position()[1]) is used to get x and y position of the turtle.
  • tur_txt.write(“Python Guides”) is used to write the text on that position where is located.
  • time.sleep(2) is used to delay execution for the given number of seconds.
from turtle import *
import turtle
import time

tur = turtle.Turtle()
tur_txt = turtle.Turtle()
tur_txt.goto(tur.position()[0], tur.position()[1])
tur_txt.write("Python Guides")
time.sleep(2)
turtle.done()

Output:

After running the above code we get the following output in which we can see the text is placed on the screen from which we can get the x and y position.

Python turtle get x and y position
Python turtle get x and y position Output

Read: Python Turtle Square – Helpful Guide

Python turtle get mouse position

In this section, we will learn about how to get a mouse position in a python turtle.

As we know with the help of a mouse we click on the item placed on the screen of a laptop or computer. Here we want to capture the position of the mouse. As the mouse moves the position of the mouse is seen on the command prompt.

Code:

In the following code, we will import the turtle module from turtle import *, import turtle as tur from which we get the mouse position. The turtle() method is used to make objects.

  • a, b = event.x, event.y is used to get the position of the mouse anywhere the mouse moves on the screen the position of the mouse capture.
  • print(‘{}, {}’.format(a, b)) is used to print the position of mouse.
from turtle import *
import turtle as tur
def position(event):
    a, b = event.x, event.y
    print('{}, {}'.format(a, b))

ws = tur.getcanvas()
ws.bind('<Motion>', position)
tur.done()

Output:

After running the above code we get the following output in which we can see on clicking on the mouse the cursor start moving on the screen and we get the position of this moving cursor on the command prompt.

Python turtle get mouse position
Python turtle get mouse position

Here we can see in the below picture we get the positions of the moving cursor.

python turtle get mouse position
python turtle get mouse position Output

You may also like to read the following articles on Python Turtle.

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

  • Python turtle get position
  • Python turtle get X and Y position
  • Python turtle get mouse position