How to control turtle graphics window?: introduction to turtle GUI



Introduction

Turtle graphics is a pre-installed Python GUI library that enables users to draw pictures, shapes and create colorful animations and games. Therefore, it’s a popular way of introducing programming to kids and beginners which makes coding fun and interactive. The output is seen in the form of graphics which even makes the coding self-explanatory.

turtle module can be used to develop simple games such as flappy bird, snake game, and a lot more. However, to develop more complex and interactive games, there is another module in python called pygame which is particularly used to develop games.

Importing turtle module

To make use of turtle methods and functionalities, first, we need to import the turtle module (standard python package).  There are two ways where we can import the turtle module:

1. Directly using the import statement at the beginning of the statement.

>>>import turtle

2. Using the ‘from’ clause at the beginning of the statement.

>>>from turtle import *

External link: If you want to know more about importing modules click here



Methods to control a turtle window

We will look into the six methods used to control the turtle window with an example:

1. bgpic(picname=None)

This function is used to set a background image or return the name of the current background image. It requires only one argument “image URL”. This argument can be used in different ways as follows:

● if picname is a filename, sets the corresponding image as background.

● if picname is “nopic”, deletes background image if present.

● if picname is None, returns the filename of the current background image.

Syntax:

bgpic(picname=None)

Example:

from turtle import *
bgpic("turtle.png") 

Output:

turtle graphics

Note: Make sure that the file “turtle.png” is saved in the same folder where the source code is located.

2. screensize(canvwidth=None, canvheight=None, bg=None)

This method is used to resize the output window of the turtle. If no arguments is passed, this function returns the current (canvas width, canvas height) or the default value (width= 400, height=300) in pixel.

Note: Python version above 3 has no effect of screensize() method.

Syntax:

screensize(canvwidth=None, canvheight=None, bg=None)

Example:

from turtle import *
screensize(canvheight=1000, canvwidth=1000, bg="red")

Where:

  • The canvwidth is the new width of the canvas in pixel.
  • The canvheight is the new height of the canvas in pixels.
  • The bg is the new background color.

Output:

turtle bg
3. title(title string)

This function is used to set the title in the title bar of the turtle graphics window. By default, the title of the turtle graphics window is “Python Turtle Graphics”.

Syntax:

title(title string)

Example:

from turtle import *
title("We are learning turtle")

Output:

turtle graphics title


4. turtlesize(stretch_wid=None, stretch_len=None, outline=None)

This function is used to return or set the size of the pen or turtle.

Parameters:

  • stretch _wid – Width of the turtle
  • Stretch_len – Length of the turtle
  • Outline – width of the turtle outline.

Syntax:

turtlesize(stretch_wid=None, stretch_len=None,  outline=None)

Example:

from turtle import *
shape("turtle")    # To show a turtle on the screen.
turtlesize(5, 3, 2)

Output:

turtle size
Width of the turtle: 5pixel, height: 3pixel, and outline thickness of 2pixel.
5. hideturtle() and showturtle()

The hideturtle() and showturtle() methods can be used to hide and show turtle drawing icons. It can be particularly helpful during or after the drawing to improve the visibility of the turtle drawing. The default state of the turtle is showturtle() but you can use hideturtle() to hide the turtle. Drawings will still proceed but you won’t see the turtle icon that makes the drawing.

Syntax:

hideturtle()
showturtle()

Example:

from turtle import *
shape("turtle")
hideturtle()
forward(100)
showturtle()

Output:

turtle hide and show
The turtle will be hidden when the program starts executing. However, it appears back when she finishes drawing a forward line of 100px.
6. clear()

This function is used to delete the turtle’s drawings from the screen.

Syntax:

clear()

Example:

from turtle import *
shape("turtle")
forward(100)
clear()

Output:

turtle clear method
The turtle draws a forward line of length 100px, but it clears it all as soon as the turtle finishes drawing.