Sunday, 28 May 2017

GUI:Calculator program

import sys
from tkinter import *
def btnClearDisplay( ):
    global operator
    operator=" "
    input_num.set(" ")

def btnClick(numbers):
    global operator
    operator=operator +str(numbers)
    input_num.set(operator)

def btnEqual( ):
    global operator
    sumup=str(eval(operator))
    input_num.set(sumup)
    operator=" "
 
root=Tk( )
frame=Frame(root)
frame.pack()
root.title('calculater')
operator=" "
input_num=StringVar()
frame1=Frame(root)
frame1.pack(side=TOP)
txtDisplay=Entry(frame1,textvariable=input_num,bd=20,insertwidth=1,font=30)
txtDisplay.pack(side=TOP)
button1=Button(frame1,padx=16,pady=16,bd=8,text="1",fg="black",command=lambda:btnClick(1))
button1.pack(side=LEFT)
button2=Button(frame1,padx=16,pady=16,bd=8,text="2",fg="black",command=lambda:btnClick(2))
button2.pack(side=LEFT)
button3=Button(frame1,padx=16,pady=16,bd=8,text="3",fg="black",command=lambda:btnClick(3))
button3.pack(side=LEFT)
button4=Button(frame1,padx=16,pady=16,bd=8,text="4",fg="black",command=lambda:btnClick(4))
button4.pack(side=LEFT)

frame2=Frame(root)
frame2.pack(side=TOP)

button1=Button(frame2,padx=16,pady=16,bd=8,text="5",fg="black",command=lambda:btnClick(5))
button1.pack(side=LEFT)
button2=Button(frame2,padx=16,pady=16,bd=8,text="6",fg="black",command=lambda:btnClick(6))
button2.pack(side=LEFT)
button3=Button(frame2,padx=16,pady=16,bd=8,text="7",fg="black",command=lambda:btnClick(7))
button3.pack(side=LEFT)
button4=Button(frame2,padx=16,pady=16,bd=8,text="8",fg="black",command=lambda:btnClick(8))
button4.pack(side=LEFT)

frame3=Frame(root)
frame3.pack(side=TOP)

button1=Button(frame3,padx=16,pady=16,bd=8,text="9",fg="black",command=lambda:btnClick(9))
button1.pack(side=LEFT)
button2=Button(frame3,padx=16,pady=16,bd=8,text="0",fg="black",command=lambda:btnClick(0))
button2.pack(side=LEFT)
button3=Button(frame3,padx=16,pady=16,bd=8,text="c",fg="black",command=btnClearDisplay)
button3.pack(side=LEFT)
button4=Button(frame3,padx=16,pady=16,bd=8,text="=",fg="black",command=btnEqual)
button4.pack(side=LEFT)

frame4=Frame(root)
frame4.pack(side=TOP)

button1=Button(frame4,padx=16,pady=16,bd=8,text="*",fg="black",command=lambda:btnClick("*"))
button1.pack(side=LEFT)
button2=Button(frame4,padx=16,pady=16,bd=8,text="/",fg="black",command=lambda:btnClick("/"))
button2.pack(side=LEFT)
button3=Button(frame4,padx=16,pady=16,bd=8,text="-",fg="black",command=lambda:btnClick("-"))
button3.pack(side=LEFT)
button4=Button(frame4,padx=16,pady=16,bd=8,text="+",fg="black",command=lambda:btnClick("+"))
button4.pack(side=LEFT)


root.mainloop( )

Output:


No comments:

Post a Comment