Monday, April 21, 2025

Python Turtle - Background Color, Title, Shapesize, Pensize

Run this simple script, and then we will explain:

import turtle

turtle.bgcolor("lightgreen")
turtle.title("Custom Turtle Title")

t = turtle.Turtle()
t.shapesize(20, 20, 20)

To set background color we will use bgcolor() method. We are not targeting specific turtle, this method is used for turtle window:

turtle.bgcolor("lightgreen")

Same thing with turtle window title: 

turtle.title("Custom Turtle Title")

We must pass 3 attributes to method shapesize() to play around with different arrow dimensions. Bu default turtle shape is arrow: 

t.shapesize(20, 20, 20)

Turtle Pensize

Method pensize() is used to change size, well, pen size. Every time when we change it, we advance forward for 50 pixels:

import turtle

turtle.bgcolor("lightgreen")
turtle.title("Custom Turtle Title")

t = turtle.Turtle()

t.pensize(5)
t.fd(50)

t.pensize(10)
t.fd(50)

t.pensize(15)
t.fd(50)

t.pensize(20)
t.fd(50)

Using for loop it's easy to create interesting shapes:

import turtle

turtle.bgcolor("lightgreen")
turtle.title("Custom Turtle Title")

t = turtle.Turtle()

for x in range(4):
    t.left(90)
    t.pensize(5)
    t.fd(50)

    t.pensize(15)
    t.fd(50)

    t.pensize(20)
    t.fd(50)

There's YouTube tutorial on turtle background color, title, shape and pensize.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...