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.

 

CODE SNIPPET:->



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 

pygame.mixer.init()
pygame.mixer.music.load('')
pygame.mixer.music.play(loops=0)

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),  pygame.mixer.music.load('C:/Users/sweta/Pictures/images/one.mp3'), pygame.mixer.music.play(loops=0)),   font=("Courier New", 10, 'bold'), bg="pink")
btn_2= Button(root, text="2",bd=4, padx=40, pady=20,command=lambda: ( click_butn(2),  pygame.mixer.music.load('C:/Users/sweta/Pictures/images/two.mp3'), pygame.mixer.music.play(loops=0)),   font=("Courier New", 10, 'bold'), bg="pink")
btn_3= Button(root, text="3",bd=4, padx=40, pady=20,command=lambda: ( click_butn(3),  pygame.mixer.music.load('C:/Users/sweta/Pictures/images/three.mp3'), pygame.mixer.music.play(loops=0)),   font=("Courier New", 10, 'bold'), bg="pink")
btn_4= Button(root, text="4",bd=4, padx=40, pady=20,command=lambda: ( click_butn(4),  pygame.mixer.music.load('C:/Users/sweta/Pictures/images/four.mp3'), pygame.mixer.music.play(loops=0)),   font=("Courier New", 10, 'bold'), bg="pink")
btn_5= Button(root, text="5",bd=4, padx=40, pady=20,command=lambda:( click_butn(5),  pygame.mixer.music.load('C:/Users/sweta/Pictures/images/five.mp3'), pygame.mixer.music.play(loops=0)),   font=("Courier New", 10, 'bold'), bg="pink")
btn_6= Button(root, text="6",bd=4, padx=40, pady=20,command=lambda:( click_butn(6),  pygame.mixer.music.load('C:/Users/sweta/Pictures/images/six.mp3'), pygame.mixer.music.play(loops=0)),   font=("Courier New", 10, 'bold'), bg="pink")
btn_7= Button(root, text="7",bd=4, padx=40, pady=20,command=lambda: ( click_butn(7),  pygame.mixer.music.load('C:/Users/sweta/Pictures/images/seven.mp3'), pygame.mixer.music.play(loops=0)),   font=("Courier New", 10, 'bold'), bg="pink")
btn_8= Button(root, text="8",bd=4, padx=40, pady=20,command=lambda:( click_butn(8),  pygame.mixer.music.load('C:/Users/sweta/Pictures/images/eight.mp3'), pygame.mixer.music.play(loops=0)),   font=("Courier New", 10, 'bold'), bg="pink")
btn_9= Button(root, text="9",bd=4, padx=40, pady=20,command=lambda: ( click_butn(9),  pygame.mixer.music.load('C:/Users/sweta/Pictures/images/nine.mp3'), pygame.mixer.music.play(loops=0)),   font=("Courier New", 10, 'bold'), bg="pink")
btn_0= Button(root, text="0", bd=4, padx=40, pady=20,command=lambda: ( click_butn(0),  pygame.mixer.music.load('C:/Users/sweta/Pictures/images/zero.mp3'), pygame.mixer.music.play(loops=0)),   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.

 

 

OUTPUT:->

Download Complete Code

Comments

No comments yet