Coders Packet

AUDIO CALCULATOR using pygame in Python

By Sweta Kumari

This calculator is programmed using pygame in Python for audio and Tkinter for the interface of the calculator.

The audio system in the calculator enhances the calculator more and makes it fun to use.



step 1: in step one we just build the interface of our Tkinter. the height and width you desired the background colour you want you can put all theses according to your mood to create the interface.



from tkinter import * root=Tk() root.title("CODESPEEDY Calculator") x= Entry(root,width=35,borderwidth=5, bg="yellow") x.grid(row=0, column=0, columnspan=3, padx=10,pady=10)

step 2:   now we will be adding pygame module to this code for our audio purpose. and we also use pygame mixer module inside pygame which helps to control music(audio in our case) in pygame program.

import pygame 


step 3: then we would make certain function for different kind of purpose in our calculator. lets say what we want our calculator to do when certain numbers are pressed so we will make [click_butn] function and define the work inside it. similarly we would do for plus, minus and different kind of buttons and works. i have listed some of the functions that I have used in our program. 

                                 ->def click_butn(num):

                                 ->def butn_add():

                                 ->def bclear():

                                 -> def b_equal():

                                 ->def butn_subtract():

                                ->def butn_multiply():

                                ->def butn_divide():


step 4:

then we have defined each buttons of numbers using butn_number and defined its width, font colour and function and even audio for each button which is saved in my file manager and I have used from my local file manager.

btn_1= Button(root, text="1",bd=4, padx=40, pady=20,command=lambda: ( click_butn(1),'C:/Users/sweta/Pictures/images/one.mp3'),,   font=("Courier New", 10, 'bold'), bg="pink")
btn_2= Button(root, text="2",bd=4, padx=40, pady=20,command=lambda: ( click_butn(2),'C:/Users/sweta/Pictures/images/two.mp3'),,   font=("Courier New", 10, 'bold'), bg="pink")
btn_3= Button(root, text="3",bd=4, padx=40, pady=20,command=lambda: ( click_butn(3),'C:/Users/sweta/Pictures/images/three.mp3'),,   font=("Courier New", 10, 'bold'), bg="pink")
btn_4= Button(root, text="4",bd=4, padx=40, pady=20,command=lambda: ( click_butn(4),'C:/Users/sweta/Pictures/images/four.mp3'),,   font=("Courier New", 10, 'bold'), bg="pink")
btn_5= Button(root, text="5",bd=4, padx=40, pady=20,command=lambda:( click_butn(5),'C:/Users/sweta/Pictures/images/five.mp3'),,   font=("Courier New", 10, 'bold'), bg="pink")
btn_6= Button(root, text="6",bd=4, padx=40, pady=20,command=lambda:( click_butn(6),'C:/Users/sweta/Pictures/images/six.mp3'),,   font=("Courier New", 10, 'bold'), bg="pink")
btn_7= Button(root, text="7",bd=4, padx=40, pady=20,command=lambda: ( click_butn(7),'C:/Users/sweta/Pictures/images/seven.mp3'),,   font=("Courier New", 10, 'bold'), bg="pink")
btn_8= Button(root, text="8",bd=4, padx=40, pady=20,command=lambda:( click_butn(8),'C:/Users/sweta/Pictures/images/eight.mp3'),,   font=("Courier New", 10, 'bold'), bg="pink")
btn_9= Button(root, text="9",bd=4, padx=40, pady=20,command=lambda: ( click_butn(9),'C:/Users/sweta/Pictures/images/nine.mp3'),,   font=("Courier New", 10, 'bold'), bg="pink")
btn_0= Button(root, text="0", bd=4, padx=40, pady=20,command=lambda: ( click_butn(0),'C:/Users/sweta/Pictures/images/zero.mp3'),,   font=("Courier New", 10, 'bold'), bg="pink")
badd= Button(root, text="+", bg="powder blue",bd=4, padx=40, pady=20,command=butn_add)
bsubs= Button(root, text="-",bg="powder blue",bd=4,  padx=41, pady=20,command=butn_subtract)
bmulti= Button(root, text="*", bg="powder blue",bd=4,  padx=40, pady=20,command=butn_multiply)
bdiv= Button(root, text="/", bg="powder blue",bd=4, padx=40, pady=20,command=butn_divide)
bequal= Button(root, text="=",bg="powder blue",bd=4,  padx=88, pady=20,command=b_equal)
bclear= Button(root, text="Clear", bg="powder blue",bd=4,  padx=80, pady=20,command=bclear)


 step 5: in step 5 I have assigned all button according to the row and grid system which is very essential step as we all want our calculator to look organized.




Download Complete Code


No comments yet