How to Wish Happy Diwali with tkinter in Python Language

happy Diwali by python language

Introduction :

Celebrating festivals is an essential part of human culture. The technology allows us to celebrate these traditions in new and creative ways. Using the Python programming language and the tkinter library, you can make Happy Diwali wishes digitally. The creation of a Python program to wish somebody a happy Diwali can be a fun and interesting project. In this article, I shall explore you how to create an animation of graphics-based Happy Diwali wishing program using the tkinter library by Python language with source code.

What is Diwali :

The name “Diwali” is originating from the Sanskrit word “Deepavali” means a row of lights. So Diwali is known as Deepavali or the Festival of Lights. Diwali is one of the most celebrated festivals in India and various parts of the world in Hindu, Jain and Sikh communities. The festival generally falls between October and November, depending on the lunar calendar.

It is a celebration filled with joy, knowledge, light and togetherness. It symbolizes the victory of light over darkness, good over evil and knowledge over ignorance. People exchange greetings, sweets and gifts during this favourable occasion.

About the program :

After running the happy Diwali program by python, you see a graphics window open on the pc. In this graphics window, you see a house, a man and a rocket bomb on the screen. Here, you see some lighting and sound bomb blasting here and there on screen. The man is walking toward the rocket bomb. When the man reaches near the rocket bomb, he gives fire on the rocket using the right hand.

Now, the rocket bomb goes toward the sky. After some time, the rocket bomb is blasted and you can hear the blasting sound. At last, a happy Diwali photo is displayed on the screen and happy Diwali wishes song play on the background.

Explanation of the program :

Before you begin, you have to ensure that you have Python, tkinter, winsound and PIL installed on your system. At first, you have to import the tkinter library, which provides the tools needed to create graphical user interfaces. Then, import random library to get random numbers. Here, winsound and PIL libraries are import for produce sound and display image on the screen. After that, create the main window for our happy Diwali program using root = Tk(). The window’s title is set using title() method like root.title().

Next, a canvas is instantiated with width, height and background color using Canvas() class like w = Canvas(). The canvas class or widget will serve as our graphical display area where you can display various graphics with the help of functions or methods of Canvas class. The canvas is packed into the main window by w.pack(). Next, declare some variable which you have to use in the program.

Then, draw a house using create_polygon(), create_rectangle() and create_line() methods. Using create_rectangle() and create_oval, create the rocket bomb and its stand. After that, in the while loop displays text using create_text() method. After that, using create_line(), create_oval() and create_rectangle() create lighting of bomb and create the sound of blasting using winsound.Beep() in the screen. Using create_rectangle() and create_oval() create the man and the rocket bomb.

You have to download a bomb blasting sound, a happy Diwali song and a happy Diwali photo in the folder where your program file is situated. In my program, I have used the sound in “.wav” format and image in “.jpg” format. Lastly, you can display a photo of happy Diwali using Image.open() and ImageTk.PhotoImag() methods. You can play bombing sound and happy Diwali song using winsound.PlaySound() method. Finally, you can initiate the GUI event loop using mainloop().

How run the program :

I have run the happy Diwali program in VS Code. If you want to run python code in VS Code follow my link. At first, create a new directory or folder for your project, naming it something like “happy_diwali_project”. After open the folder, right click of the mouse and click on “Open with Code”. So your VS Code is open in your project folder. Here, you create a file and save with .py extension such as “happy_diwali.py”.

Now, copy the source code of wishing happy Diwali program using the tkinter library of Python language and paste in the file (happy_diwali.py) you have just created. After that, run the program and you can see the animation of wishing happy Diwali on the screen.

Source code of happy Diwali program by python :

The below code is the source code of wishing happy Diwali program using the tkinter library by Python language. You can copy the code and use in your python program.

# Developed by Puskar Jasu
from tkinter import *
import random
import winsound
from PIL import Image, ImageTk
root = Tk()
root.title("Happy Diwali")
canvas_width = 1200
canvas_height = 650
w = Canvas(root, width=canvas_width, height=canvas_height,bg="black")          
w.pack()
s1=0
s2=0
s3=0
s4=0
ss1=0
ss2=0
ss3=0
ss4=0
n=0
i=0
j=0
k=0
m=0
x1=0
y1=0
p=0
points = [50,400,150,350,250,400]
points1 = [130,500,130,520,140,540,140,560,150,580,190,580,180,560,180,540,170,520,170,500]
w.create_polygon(points, fill='brown4', width=3)           
w.create_rectangle(80, 400, 220, 500, fill="blue")
w.create_rectangle(85, 430, 115, 470, fill="red")
w.create_rectangle(185, 430, 215, 470, fill="red")
w.create_rectangle(130, 420, 170, 490, fill="green")
w.create_polygon(points1, fill='goldenrod2', width=3)            
w.create_line(130, 520, 170, 520, fill="#476042", width=3)
w.create_line(140, 540, 180, 540, fill="#476042", width=3)
w.create_line(140, 560, 180, 560, fill="#476042", width=3)
w.create_rectangle(655, 550, 705, 600, fill="hotpink")
w.create_rectangle(675, 490, 685, 550, fill="lightcoral")
w.create_oval(674, 480, 686, 500, fill="#476042")
while p<130-14:
     w.create_text(180, 40,  text="DEVELOPED  BY", fill="red",font=('verdana', 30))
     w.create_text(150, 80,  text="PUSKAR JASU", fill="yellow",font=('verdana', 25))
     w.create_rectangle(x1-20,y1-20,x1+20, y1+20, fill="black")
     x1=random.randint(0,1200)
     y1=random.randint(0,330)
     winsound.Beep(300, 100)
     x=random.randint(0,10)
     y=random.randint(0,10)
     xx=random.randint(0,10)
     yy=random.randint(0,10)
     w.create_oval(s3,s4,s3+20,s4+20,fill="black")
     w.create_oval(s1,s2,s1+20,s2+20,fill="black")
     w.create_oval(ss1,ss2,ss1+20,ss2+20,fill="black")
     w.create_oval(ss3,ss4,ss3+20,ss4+20,fill="black")
     s1=random.randint(0,1200)
     s2=random.randint(0,330)
     s3=random.randint(0,1200)
     s4=random.randint(0,330)
     ss1=random.randint(0,1200)
     ss2=random.randint(0,330)
     ss3=random.randint(0,1200)
     ss4=random.randint(0,330)
     w.create_line(x1,y1,x1+2*x, y1-2*y, fill="green1")
     w.create_line(x1, y1, x1-x*2, y1+y*2, fill="red")
     w.create_line(x1, y1, x1+x*2, y1+y*2, fill="yellow")
     w.create_line(x1, y1, x1-x*2, y1-y*2, fill="blue")
     w.create_line(x1,y1,x1+2*xx, y1-2*yy, fill="green1")
     w.create_line(x1, y1, x1-xx*2, y1+yy*2, fill="red")
     w.create_line(x1, y1, x1+xx*2, y1+yy*2, fill="yellow")
     w.create_line(x1, y1, x1-xx*2, y1-yy*2, fill="blue")
     w.create_oval(s1,s2,s1+20,s2+20,fill="red")
     w.create_oval(s3,s4,s3+20,s4+20,fill="blue")
     w.create_oval(ss1,ss2,ss1+20,ss2+20,fill="green1")
     w.create_oval(ss3,ss4,ss3+20,ss4+20,fill="orchid")
     w.create_oval(242+4+i, 450, 272+i+4, 490, fill="black")
     w.create_oval(250+i, 450, 280+i, 490, fill="bisque4")
     w.create_rectangle(260-4+i, 490, 270+i-4, 500, fill="black")
     w.create_rectangle(260+i, 490, 270+i, 500, fill="bisque3")
     if i>=320:
         i=320
     w.create_rectangle(240+i-4, 500, 290-4+i, 560, fill="black")
     w.create_rectangle(240+i, 500, 290+i, 560, fill="azure2")
     w.create_line(240+i-4, 500, 230-4+i, 554, fill="black",width=12)
     w.create_line(240+i, 500, 230+i, 550, fill="blue",width=5)
     w.create_line(290-1+i, 500, 300-6+i, 552, fill="black",width=5)
     w.create_line(290+i, 500, 300+i, 550, fill="blue",width=5)
     w.create_rectangle(240-4+i, 560, 250-4+i, 600, fill="black")
     w.create_rectangle(240+i, 560, 250+i, 600, fill="burlywood")
     w.create_rectangle(280-4+i, 560, 290-4+i, 600, fill="black")
     w.create_rectangle(280+i, 560, 290+i, 600, fill="burlywood")
     if i>=320:
         i=320
         w.create_line(290+i, 500, 300+i, 550, fill="black",width=5)
         w.create_line(290+i, 500, 300+i+j, 556-j, fill="black",width=10)
         w.create_line(290+i, 500, 300+i+j, 550-j, fill="blue",width=5)
         j+=3
         if j>=54:
             j=54
             w.create_rectangle(655, 550, 705, 600, fill="hotpink")
             w.create_line(290+i, 500, 300+i+j+4, 554-j, fill="black",width=12)
             w.create_line(290+i+2, 500, 300+7+i+j-k, 549-4-j+k, fill="black",width=7)
             w.create_line(290-2+i, 500, 300+i+j-k, 550-j+k, fill="blue",width=5)
             w.create_rectangle(675, 490-m, 685, 550-m, fill="lightcoral")
             w.create_oval(674, 480-m, 686, 500-m, fill="red")
             w.create_rectangle(670, 550-m, 690, 566-m, fill="black")
             k+=3
             m+=16
             if k>=55:
                k=55
             if m>=300:
                  m=300
                  w.create_rectangle(675, 490-m, 685, 565-m, fill="black")
                  w.create_oval(674, 480-m, 686, 500-m, fill="black")
                  r1=random.randint(0,120)
                  r2=random.randint(0,120)
                  while n<50:
                        x=random.randint(0,90)
                        y=random.randint(0,90)
                        x1=random.randint(0,90)
                        y1=random.randint(0,90)
                        w.create_oval(660, 200, 700, 240, fill="red")
                        w.create_line(680, 220, 680+x, 220-y, fill="red")
                        w.create_line(680, 220, 680-x, 220+y, fill="red")
                        w.create_line(680, 220, 680+x, 220+y, fill="red")
                        w.create_line(680, 220, 680-x, 220-y, fill="red")
                        w.create_line(680+200, 220, 680+200+x1-30, 220-y1+30, fill="blue")
                        w.create_line(680+200, 220, 680-x1+200+30, 220+y1-30, fill="blue")
                        w.create_line(680+200, 220, 680+x1+200-30, 220+y1-30, fill="blue")
                        w.create_line(680+200, 220, 680-x1+200+30, 220-y1+30, fill="blue")
                        w.create_line(680+300+100, 220, 680+300+100+x1-30, 220-y1+30,fill="white")
                        w.create_line(680+300+100, 220, 680-x1+100+300+30, 220+y1-30, fill="white")
                        w.create_line(680+300+100, 220, 680+x1+100+300-30, 220+y1-30, fill="white")
                        w.create_line(680+300+100, 220, 680-x1+100+300+30, 220-y1+30, fill="white")
                        w.create_line(480, 220, 480+x1-30, 220-y1+30, fill="darkseagreen1")
                        w.create_line(480, 220, 480-x1+30, 220+y1-30, fill="darkseagreen1")
                        w.create_line(480, 220, 480+x1-30, 220+y1-30, fill="darkseagreen1")
                        w.create_line(480, 220, 480-x1+30, 220-y1+30, fill="darkseagreen1")
                        w.create_line(280, 220, 280+x1-30, 220-y1+30, fill="deeppink1")
                        w.create_line(280, 220, 280-x1+30, 220+y1-30, fill="deeppink1")
                        w.create_line(280, 220, 280+x1-30, 220+y1-30, fill="deeppink1")
                        w.create_line(280, 220, 280-x1+30, 220-y1+30, fill="deeppink1")
                        w.create_line(380-300, 220, 380-300+x1-30, 220-y1+30, fill="yellow")
                        w.create_line(380-300, 220, 380-300-x1+30, 220+y1-30, fill="yellow")
                        w.create_line(380-300, 220, 380-300+x1-30, 220+y1-30, fill="yellow")
                        w.create_line(380-300, 220, 380-300-x1+30, 220-y1+30, fill="yellow")
                        if n==1:
                            filename = 'mixkit-8-bit-bomb-explosion-2811.wav'
                            winsound.PlaySound(filename, winsound.SND_FILENAME)
                        w.update()
                        n+=1
                        if n>=50:
                            n=50
                            w.create_rectangle(630-40, 170-40, 730+40, 270+40, fill="black")
                            w.create_rectangle(330-40, 170-40, 430+40, 270+40, fill="black")
                            w.create_rectangle(930-40, 170-40, 1030+40, 270+40, fill="black")
                            im = Image.open('dipawali.jpg')
                            im = im.resize((1200, 450), Image.ANTIALIAS)
                            w.image = ImageTk.PhotoImage(im)
                            w.create_image(0, 0, image=w.image, anchor='nw')
                            w.create_text(50+40, 220,  text="H", fill="red",font=('verdana', 140))
                            w.create_text(170+40, 220,  text="a", fill="blue",font=('verdana', 120))
                            w.create_text(270+40, 220,  text="p", fill="green",font=('verdana', 120))
                            w.create_text(370+40, 220,  text="p", fill="yellow",font=('verdana', 120))
                            w.create_text(470+40, 220,  text="y", fill="white",font=('verdana', 120))
                            w.create_text(700, 220,  text="D", fill="cyan",font=('verdana', 140))
                            w.create_text(800, 220,  text="i", fill="magenta",font=('verdana', 120))
                            w.create_text(900, 220,  text="w", fill="brown",font=('verdana', 120))
                            w.create_text(1030, 220,  text="a", fill="chartreuse1",font=('verdana', 120))
                            w.create_text(1100, 220,  text="l", fill="crimson",font=('verdana', 120))
                            w.create_text(1000+150, 220,  text="i", fill="darkolivegreen1",font=('verdana', 120))
                            w.create_text(180, 40,  text="DEVELOPED  BY", fill="red",font=('verdana', 30))
                            w.create_text(150, 80,  text="PUSKAR JASU", fill="yellow",font=('verdana', 25))
                            w.create_polygon(points, fill='brown4', width=3)
                            w.create_rectangle(80, 400, 220, 500, fill="blue")
                            w.create_rectangle(85, 430, 115, 470, fill="red")
                            w.create_rectangle(185, 430, 215, 470, fill="red")
                            w.create_rectangle(130, 420, 170, 490, fill="green")       
     i+=4
     p+=1
     w.update()
filename = 'diwali-song-61.wav'
winsound.PlaySound(filename, winsound.SND_FILENAME)
mainloop()

Output :

After running the program, you can see the wishing happy Diwali program using the tkinter library by Python programming language come to life on your screen. You can also see the output of the happy Diwali program in my YouTube channel.

Conclusion :

Finally, you learned how to create an animation of graphical program to wish someone a Happy Diwali using the tkinter library in Python with source code. You can add more graphics, animations and creativity to make your Diwali wishing graphics program even more festive and attractive. Thank you for visiting my site. Wish you happy Diwali.

Scroll to Top