The Tkinter module helps us to create some interesting widgets that we can show-off on our desktop. It’s an easy project for Python beginners (like us😉). So, let’s create a widget of Calendar & Clock using Python. Like this :

Modules required: ◾ Tkinter ◾Calendar ◾Time

So, first thing first importing all the modules.

import calendar
from tkinter import *
from time import strftime

You can easily install Tkinter module on your pc using PIP. Go to the command prompt and give the command pip install tkinter. The Calendar and Time modules are already there in Python as default.

Make sure you have PIP installed. If you don’t have PIP installed on your system you have to install PIP first, but if you are having Python 3.4 or higher, then it is already there.

Step 2 : (Creating the basic tk window)

my_root = Tk()        #gives a basic GUI window
my_root.config(bg="black")         #theme of the window
my_root.title("Calendar & Clock")          #title of the window

# Write the code here 

my_root.mainloop()

Step 3 : (Done with the Calendar)

yy = int(strftime("%Y"))          #Getting the Year and Month values using strftime() function
mm = int(strftime("%m"))         #and converting it into an integer type at the same time

my_lbl = Label(text = calendar.month(yy,mm), fg = "cyan", bg="Black")     #text label
my_lbl.config(font=("Script",22,"bold"))
my_lbl.pack() 

#calendar.month(Year,Month) gives the month calendar.
Output

Step 4 : (Creating the clock widget)

def now_time():
    time_is = strftime("%d/%m/%Y %H:%M:%S %p")    #extracting the current date and time using strftime() function
    label.config(text= time_is)
    label.after(1000, now_time)           #This is how the widget is created, it gets updated in every 1000 milliseconds i'e 1sec

label = Label(my_root, bg = "black", fg = "cyan")
label.config(font=("Script",18,"bold"))    
label.pack(pady=10,anchor="center")

now_time()         # run the function
Output

Finally, merge them together and it’s ready.

Final Result

Source Code :

import calendar
from tkinter import *
from time import strftime

my_root = Tk()        
my_root.config(bg="black")   
my_root.title("Calendar & Clock")    

yy = int(strftime("%Y"))
mm = int(strftime("%m"))

my_lbl = Label(text = calendar.month(yy,mm), fg = "cyan", bg="Black")
my_lbl.config(font=("Script",22,"bold"))
my_lbl.pack() 


def now_time():
    time_is = strftime("%d/%m/%Y %H:%M:%S %p")
    label.config(text= time_is)
    label.after(1000, now_time)

label = Label(my_root, bg = "black", fg = "cyan")
label.config(font=("Script",18,"bold"))
label.pack(pady=10,anchor="center")

now_time()
my_root.mainloop()

Leave a comment

Design a site like this with WordPress.com
Get started