Pythondex
Turtle Python Programs

Draw Traffic Light In Python Turtle With Code

Last updated June 21, 2023 by Jarvis Silva

In this tutorial I will show you how to draw traffic light using python, to create this program we will use the turtle module in python, turtle is a GUI python library which can be used to draw anything from characters, cartoons, shapes and other objects.

Python Code To Draw Traffic Light


import turtle

def draw(tur,screen,color):
    tur.speed(10)
    screen.clear()
    tur.penup()
    tur.setpos(0,-70)
    tur.pensize(12)
    if color == 1:
        tur.color("black","orange")
    else:
        tur.color("black","gray")
    tur.pendown()
    tur.begin_fill()
    tur.circle(100)
    tur.end_fill()
    tur.penup()
    tur.setpos(0,150)
    if color == 0:
        tur.color("black","red")
    else:
        tur.color("black","gray")
    tur.pendown()
    tur.begin_fill()
    tur.circle(100)
    tur.end_fill()
    tur.penup()
    tur.setpos(0,-290)
    if color == 2:
        tur.color("black","green")
    else:
        tur.color("black","gray")
    tur.pendown()
    tur.begin_fill()
    tur.circle(100)
    tur.end_fill()
    tur.penup()
    tur.hideturtle()
    
    
screen = turtle.Screen()
tur = turtle.Turtle()
color = int(input("Enter color number\n"))
draw(tur,screen,color)
turtle.done()

Above is the code for drawing the traffic light, Now to run this program you need to have python installed on your computer, If you don’t have then follow this guide: Install and setup python on your computer.

So when you run the program and it will ask you to enter a number according to it, it will draw the traffic light, enter 0 for red light, enter 1 for orange light and enter 2 for green light and below is the finished drawing of a red traffic light.

Traffic Light Drawing In Python Turtle

As you can see, we successfully drew a traffic light using python turtle, I hope you found this tutorial helpful and useful, do share this tutorial with your friends who might be interested in this program.

Here are some more python drawing tutorials for you:

I hope you found what you were looking for from this tutorial, and if you want more python guides and tutorials like this, do join our Telegram channel for future updates.

Thanks for reading, have a nice day 🙂