Python Turtle Mouse + Multiple Examples

In this Python turtle tutorial, we will learn How to use turtle mouse in Python Turtle and we will also cover different examples related to Turtle Mouse. Also, we will cover these topics.

  • Python turtle mouse
  • Python turtle mouse events
  • Python turtle mouse position
  • Python turtle follows mouse
  • Python turtle mickey mouse
  • Python turtle draw with mouse
  • Python turtle wait for mouse click

Python turtle mouse

In this section, we will learn about how we can use turtle mouse in python turtle.

Turtle is used to draw the shape we can move the turtle with the help of the mouse. The turtle is starting working when the mouse gives the command.

Code:

In the following code, we will import turtle libraries from turtle import *, import turtle. The turtle() is used to make objects.

  • turtle.right(90) is used to move the turtle in the right direction.
  • turtle.forward(150) is used to move the turtle in the forward direction.
  • turtle.speed(6) is used to give the normal speed to the turtle.
  • turtle.onclick(func) is used to allow the user to click the mouse for some action.
from turtle import *
import turtle
  
def func(i,j):
     
    turtle.right(90)
    turtle.forward(150)
  
turtle.speed(6)
turtle.forward(100) 
turtle.onclick(func)
turtle.done()

Output:

After running the above code we get the following output in which we can see on clicking on the arrow with the help of the mouse the turtle move in the forward direction and the square is drawn.

Python turtle mouse
Python turtle mouse Output

Also, check: Python Turtle Race

Python turtle mouse events

In this section, we will learn about turtle mouse events in python turtle.

Before moving forward we should have a piece of knowledge about the event.

An event is something that occurs or takes place at a particular event of time. Here on clicking on the screen with the help of a mouse an event occurs the turtle change their position from one place to other.

Code:

In the following code, we will import turtle libraries from turtle import *, import turtle.

  • ws = turtle.Screen() is used to make a screen in which we draw shapes.
  • turtle.goto(i, j) is used to move the turtle to an absolute position.
  • ws.onclick(func) is used after clicking the mouse the cursor is moved and draw the shape.
from turtle import *

import turtle
  

ws = turtle.Screen()
  

def func(i, j):
  turtle.goto(i, j)
  turtle.write(str(i)+","+str(j))
  

ws.onclick(func)
ws.mainloop()

Output:

READ:  Pandas groupby without aggregation function in Python [4 Examples]

After running the above code we get the following output in which we can see after clicking on the mouse some action is done and an event is generated.

Python turtle mouse event
Python turtle mouse event Output

Read: Python turtle onclick

Python turtle mouse position

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

As we know with the help of the mouse we perform most of the functions. Here also on click on the mouse, the cursor which is placed on the screen starts moving. Anywhere the cursor move we get the position and the position is shown on the command prompt.

Code:

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

  • i,j = event.x, event.y is used to define the position of turtle.
  • turtle.getcanvas() is used for making the canvas.
from turtle import *
import turtle
def position(event):
    i,j = event.x, event.y
    print('{}, {}'.format(i, j))

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

Output:

After running the above code, we get the following output in which we can see the cursor is placed on the screen after clicking on the mouse the cursor starts moving and we get the position of the cursor on the command prompt.

Python turtle mouse position
Python turtle mouse position

In the below picture we can see the cursor position after moving on the screen.

Python turtle mouse position Output
Python turtle mouse position Output

Read: Python Turtle Tracer

Python turtle follows mouse

In this section, we will learn about how to follow mouse in python turtle.

As we know after clicking on the mouse the cursor can start moving on the screen. The cursor follows the mouse command and draws the shape on the screen.

Code:

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

  • tur.goto(i, j) is used to move the turtle to an absolute position.
  • tur.ondrag(drag) is used to drag the cursor and follow the mouse.
  • ws = Screen() is used to make the screen.
  • tur.speed(‘fastest’) is used to give the fastest speed to the turtle.
from turtle import *
from turtle import Turtle, Screen

def drag(i, j):
    tur.ondrag(None)
    tur.setheading(tur.towards(i, j))
    tur.goto(i, j)
    tur.ondrag(drag)

ws = Screen()

tur = Turtle('turtle')
tur.speed('fastest')

tur.ondrag(drag)

ws.mainloop()

Output:

READ:  Matplotlib multiple plots

After running the above code, we get the following output in which we can see the turtle follow the mouse command and draw the shape.

Python turtle follow mouse
Python turtle follows mouse Output

Read: Python Turtle Triangle

Python turtle mickey mouse

In this section, we will learn about how to draw mickey mouse in python turtle.

Here we can draw the shape of the mickey mouse with the help of the turtle. As we know the turtle() method is used to make different shapes.

Code:

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

  • turtle.Screen() is used to make a screen in which we can draw different shapes.
  • background.bgcolor(“white”) is used to give background color to the screen.
  • tur.shape (“turtle”) is used to give the turtle shape to the cursor.
  • tur.speed(100) is used to give speed to the turtle.
  • turt.penup() is used to stop the drawing.
  • turt.fillcolor(clr) is used to fill the color inside the shape.
  • turt.goto(i,j) is used to move the turtle to an absolute position.
  • turt.begin_fill() is used to start filling color.
  • turt.circle(siz) is used to draw a circle shape with the help of a turtle.
  • turt.end_fill() is used stop filling color.
from turtle import *
import turtle

background = turtle.Screen()
background.bgcolor("white")

tur = turtle.Turtle()
tur.shape ("turtle")
tur.speed(100)

def draw_mickymouse(turt, clr, siz, i,j):
  turt.penup()
  turt.color(clr)
  turt.fillcolor(clr)
  turt.goto(i,j)
  turt.pendown()
  turt.begin_fill()
  turt.circle(siz)
  turt.end_fill()
  
draw_mickymouse(tur, "black", 80, 0, -60)
draw_mickymouse(tur, "black", 45, 60, 60)
draw_mickymouse(tur, "black", 45, -60, 60)

turtle.done()

Output:

After running the above code, we get the following output in which we can see the shape mickey mouse is drawn with the help of turtle.

Python turtle mickey mouse
Python turtle mickey mouse

Read: Python Turtle Size

Python turtle draw with mouse

In this section, we will learn about how to draw different shapes with the mouse in a python turtle.

As we know turtle is used for making different shapes. Here we use the mouse which helps the turtle for drawing the shape and the turtle follows the mouse instruction.

On clicking on the mouse they draw the turtle for drawing the shape and the turtle also follows the mouse command and the shape is drawn.

Code:

from turtle import *
import turtle
from turtle import Screen, Turtle

ws = Screen()
tur = turtle.Turtle()
tur.speed(-1)

def dragging(x, y): 
    tur.ondrag(None)
    tur.setheading(tur.towards(x, y))
    tur.goto(x, y)
    tur.ondrag(dragging)

def clickRight():
    tur.clear()

def main():  
    turtle.listen()
    
    tur.ondrag(dragging)  dragging
    turtle.onscreenclick(clickRight, 3)

    ws.mainloop()  

main()

Output:

READ:  Python Screen Capture

After running the above code, we get the following output in which we can see the shape of the cap is drawn with the help of the mouse.

Python turtle draw with mouse
Python turtle draw with mouse

Read: Python Turtle Font

Python turtle wait for mouse click

In this section, we will learn about turtle waiting for a mouse click in python turtle.

We draw a different shape on the screen with the help of a turtle. Here turtle is waiting for a mouse click. With just a single click of the mouse on the screen, the turtle starts drawing.

Code:

In the following code, we will import turtle libraries from turtle import *, import turtle as tur, and also import time as t for managing the time. The turtle() method is used to make objects.

  • tur.onscreenclick(on_click) function is used for sending the turtle on the current coordinate after a single click on the screen.
  • t.sleep(.1) is used to delay the execution of the program.
  • tur.forward(100) is used to move the turtle in the forward direction.
  • tur.left(90) is used to move the turtle in the left direction.
from turtle import *
import turtle as tur
import time as t


click = False

def on_click(i, j):
    global click

    click = True

tur.onscreenclick(on_click)

def waitforclick():
    global click

    tur.update()
    click = False

    while not click:
        tur.update()
        t.sleep(.1)

    click = False


tur.update()

for _ in range(4):
    waitforclick()
    tur.forward(100)
    tur.left(90)

tur.exitonclick() 

Output:

After running the above code, we get the following output in which we can see that the turtle is waiting for mouse click after a single click of the mouse the turtle starts drawing a shape on the screen.

Python turtle wait for mouse click
Python turtle wait for mouse click Output

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

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

  • Python turtle mouse
  • Python turtle mouse events
  • Python turtle mouse position
  • Python turtle follows mouse
  • Python turtle mickey mouse
  • Python turtle draw with mouse
  • Python turtle wait for mouse click