Create a Digital Clock using Tkinter module in python | Nepohits

create digital clock using python



In this post, Today we are going to create a Digital Clock using the Tkinter module in python. For Building this clock up, Tkinter and Time module are required.

You may also like to read     Unlock Your pc without using passwords/resetting or formating the system


Let's start to build the Digital Clock. Following are the steps to build the Digital Clock using python.


1. Firstly, Import Tkinter and Time libraries:



from tkinter import Label, Tk

import time


2. Create a variable that will store our Tkinter window:


clock = Tk()
clock.title ("Nepohits Digital clock")


3. Define Size of the Window:


clock.geometry ("2000x2000")
clock.resizable (1,1)
text_font = ("Times", 200 , 'bold')
background = "Red"
foreground = "Black"
clock.configure (bg="#F08080")
border_width = 38


4. Create a variable for Digital Clock:


label = Label (clock, font= text_font, bg = background, fg = foreground, bd = border_width)
label.place  (x=180, y=200)


5. Create a variable for Name, Hours, Minutes and seconds:


nota = Label (clock, text= '  Hours            Minutes        Seconds  ', font= "times 50 bold")
nota.place(x= 180, y= 500)

Name = Label (clock, text=('Designed By Nawaj'), font=  "times 20 bold", bg= "Yellow")
Name.place (x=180, y=200)


5. Create a function Digital_Clock and variable time_live:


def digital_clock():
  time_live = time.strftime ("%H:%M:%S")
  label.config(text = time_live)
  label.after (200, digital_clock)
 
digital_clock()
clock.mainloop()


Let's implement the final code:


# IMPORT ALL THE LIBRARIES FIRST
from tkinter import Label, Tk
#import time library to obtain current time
import time
#Create a variable that will store our tkinter window
clock = Tk()
clock.title ("Nepohits Digital clock")
#define size of the window
clock.geometry ("2000x2000")
clock.resizable (1,1)
text_font = ("Times", 200 , 'bold')
background = "Red"
foreground = "Black"
clock.configure (bg="#F08080")
border_width = 38
#create a variable for digital clock
label = Label (clock, font= text_font, bg = background, fg = foreground, bd = border_width)
label.place  (x=180, y=200)
# create a variable for Hours, minutes and seconds
nota = Label (clock, text= '  Hours            Minutes        Seconds  ', font= "times 50 bold")
nota.place(x= 180, y= 500)

Name = Label (clock, text=('Designed By Nawaj'), font=  "times 20 bold", bg= "Yellow")
Name.place (x=180, y=200)

# create a function Digital_clock and variable time_live
def digital_clock():
  time_live = time.strftime ("%H:%M:%S")
  label.config(text = time_live)
  label.after (200, digital_clock)
 
digital_clock()
clock.mainloop()


RESULT:


create a digital clock using python




If You have any queries or suggestions, please feel free to ask! Thank You. 

No comments:

If you have any doubts please let's me know

Powered by Blogger.