Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Draw a Tic Tac Toe Board using Python-Turtle
Next article icon

Turtle is a special feature of Python. Using Turtle, we can easily draw on a drawing board. First, we import the turtle module. Then create a window, next we create a turtle object and using the turtle methods we can draw in the drawing board.

Prerequisites:Turtle Programming in Python

Installation: To install this module type the below command in the terminal. 
 

pip install turtle


Note: To create a clock we will use the 'time' and 'DateTime' modules of Python also, To install time use the following command:

Below is the implementation. 

Python3
importtimeimportdatetimeasdtimportturtle# create a turtle to display timet=turtle.Turtle()# create a turtle to create rectangle boxt1=turtle.Turtle()# create screens=turtle.Screen()# set background color of the screens.bgcolor("green")# obtain current hour, minute and second# from the systemsec=dt.datetime.now().secondmin=dt.datetime.now().minutehr=dt.datetime.now().hourt1.pensize(3)t1.color('black')t1.penup()# set the position of turtlet1.goto(-20,0)t1.pendown()# create rectangular boxforiinrange(2):t1.forward(200)t1.left(90)t1.forward(70)t1.left(90)# hide the turtlet1.hideturtle()whileTrue:t.hideturtle()t.clear()# display the timet.write(str(hr).zfill(2)+":"+str(min).zfill(2)+":"+str(sec).zfill(2),font=("Arial Narrow",35,"bold"))time.sleep(1)sec+=1ifsec==60:sec=0min+=1ifmin==60:min=0hr+=1ifhr==13:hr=1

Output: 

Code Explanation:

  1. The code starts by creating two turtles.
  2. The first turtle, t1, will be used to create a rectangular box on the screen.
  3. The second turtle, t2, will be used to display the time.
  4. The code then sets up some variables.
  5. The hour variable stores the current hour as a number (e.g., 13).
  6. The minute variable stores the current minute as a number (e.g., 59).
  7. And the second variable stores the current second as a number (e.g., 2).
  8. Next, the code obtains information about the system time from datetime object dt.datetime.now().second .
  9. This object contains information about the date and time that is currently being displayed on your computer or device.
  10. The next section of code sets up some variables for drawing onscreen using turtle graphics objects.
  11. These variables store information about how wide and high each line should be drawn in pixels (i.e., they are called pensize() variables), and they also specify which color to use for each line (i.e., they are called color() variables).
  12. Finally, these variables tell the turtle to start drawing at position (-20, 0) in the upper-left corner of the screen and to stop drawing. The code first creates two turtles, one to display time and another to create a rectangular box.
  13. Next, the code sets up the screen and obtains the current hour, minute, and second from the system.
  14. The code then sets the position of the turtle for each line in the for a loop.
  15. The first line moves the turtle 200 pixels to the right, 90 pixels down and 70 pixels forward.
  16. The next line moves the turtle again but this time it left 90 pixels to the right and moved it 70 pixels down.
  17. The last two lines move the turtle forward by 200 pixels again and left it at its original position.
  18. Next, the code hides both turtles and waits for a second.
  19. Once a second has passed, it displays time by writing.

Improve
Article Tags :
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp