Making a Calculator using Tkinter Gui.

 Code..

 

 

from tkinter import *

def click(event):
    global scvalue
    text = event.widget.cget("text")
    print(text)
    if text == "=":
        if scvalue.get().isdigit():
            value = int(scvalue.get())
        else:
            try:
                value = eval(screen1.get())
            except Exception as e:
                print(e)
                value = "undefined.."

        scvalue.set(value)
        screen1.update()

    elif text =="C":
        scvalue.set("")
        screen1.update()
    else:
        scvalue.set(scvalue.get() + text)
        screen1.update()




root = Tk()
root.geometry("600x700")
root.title("Calculator By Faisal")

scvalue = StringVar()
scvalue.set("")

screen1 = Entry(root, textvariable=scvalue,font="lucida 40 bold")
screen1.pack(pady=20,padx=20,fill=X, ipadx=8)

frame1 = Frame(root, bg="grey")
button1 = Button(frame1,text="9", font="lucida 35 bold",padx=28,pady=22)
button1.pack(side=LEFT, pady=12,padx=18)
button1.bind('<Button-1>', click)


button1 = Button(frame1,text="8", font="lucida 35 bold",padx=28,pady=22)
button1.pack(side=LEFT, pady=12,padx=18)
button1.bind('<Button-1>', click)

button1 = Button(frame1,text="7", font="lucida 35 bold",padx=28,pady=22)
button1.pack(side=LEFT, pady=12,padx=18)
button1.bind('<Button-1>', click)

button1 = Button(frame1,text="C", font="lucida 35 bold",padx=28,pady=22)
button1.pack(side=LEFT, pady=12,padx=18)
button1.bind('<Button-1>', click)

frame1.pack()




frame2 = Frame(root, bg="grey")
button1 = Button(frame2,text="6", font="lucida 35 bold",padx=28,pady=22)
button1.pack(side=LEFT, pady=12,padx=18)
button1.bind('<Button-1>', click)


button1 = Button(frame2,text="5", font="lucida 35 bold",padx=28,pady=22)
button1.pack(side=LEFT, pady=12,padx=18)
button1.bind('<Button-1>', click)

button1 = Button(frame2,text="4", font="lucida 35 bold",padx=28,pady=22)
button1.pack(side=LEFT, pady=12,padx=18)
button1.bind('<Button-1>', click)

button1 = Button(frame2,text="*", font="lucida 35 bold",padx=28,pady=22)
button1.pack(side=LEFT, pady=12,padx=18)
button1.bind('<Button-1>', click)

frame2.pack()





frame1 = Frame(root, bg="grey")
button1 = Button(frame1,text="3", font="lucida 35 bold",padx=28,pady=22)
button1.pack(side=LEFT, pady=12,padx=18)
button1.bind('<Button-1>', click)


button1 = Button(frame1,text="2", font="lucida 35 bold",padx=28,pady=22)
button1.pack(side=LEFT, pady=12,padx=18)
button1.bind('<Button-1>', click)

button1 = Button(frame1,text="1", font="lucida 35 bold",padx=28,pady=22)
button1.pack(side=LEFT, pady=12,padx=18)
button1.bind('<Button-1>', click)

button1 = Button(frame1,text="+", font="lucida 35 bold",padx=28,pady=22)
button1.pack(side=LEFT, pady=12,padx=18)
button1.bind('<Button-1>', click)

frame1.pack()




frame1 = Frame(root, bg="grey")
button1 = Button(frame1,text="%", font="lucida 35 bold",padx=28,pady=22)
button1.pack(side=LEFT, pady=12,padx=18)
button1.bind('<Button-1>', click)


button1 = Button(frame1,text="0", font="lucida 35 bold",padx=28,pady=22)
button1.pack(side=LEFT, pady=12,padx=18)
button1.bind('<Button-1>', click)

button1 = Button(frame1,text="-", font="lucida 35 bold",padx=28,pady=22)
button1.pack(side=LEFT, pady=12,padx=18)
button1.bind('<Button-1>', click)

button1 = Button(frame1,text="=", font="lucida 35 bold",padx=28,pady=22)
button1.pack(side=LEFT, pady=12,padx=18)
button1.bind('<Button-1>', click)

frame1.pack()



root.mainloop()

Comments

Popular Posts