Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Create Countdown Timer using Python-Tkinter
Next article icon

As we know Tkinter is used to create a variety of GUI (Graphical User Interface) applications. In this article we will learn how to create a Digital clock usingTkinter

Prerequisites: 

  • Python functions 
  • Tkinter basics (Label Widget) 
  • Time module 


Using Label widget from Tkinter and time module: 

In the following application, we are going to use Label widgetand also going to usetime module which we will use to retrieve system's time.

Below is the implementation: 
 

Python
# importing whole modulefromtkinterimport*fromtkinter.ttkimport*# importing strftime function to# retrieve system's timefromtimeimportstrftime# creating tkinter windowroot=Tk()root.title('Clock')# This function is used to# display time on the labeldeftime():string=strftime('%H:%M:%S %p')lbl.config(text=string)lbl.after(1000,time)# Styling the label widget so that clock# will look more attractivelbl=Label(root,font=('calibri',40,'bold'),background='purple',foreground='white')# Placing clock at the centre# of the tkinter windowlbl.pack(anchor='center')time()mainloop()

Output: 
 

Code Explanation:

  1. The code starts by importing the necessary modules.
  2. The first module is the tkinter library, which provides basic functionality for creating graphical user interfaces (GUIs).
  3. Next, the strftime function is imported to retrieve system time.
  4. Next, a window is created and given a title of "Clock."
  5. A function called time() is then created to display the current time on the label widget.
  6. This function uses the strftime() function to format the time string according to system conventions.
  7. The last part of this code sets up styling for the label widget so that it will look nicer.
  8. Finally, an instance of Label is created and placed at the center of the window.
  9. The time() function is executed, and your output should look like this: Clock: Tue Dec 12 08:00:00 2016
  10. The code creates a window and assigns it the title "Clock".
  11. The time() function is then called to display the current time on the label widget.
  12. The lbl.config() function is used to set the text of the label widget.
  13. The after() function is used to delay displaying the time for 1000 milliseconds.
  14. Finally, the style of the label widget is modified with lbl.pack().

Create a digital clock using Tkinter
Video Thumbnail

Create a digital clock using Tkinter

Video Thumbnail

How to Create a Digital Clock Using Python

Improve
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