In this Python Turtle tutorial, we will learn How to create a window in Python Turtle and we will also cover different examples related to Turtle Window. And, we will cover these topics.
- Python turtle window
- Python turtle window size
- Python turtle window disappear
- Python turtle window position
- Python turtle window setup
- Python turtle window title
- Python turtle window background color
Python turtle window
In this section, we will learn about how to create a turtle window in a python turtle.
Python turtle window is a place where a turtle can draw different shapes and pictures. Here TurtleScreen class defines the window. We can also resize the size of the window by applying a simple method. We can run different turtle commands and also get the running output on the window.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle. The turtle() method is used to make objects.
- ws.bgcolor(“cyan”) is used to give the background color to the window.
from turtle import *
import turtle
ws = turtle.Screen()
ws.bgcolor("cyan")
turtle.done()
Output:
After running the above code, we get the following output in which we can see a window with a beautiful background color.
Also, check: Python Turtle Hide
Python turtle window size
In this section, we will learn about how to get default the size of a window in a python turtle.
As we know all the images, pictures and shapes are shown on the window. We can also get the default size of the window with the help of the ScreenSize() function.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle. The turtle() method is used to make objects. And print(turtle.screensize()) is used to print the default size of the window on the command prompt.
from turtle import *
import turtle
print(turtle.screensize())
Output:
After running the above code, we get the following output in which we can see the default size of the window is shown in the command prompt.
Read: Python Turtle Background
Python turtle window disappear
In this section, we will learn about how the window disappears in python turtle.
Before moving forward we should have a piece of knowledge about disappear. Disappear means the thing which is impossible to find or go missing somewhere. Here we can see our window can disappear in a fraction of a second when our program is finished.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle as tur. The turtle() method is used to make objects.
- 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
tur.forward(100)
tur.left(90)
tur.forward(100)
Output:
After running the above code we get the following output in which we can see that in a few seconds our window disappears.
Read: Python Turtle Cheat Sheet
Python turtle window position
In this section, we will learn about the turtle widow position in python turtle.
The position is defined as the place where the object is located or wants to be placed in that location. Here we can see the window is moved from one position to another position.
Code:
In the following code, we will import the turtle library from turtle import Turtle. The turtle() method is used to make objects.
- ws.setup(width=0.333, height=0.333, startx=offset, starty=offset) is used give the size to the screen.
- turtle.dot(offset) is used to create the dot inside the window.
- winpos() is used to get the position of the window.
from turtle import Turtle, Screen
def winpos():
global offset
ws.setup(width=0.333, height=0.333, startx=offset, starty=offset)
turtle.dot(offset)
offset += 10
if offset < 300:
ws.ontimer(winpos, 100)
ws = Screen()
turtle = Turtle()
offset = 30
winpos()
ws.exitonclick()
Output:
After running the above code, we get the following output in which we can see that the window move from starting position to the ending position.
Read: Python Turtle Oval
Python turtle window setup
In this section, we will learn about how to set up the window in python turtle.
In python turtle we use Screen.setup() function to set the size and position of the window. We can give a different size to the window with the help of the setup() function.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle. The turtle() method is used to make objects.
- ws.setup(500,300) is used to set up the size of the window by giving height and width inside the argument.
- ws.bgcolor(“cyan”) is used to give the background color to the window.
from turtle import *
import turtle
ws = turtle.Screen()
ws.setup(500,300)
ws.bgcolor("cyan")
turtle.done()
Output:
After running the above code, we get the following output in which we can see a window is created with the given size.
Read: Python Turtle Star
Python turtle window title
In this section, we will learn about how to display the title on the screen in python turtle.
Window title specifies the name of the task which we are currently working on the window. We can give any title to the window of our choice which suits the task.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle. The turtle() method is used to make objects.
- ws.setup(500,400) is used to give the size to the window.
- turtle.title(“Python Guides”) is used to give a title to the window.
from turtle import *
import turtle
ws = turtle.Screen()
ws.setup(500,400)
turtle.title("Python Guides")
turtle.done()
Output:
After running the above code, we get the following output in which we can see a window on this window at the top left corner a title is placed which tells us about the task or company.
Read: How to Draw Flower in Python Turtle
Python turtle window background color
In this section, we will learn about how to give the background color to a window in a python turtle.
Color is used to make the object beautiful and attractive. Here we give the color to the background of the window from which our window looks attractive and users want to draw the shapes and pictures inside this window.
Code:
In the following code, we will import the turtle library from turtle import *, import turtle. The turtle() method is used to make objects.
- turtle.Screen().bgcolor(“light blue”) is used give the background color to the window.
- turtle.forward(100) is used to move the turtle in the forward direction.
- turtle.right(90) is used to move the turtle in the right direction.
from turtle import *
import turtle
turtle.Screen().bgcolor("light blue")
turtle.forward(100)
turtle.right(90)
turtle.done()
Output:
After running the above code, we get the following output in which we can see the beautiful background color of the window.
You may also like to read the following articles on Python turtle.
- Python Turtle Grid – Helpful Guide
- Python Turtle Pen + Examples
- Python turtle input with examples
- Python turtle draw letters
So, in this tutorial, we discussed Python Turtle Window and we have also covered different examples related to its implementation. Here is the list of examples that we have covered.
- Python turtle window
- Python turtle window size
- Python turtle window disappear
- Python turtle window position
- Python turtle window setup
- Python turtle window title
- Python turtle window background color
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.