| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,148 @@ | |||
| 1 | + | ||
| 2 | + from tkinter import * | ||
| 3 | + expression = "" | ||
| 4 | + def press(num): | ||
| 5 | + | ||
| 6 | + global expression | ||
| 7 | + expression = expression + str(num) | ||
| 8 | + | ||
| 9 | + # update the expression by using set method | ||
| 10 | + equation.set(expression) | ||
| 11 | + | ||
| 12 | + | ||
| 13 | + # Function to evaluate the final expression | ||
| 14 | + def equalpress(): | ||
| 15 | + | ||
| 16 | + try: | ||
| 17 | + | ||
| 18 | + global expression | ||
| 19 | + | ||
| 20 | + # eval function evaluate the expression | ||
| 21 | + # and str function convert the result | ||
| 22 | + # into string | ||
| 23 | + total = str(eval(expression)) | ||
| 24 | + | ||
| 25 | + equation.set(total) | ||
| 26 | + | ||
| 27 | + # initialze the expression variable | ||
| 28 | + # by empty string | ||
| 29 | + expression = "" | ||
| 30 | + | ||
| 31 | + # if error is generate then handle | ||
| 32 | + # by the except block | ||
| 33 | + except: | ||
| 34 | + | ||
| 35 | + equation.set(" error ") | ||
| 36 | + expression = "" | ||
| 37 | + | ||
| 38 | + | ||
| 39 | + # Function to clear the contents | ||
| 40 | + # of text entry box | ||
| 41 | + def clear(): | ||
| 42 | + global expression | ||
| 43 | + expression = "" | ||
| 44 | + equation.set("") | ||
| 45 | + | ||
| 46 | + | ||
| 47 | + # Driver code | ||
| 48 | + if __name__ == "__main__": | ||
| 49 | + # create a GUI window | ||
| 50 | + gui = Tk() | ||
| 51 | + | ||
| 52 | + # set the background colour of GUI window | ||
| 53 | + gui.configure(background="light green") | ||
| 54 | + | ||
| 55 | + # set the title of GUI window | ||
| 56 | + gui.title("Simple Calculator") | ||
| 57 | + | ||
| 58 | + # set the configuration of GUI window | ||
| 59 | + gui.geometry("270x150") | ||
| 60 | + | ||
| 61 | + # StringVar() is the variable class | ||
| 62 | + # we create an instance of this class | ||
| 63 | + equation = StringVar() | ||
| 64 | + | ||
| 65 | + # create the text entry box for | ||
| 66 | + # showing the expression . | ||
| 67 | + expression_field = Entry(gui, textvariable=equation) | ||
| 68 | + | ||
| 69 | + # grid method is used for placing | ||
| 70 | + # the widgets at respective positions | ||
| 71 | + # in table like structure . | ||
| 72 | + expression_field.grid(columnspan=4, ipadx=70) | ||
| 73 | + | ||
| 74 | + equation.set('enter your expression') | ||
| 75 | + | ||
| 76 | + # create a Buttons and place at a particular | ||
| 77 | + # location inside the root window . | ||
| 78 | + # when user press the button, the command or | ||
| 79 | + # function affiliated to that button is executed . | ||
| 80 | + button1 = Button(gui, text=' 1 ', fg='black', bg='red', | ||
| 81 | + command=lambda: press(1), height=1, width=7) | ||
| 82 | + button1.grid(row=2, column=0) | ||
| 83 | + | ||
| 84 | + button2 = Button(gui, text=' 2 ', fg='black', bg='red', | ||
| 85 | + command=lambda: press(2), height=1, width=7) | ||
| 86 | + button2.grid(row=2, column=1) | ||
| 87 | + | ||
| 88 | + button3 = Button(gui, text=' 3 ', fg='black', bg='red', | ||
| 89 | + command=lambda: press(3), height=1, width=7) | ||
| 90 | + button3.grid(row=2, column=2) | ||
| 91 | + | ||
| 92 | + button4 = Button(gui, text=' 4 ', fg='black', bg='red', | ||
| 93 | + command=lambda: press(4), height=1, width=7) | ||
| 94 | + button4.grid(row=3, column=0) | ||
| 95 | + | ||
| 96 | + button5 = Button(gui, text=' 5 ', fg='black', bg='red', | ||
| 97 | + command=lambda: press(5), height=1, width=7) | ||
| 98 | + button5.grid(row=3, column=1) | ||
| 99 | + | ||
| 100 | + button6 = Button(gui, text=' 6 ', fg='black', bg='red', | ||
| 101 | + command=lambda: press(6), height=1, width=7) | ||
| 102 | + button6.grid(row=3, column=2) | ||
| 103 | + | ||
| 104 | + button7 = Button(gui, text=' 7 ', fg='black', bg='red', | ||
| 105 | + command=lambda: press(7), height=1, width=7) | ||
| 106 | + button7.grid(row=4, column=0) | ||
| 107 | + | ||
| 108 | + button8 = Button(gui, text=' 8 ', fg='black', bg='red', | ||
| 109 | + command=lambda: press(8), height=1, width=7) | ||
| 110 | + button8.grid(row=4, column=1) | ||
| 111 | + | ||
| 112 | + button9 = Button(gui, text=' 9 ', fg='black', bg='red', | ||
| 113 | + command=lambda: press(9), height=1, width=7) | ||
| 114 | + button9.grid(row=4, column=2) | ||
| 115 | + | ||
| 116 | + button0 = Button(gui, text=' 0 ', fg='black', bg='red', | ||
| 117 | + command=lambda: press(0), height=1, width=7) | ||
| 118 | + button0.grid(row=5, column=0) | ||
| 119 | + | ||
| 120 | + plus = Button(gui, text=' + ', fg='black', bg='red', | ||
| 121 | + command=lambda: press("+"), height=1, width=7) | ||
| 122 | + plus.grid(row=2, column=3) | ||
| 123 | + | ||
| 124 | + minus = Button(gui, text=' - ', fg='black', bg='red', | ||
| 125 | + command=lambda: press("-"), height=1, width=7) | ||
| 126 | + minus.grid(row=3, column=3) | ||
| 127 | + | ||
| 128 | + multiply = Button(gui, text=' * ', fg='black', bg='red', | ||
| 129 | + command=lambda: press("*"), height=1, width=7) | ||
| 130 | + multiply.grid(row=4, column=3) | ||
| 131 | + | ||
| 132 | + divide = Button(gui, text=' / ', fg='black', bg='red', | ||
| 133 | + command=lambda: press("/"), height=1, width=7) | ||
| 134 | + divide.grid(row=5, column=3) | ||
| 135 | + | ||
| 136 | + equal = Button(gui, text=' = ', fg='black', bg='red', | ||
| 137 | + command=equalpress, height=1, width=7) | ||
| 138 | + equal.grid(row=5, column=2) | ||
| 139 | + | ||
| 140 | + clear = Button(gui, text='Clear', fg='black', bg='red', | ||
| 141 | + command=clear, height=1, width=7) | ||
| 142 | + clear.grid(row=5, column='1') | ||
| 143 | + | ||
| 144 | + Decimal= Button(gui, text='.', fg='black', bg='red', | ||
| 145 | + command=lambda: press('.'), height=1, width=7) | ||
| 146 | + Decimal.grid(row=6, column=0) | ||
| 147 | + # start the GUI | ||
| 148 | + gui.mainloop() | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments