Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions 110 guicalculator
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
from tkinter import *

# globally declare the expression variable
expression = ""


# Function to update expressiom
# in the text entry box
def press(num):
global expression
expression = expression + str(num)
equation.set(expression)


# Function to evaluate the final expression
def equalpress():
try:
global expression
total = str(eval(expression))
equation.set(total)
expression = ""

except:
equation.set(" error ")
expression = ""



def clear():
global expression
expression = ""
equation.set("")





if __name__ == "__main__":
# create a GUI window
gui = Tk()

# set the background colour of GUI window
gui.configure(background="light green")


gui.title("Simple Calculator")

gui.geometry("265x265")

# StringVar() is the variable class
# we create an instance of this class
equation = StringVar()

# create the text entry box for
# showing the expression .
expression_field = Entry(gui, textvariable=equation)
expression_field.grid(columnspan=4, ipadx=70)

equation.set('enter your expression')

button1 = Button(gui, text=' 1 ',fg='black',bg='red',command=lambda: press(1))
button1.grid(row=2, column=0)

button2 = Button(gui, text=' 2 ',fg='black',bg='red',command=lambda: press(2))
button2.grid(row=2, column=1)

button3 = Button(gui, text=' 3 ',fg='black',bg='red',command=lambda: press(3))
button3.grid(row=2, column=2)

button4 = Button(gui, text=' 4 ',fg='black',bg='red',command=lambda: press(4))
button4.grid(row=3, column=0)

button5 = Button(gui, text=' 5 ',fg='black',bg='red',command=lambda: press(5))
button5.grid(row=3, column=1)

button6 = Button(gui, text=' 6 ',fg='black',bg='red',command=lambda: press(6))
button6.grid(row=3, column=2)

button7 = Button(gui, text=' 7 ',fg='black',bg='red',command=lambda: press(7))
button7.grid(row=4, column=0)

button8 = Button(gui, text=' 8 ',fg='black',bg='red',command=lambda: press(8))
button8.grid(row=4, column=1)

button9 = Button(gui, text=' 9 ',fg='black',bg='red',command=lambda: press(9))
button9.grid(row=4, column=2)

button0 = Button(gui, text=' 0 ',fg='black',bg='red',command=lambda: press(0))
button0.grid(row=5, column=0)

plus = Button(gui, text=' + ',fg='black',bg='red',command=lambda: press("+"))
plus.grid(row=2, column=3)

minus = Button(gui, text=' - ',fg='black',bg='red',command=lambda: press("-"))
minus.grid(row=3, column=3)

multiply = Button(gui, text=' * ',fg='black',bg='red',command=lambda: press("*"))
multiply.grid(row=4, column=3)

divide = Button(gui, text=' / ',fg='black',bg='red',command=lambda: press("/"))
divide.grid(row=5, column=3)

equal = Button(gui, text=' = ', fg='black',bg='red',command=equalpress)
equal.grid(row=5, column=2)

clear = Button(gui, text='Clear', fg='black', bg='red',command=clear)
clear.grid(row=5, column='1')

# start the GUI
gui.mainloop()
Morty Proxy This is a proxified and sanitized view of the page, visit original site.