from tkinter import *
GRID_DICT = {
1: {"r": 2, "c": 0},
2: {"r": 2, "c": 1},
3: {"r": 2, "c": 2},
4: {"r": 3, "c": 0},
5: {"r": 3, "c": 1},
6: {"r": 3, "c": 2},
7: {"r": 4, "c": 0},
8: {"r": 4, "c": 1},
9: {"r": 4, "c": 2},
0: {"r": 5, "c": 0},
"+": {"r": 2, "c": 3},
"-": {"r": 3, "c": 3},
"*": {"r": 4, "c": 3},
"/": {"r": 5, "c": 3},
"=": {"r": 5, "c": 2},
"c": {"r": 5, "c": 1},
".": {"r": 6, "c": 0},
}
expression = ""
def press(num):
global expression
expression = expression + str(num)
equation .set(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__':
gui = Tk()
gui.configure(background="dark blue")
gui.title("Simple calculator")
gui.geometry("270x150")
equation = StringVar()
expression_field = Entry(gui, textvariable=equation)
expression_field.grid(columnspan=6, ipadx=70)
bn1 = Button(gui, text='1', fg='white', bg='blue', command=lambda: press(1), height=1, width=7)
bn1.grid(row=GRID_DICT[1]["r"], column=GRID_DICT[1]["c"])
bn2 = Button(gui, text='2', fg='white', bg='blue', command=lambda: press(2), height=1, width=7)
bn2.grid(row=GRID_DICT[2]["r"], column=GRID_DICT[2]["c"])
bn3 = Button(gui, text='3', fg='white', bg='blue', command=lambda: press(3), height=1, width=7)
bn3.grid(row=GRID_DICT[3]["r"], column=GRID_DICT[3]["c"])
bn4 = Button(gui, text='4', fg='white', bg='blue', command=lambda: press(4), height=1, width=7)
bn4.grid(row=GRID_DICT[4]["r"], column=GRID_DICT[4]["c"])
bn5 = Button(gui, text='5', fg='white', bg='blue', command=lambda: press(5), height=1, width=7)
bn5.grid(row=GRID_DICT[5]["r"], column=GRID_DICT[5]["c"])
bn6 = Button(gui, text='6', fg='white', bg='blue', command=lambda: press(6), height=1, width=7)
bn6.grid(row=GRID_DICT[6]["r"], column=GRID_DICT[6]["c"])
bn7 = Button(gui, text='7', fg='white', bg='blue', command=lambda: press(7), height=1, width=7)
bn7.grid(row=GRID_DICT[7]["r"], column=GRID_DICT[7]["c"])
bn8 = Button(gui, text='8', fg='white', bg='blue', command=lambda: press(8), height=1, width=7)
bn8.grid(row=GRID_DICT[8]["r"], column=GRID_DICT[8]["c"])
bn9 = Button(gui, text='9', fg='white', bg='blue', command=lambda: press(9), height=1, width=7)
bn9.grid(row=GRID_DICT[9]["r"], column=GRID_DICT[9]["c"])
bn0 = Button(gui, text='0', fg='white', bg='blue', command=lambda: press(0), height=1, width=7)
bn0.grid(row=GRID_DICT[0]["r"], column=GRID_DICT[0]["c"])
plus = Button(gui, text='+', fg='white', bg='blue', command=lambda: press("+"), height=1, width=7)
plus.grid(row=GRID_DICT["+"]["r"], column=GRID_DICT["+"]["c"])
minus = Button(gui, text='-', fg='white', bg='blue', command=lambda: press("-"), height=1, width=7)
minus.grid(row=GRID_DICT["-"]["r"], column=GRID_DICT["-"]["c"])
multiply = Button(gui, text='*', fg='white', bg='blue', command=lambda: press("*"), height=1, width=7)
multiply.grid(row=GRID_DICT["*"]["r"], column=GRID_DICT["*"]["c"])
divide = Button(gui, text='/', fg='white', bg='blue', command=lambda: press("/"), height=1, width=7)
divide.grid(row=GRID_DICT["/"]["r"], column=GRID_DICT["/"]["c"])
equals = Button(gui, text='=', fg='white', bg='blue', command=equalpress, height=1, width=7)
equals.grid(row=GRID_DICT["="]["r"], column=GRID_DICT["="]["c"])
clear = Button(gui, text='C', fg='white', bg='blue', command=clear, height=1, width=7)
clear.grid(row=GRID_DICT["c"]["r"], column=GRID_DICT["c"]["c"])
dec = Button(gui, text='.', fg='white', bg='blue', command=lambda: press("."), height=1, width=7)
dec.grid(row=GRID_DICT["."]["r"], column=GRID_DICT["."]["c"])
gui.mainloop()
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter