GUI CALCULATOR USING PYTHON
Source Code:-
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | # GUI Calculator using Python Programming Language from tkinter import * root = Tk() # Download icons in '.ico' format from https://iconarchive.com/" root.iconbitmap( "D:/PYTHON MODULES/TKINTER/TKINTER PROJECTS/ICONs/Calculator.ico" ) root.title( "PYTHON GUI CALCULATOR" ) root.geometry( "340x378" ) root.resizable( 0 , 0 ) F = Frame(root, bg = "navy" , bd = 10 , width = 350 , height = 400 ) F.pack() # create a function to get the entry by pressing number def press(text): num = txt.get() if num = = "Error" : txt. set ( "Error" ) else : txt. set (num + text) # clear all Entry def reset(): txt. set ("") # cancel a Entry def cancel(): txt. set (txt.get()[: - 1 ]) # calculate the expressions def cal(): try : txt. set ( eval (txt.get())) except : txt. set ( "Error" ) # create a Entry to display the inputs txt = StringVar() Entry(F, textvariable = txt, font = ( "None 20 bold" ), state = "readonly" , borderwidth = 2 ).grid(columnspan = 5 , pady = 10 , ipadx = 3 , ipady = 8 ) Button(F, text = "C" , command = reset, font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "lightblue" ).grid(row = 1 , column = 0 ) Button(F, text = "(" , command = lambda : press( "(" ), font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "lightblue" ).grid(row = 1 , column = 1 ) Button(F, text = ")" , command = lambda : press( ")" ), font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "lightblue" ).grid(row = 1 , column = 2 ) Button(F, text = "CE" , command = cancel, font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "lightblue" ).grid(row = 1 , column = 3 ) Button(F, text = "7" , command = lambda : press( "7" ), font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "lightblue" ).grid(row = 2 , column = 0 ) Button(F, text = "8" , command = lambda : press( "8" ), font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "lightblue" ).grid(row = 2 , column = 1 ) Button(F, text = "9" , command = lambda : press( "9" ), font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "lightblue" ).grid(row = 2 , column = 2 ) Button(F, text = "/" , command = lambda : press( "/" ), font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "steelblue1" ).grid(row = 2 , column = 3 ) Button(F, text = "4" , command = lambda : press( "4" ), font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "lightblue" ).grid(row = 3 , column = 0 ) Button(F, text = "5" , command = lambda : press( "5" ), font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "lightblue" ).grid(row = 3 , column = 1 ) Button(F, text = "6" , command = lambda : press( "6" ), font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "lightblue" ).grid(row = 3 , column = 2 ) Button(F, text = "x" , command = lambda : press( "*" ), font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "steelblue1" ).grid(row = 3 , column = 3 ) Button(F, text = "1" , command = lambda : press( "1" ), font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "lightblue" ).grid(row = 4 , column = 0 ) Button(F, text = "2" , command = lambda : press( "2" ), font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "lightblue" ).grid(row = 4 , column = 1 ) Button(F, text = "3" , command = lambda : press( "3" ), font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "lightblue" ).grid(row = 4 , column = 2 ) Button(F, text = "-" , command = lambda : press( "-" ), font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "steelblue1" ).grid(row = 4 , column = 3 ) Button(F, text = "." , command = lambda : press( "." ), font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "lightblue" ).grid(row = 5 , column = 0 ) Button(F, text = "0" , command = lambda : press( "0" ), font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "lightblue" ).grid(row = 5 , column = 1 ) Button(F, text = "=" , command = cal, font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "salmon2" ).grid(row = 5 , column = 2 ) Button(F, text = "+" , command = lambda : press( "+" ), font = ( "None 20 bold" ), width = 4 , bg = "yellow" , activebackground = "steelblue1" ).grid(row = 5 , column = 3 ) root.mainloop() # Follow @python_with_shubham |
# GUI Calculator using Python Programming Language from tkinter import * root = Tk() # Download icons in '.ico' format from https://iconarchive.com/" root.iconbitmap("D:/PYTHON MODULES/TKINTER/TKINTER PROJECTS/ICONs/Calculator.ico") root.title("PYTHON GUI CALCULATOR") root.geometry("340x378") root.resizable(0, 0) F = Frame(root, bg="navy", bd=10, width=350, height=400) F.pack() # create a function to get the entry by pressing number def press(text): num = txt.get() if num == "Error": txt.set("Error") else: txt.set(num + text) # clear all Entry def reset(): txt.set("") # cancel a Entry def cancel(): txt.set(txt.get()[:-1]) # calculate the expressions def cal(): try: txt.set(eval(txt.get())) except: txt.set("Error") # create a Entry to display the inputs txt = StringVar() Entry(F, textvariable=txt, font=("None 20 bold"), state="readonly", borderwidth=2).grid(columnspan=5, pady=10, ipadx=3, ipady=8) Button(F, text="C", command=reset, font=("None 20 bold"), width=4, bg="yellow", activebackground="lightblue").grid(row=1, column=0) Button(F, text="(", command=lambda: press("("), font=("None 20 bold"), width=4, bg="yellow", activebackground="lightblue").grid(row=1, column=1) Button(F, text=")", command=lambda: press(")"), font=("None 20 bold"), width=4, bg="yellow", activebackground="lightblue").grid(row=1, column=2) Button(F, text="CE", command=cancel, font=("None 20 bold"), width=4, bg="yellow", activebackground="lightblue").grid(row=1, column=3) Button(F, text="7", command=lambda: press("7"), font=("None 20 bold"), width=4, bg="yellow", activebackground="lightblue").grid(row=2, column=0) Button(F, text="8", command=lambda: press("8"), font=("None 20 bold"), width=4, bg="yellow", activebackground="lightblue").grid(row=2, column=1) Button(F, text="9", command=lambda: press("9"), font=("None 20 bold"), width=4, bg="yellow", activebackground="lightblue").grid(row=2, column=2) Button(F, text="/", command=lambda: press("/"), font=("None 20 bold"), width=4, bg="yellow", activebackground="steelblue1").grid(row=2, column=3) Button(F, text="4", command=lambda: press("4"), font=("None 20 bold"), width=4, bg="yellow", activebackground="lightblue").grid(row=3, column=0) Button(F, text="5", command=lambda: press("5"), font=("None 20 bold"), width=4, bg="yellow", activebackground="lightblue").grid(row=3, column=1) Button(F, text="6", command=lambda: press("6"), font=("None 20 bold"), width=4, bg="yellow", activebackground="lightblue").grid(row=3, column=2) Button(F, text="x", command=lambda: press("*"), font=("None 20 bold"), width=4, bg="yellow", activebackground="steelblue1").grid(row=3, column=3) Button(F, text="1", command=lambda: press("1"), font=("None 20 bold"), width=4, bg="yellow", activebackground="lightblue").grid(row=4, column=0) Button(F, text="2", command=lambda: press("2"), font=("None 20 bold"), width=4, bg="yellow", activebackground="lightblue").grid(row=4, column=1) Button(F, text="3", command=lambda: press("3"), font=("None 20 bold"), width=4, bg="yellow", activebackground="lightblue").grid(row=4, column=2) Button(F, text="-", command=lambda: press("-"), font=("None 20 bold"), width=4, bg="yellow", activebackground="steelblue1").grid(row=4, column=3) Button(F, text=".", command=lambda: press("."), font=("None 20 bold"), width=4, bg="yellow", activebackground="lightblue").grid(row=5, column=0) Button(F, text="0", command=lambda: press("0"), font=("None 20 bold"), width=4, bg="yellow", activebackground="lightblue").grid(row=5, column=1) Button(F, text="=", command=cal, font=("None 20 bold"), width=4, bg="yellow", activebackground="salmon2").grid(row=5, column=2) Button(F, text="+", command=lambda: press("+"), font=("None 20 bold"), width=4, bg="yellow", activebackground="steelblue1").grid(row=5, column=3) root.mainloop() # Follow @python_with_shubham
0 Comments