How to control turtle graphics window?: introduction to turtle GUI
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.
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:
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.
from turtle import * bgpic("turtle.png")
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.
screensize(canvwidth=None, canvheight=None, bg=None)
from turtle import * screensize(canvheight=1000, canvwidth=1000, bg="red")
- 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.
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”.
from turtle import * title("We are learning turtle")
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.
- stretch _wid – Width of the turtle
- Stretch_len – Length of the turtle
- Outline – width of the turtle outline.
turtlesize(stretch_wid=None, stretch_len=None, outline=None)
from turtle import * shape("turtle") # To show a turtle on the screen. turtlesize(5, 3, 2)
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.
from turtle import * shape("turtle") hideturtle() forward(100) showturtle()
This function is used to delete the turtle’s drawings from the screen.
from turtle import * shape("turtle") forward(100) clear()