Posted on Leave a comment

What is turtle.Turtle() in python?

In Python, turtle.Turtle() is a function that creates and returns a new turtle object that can be used to draw graphics on a canvas.

The turtle graphics library in Python provides a simple way to create graphics and animations by allowing you to control a virtual turtle that moves on a canvas. The turtle can be moved around the canvas using commands such as forward(), backward(), left(), right(), etc.

To use the turtle graphics library, you first need to import the turtle module. Then, you can create a new turtle object using the turtle.Turtle() function. For example, the following code creates a new turtle and moves it forward by 100 units:

import turtle

t = turtle.Turtle()
t.forward(100)

You can also customize the appearance and behavior of the turtle object by setting various attributes such as its color, size, shape, and speed. For example, the following code creates a red turtle with a size of 5 and a speed of 2:

import turtle

t = turtle.Turtle()
t.color("red")
t.pensize(5)
t.speed(2)
t.forward(100)

Overall, the turtle.Turtle() function is the starting point for creating and controlling turtle objects in the turtle graphics library.

Posted on Leave a comment

How to make an analog clock using Python Turtle Graphics – 3

Python Code

import turtle
from datetime import datetime

screen1 = turtle.Screen()
screen1.setup(500,500,0,0)
screen1.screensize(480,480, bg="#c0c0c0")
screen1.tracer(0)

don = turtle.Turtle()
#don.speed("fastest")
don.width(1)
don.hideturtle()

def draw_square(startx, starty, length):
    don.penup()
    don.home()
    don.goto(startx, starty)
    don.pendown()
    for side in range(4):
        don.forward(length)
        don.right(90)
    don.penup()

def draw_hand(length,rot):
    don.penup()
    don.home()
    don.pendown();
    don.right(1*rot+90*3)
    don.forward(length)
    don.penup()
    don.home()
    
def print_time(hou,minu,sec):
    don.penup()
    don.goto(-180,-190);
    don.pendown()
    data = str(hou)+" : "+str(minu)+" : "+str(sec)
    don.color("white")
    don.write(data, move = False,align='left', font=('Arial', 16, 'normal'));
    don.penup()

def draw_watchface():
    don.penup()
    for x in range(0,360,30):
        don.home()
        don.color("black")
        don.right(x)
        don.forward(150)
        don.pendown();
        don.forward(20)
        don.penup()
        
    for x in range(0,360,6):
        don.home()
        don.color("black")
        don.width(1)
        don.right(x)
        don.forward(150)
        don.pendown();
        don.forward(10)
        don.penup()
    don.penup()
    
def draw_frame(hou,minu,sec):
    don.goto(-200,200);
    don.pendown()
    draw_square(-200,200,400)
    draw_watchface()
    don.color("blue")
    draw_hand(140,sec*6)  #seconds
    don.color("red");
    don.width(3)
    draw_hand(140,minu*6)  #minutes
    don.color("green");
    don.width(10)
    draw_hand(140,hou*6*5)  #hours
    don.width(2)
    don.penup()
    don.goto(-220,-230);
    don.pendown()
    don.write("exasub.com", move = False,align='left', font=('Arial', 16, 'normal'));
    don.penup()
    print_time(hou,minu,sec)
    #print(str(hou)+" : "+str(minu)+" : "+str(sec))

def draw_time():
    while(1):
        t = datetime.today()
        sekunde = t.second 
        minuten = t.minute 
        houren = t.hour
        
        don.clear()
        draw_frame(houren,minuten,sekunde);
        screen1.update()

    
if __name__ == "__main__":
    draw_time()
    turtle.done()
Posted on Leave a comment

How to make simple graphics in Turtle Graphics – 2

Python Code

from turtle import * 

def frame():
    #setup (width=200, height=200, startx=0, starty=0)
    setup(500,500,0,0);
    screensize(canvwidth = 480, canvheight = 480, bg = "cyan")

def box1(size, angle):
    penup()
    goto(-(size/2),(size/2))
    pendown()
    forward(size)
    right(angle)
    forward(size)
    right(angle)
    forward(size)
    right(angle)
    forward(size)
    penup()

def text():
    goto(-230,-230)
    pendown()
    #turtle.write(arg, move=False, align='left', font=('Arial', 8, 'normal'))
    write("exasub.com", False, align="left", font = ('Arial', 16, 'normal'))
    
if __name__ == "__main__":
    frame();
    box1(400,90);
    text();
    mainloop()

Python Code

from turtle import * 

def frame():
    #setup (width=200, height=200, startx=0, starty=0)
    setup(500,500,0,0);
    screensize(canvwidth = 480, canvheight = 480, bg = "cyan")

def box1(size, angle):
    penup()
    goto(-(size/2),(size/2))
    pendown()
    forward(size)
    right(angle)
    forward(size)
    right(angle)
    forward(size)
    right(angle)
    forward(size)
    penup()

def text():
    goto(-230,-230)
    pendown()
    #turtle.write(arg, move=False, align='left', font=('Arial', 8, 'normal'))
    write("exasub.com", False, align="left", font = ('Arial', 16, 'normal'))
    penup()

def drawCircle():
    # Draw a circle in Anticlockwise direction
    goto(0,0)
    pendown()
    color("red")
    begin_fill()    # Start Filling the semi-circle
    circle(100,-180,40); # circle(radius, angle, steps)
    end_fill()      # Stop Filling the semi-circle
    penup()
    #Draw a circle in Clockwise Direction
    goto(0,0)
    pendown()
    color("green")
    begin_fill()
    circle(100,360,40)
    end_fill()
    penup()

def drawTriangle():
    home()
    pendown()
    color("grey")
    begin_fill()
    circle(50, 360, 3)
    end_fill()
    penup()

    
if __name__ == "__main__":
    frame();
    box1(400,90);
    text();
    drawCircle();
    drawTriangle()
    #drawCircle();
    mainloop()

Posted on Leave a comment

Python Turtle Graphics Introduction – 1

Turtle is a simple, easy and fun way to learn graphical programming.

It contains very simple commands. By combining together these and similar commands, intricate shapes and pictures can easily be drawn.

You can create simple drawings using the turtle module.

You can also create simple animations.

If you want to learn about the nitty-gritty of Turtle you can read their documentation.
https://docs.python.org/3/library/turtle.html#introduction

You do not have to install anything apart from the python programming language.

You can download the python programming language from
https://www.python.org/downloads/