Skip to content

Instantly share code, notes, and snippets.

@cedrickvstheworld
Last active June 2, 2018 03:12
Show Gist options
  • Save cedrickvstheworld/3594af63e9ae430b26f8bad2579aa5ed to your computer and use it in GitHub Desktop.
Save cedrickvstheworld/3594af63e9ae430b26f8bad2579aa5ed to your computer and use it in GitHub Desktop.
from tkinter import *
from tkinter import ttk
from math import *
calc = Tk()
calc.title('Calculator 1.0')
calc.resizable(0, 0)
# functions
def clearall():
screen.configure(text='')
def keyfunc(x):
text = screen.cget('text')
if text.find('answer') != -1 or text.find('SYNTAX ERROR') != -1:
clearall()
screen.configure(text=screen.cget('text') + x)
def clearscreen():
screen.configure(text='')
def backspace():
text = screen.cget('text')
if text.find('answer') != -1 or text.find('SYNTAX ERROR') != -1:
clearall()
else:
screen.configure(text=screen.cget('text')[:-1])
def operators(x):
text = screen.cget('text')
if text.find('answer') != -1 or text.find('SYNTAX ERROR') != -1:
clearall()
screen.configure(text=screen.cget('text') + x)
else:
textx = screen.cget('text')[-3:]
if textx == ' - ' or textx == ' + ' or textx == ' / ' or textx == ' * ':
text = screen.cget('text')[:-3]
screen.configure(text=text + x)
else:
screen.configure(text=screen.cget('text') + x)
def pointkey():
text = screen.cget('text')
if text.find('answer') != -1 or text.find('SYNTAX ERROR') != -1:
clearall()
screen.configure(text=screen.cget('text') + '.')
else:
textx = screen.cget('text')[-1:]
if textx == '.':
text = screen.cget('text')[:-1]
screen.configure(text=text + '.')
else:
screen.configure(text=screen.cget('text') + '.')
def cleaninput(x):
while x[0] == '0':
x = x[1:]
while x.find(' 0') != -1\
or x.find('(0') != -1\
or x.find(')0') != -1\
or x.find('. ') != -1\
or x.find('1(') != -1\
or x.find('2(') != -1\
or x.find('3(') != -1\
or x.find('4(') != -1\
or x.find('5(') != -1\
or x.find('6(') != -1\
or x.find('7(') != -1\
or x.find('8(') != -1\
or x.find('9(') != -1\
or x.find('0(') != -1\
or x.find('(sqrt') != -1:
x = x.replace(' 0', ' ')
x = x.replace('(0', '(')
x = x.replace(')0', ')')
x = x.replace('. ', ' ')
x = x.replace('1(', '1 * (')
x = x.replace('2(', '2 * (')
x = x.replace('3(', '3 * (')
x = x.replace('4(', '4 * (')
x = x.replace('5(', '5 * (')
x = x.replace('6(', '6 * (')
x = x.replace('7(', '7 * (')
x = x.replace('8(', '8 * (')
x = x.replace('9(', '9 * (')
x = x.replace('0(', '0 * (')
x = x.replace('(sqrt', '(1 * sqrt')
return x
def equalkey():
text = screen.cget('text')
if text.find('answer') != -1 or text.find('SYNTAX ERROR') != -1:
clearall()
else:
if screen.cget('text') == '':
screen.configure(text='')
else:
try:
text = cleaninput(screen.cget('text'))
if text[0] == '0':
textv = text[1:]
else:
textv = cleaninput(screen.cget('text'))
answer = eval(textv)
screen.configure(text=screen.cget('text') + '\n\nanswer = ' + str(answer))
except(Exception):
screen.configure(text='SYNTAX ERROR')
# screenframe
topframe = ttk.Labelframe(calc, text='Screen', width=250, height=80)
topframe.grid(column=0, row=0, columnspan=3, padx=10, pady=10)
screen = Label(topframe, text='', anchor='nw', justify='left')
screen.grid(column=0, row=0)
screen.config(width=30, height=4)
# body frame
mainframe = ttk.Frame(calc, width=250, height=80)
mainframe.grid(column=0, row=1, columnspan=3, padx=10, pady=10)
# buttons
nums = range(10)
calignment = {9: 0, 8: 1, 7: 2, 6: 0, 5: 1, 4: 2, 3: 0, 2: 1, 1: 2, 0: 0}
ralignment = {9: 0, 8: 0, 7: 0, 6: 1, 5: 1, 4: 1, 3: 2, 2: 2, 1: 2, 0: 3}
for button in range(10):
option = nums[button]
buttonx = ttk.Button(mainframe, text=nums[button], width=10, command=lambda opt=option: keyfunc(str(opt)))
buttonx.grid(column=calignment[button], row=ralignment[button])
sqrtb = ttk. Button(mainframe, text='sqrt', width=10, command=lambda: keyfunc('sqrt('))
sqrtb.grid(column=1, row=3)
equals = ttk. Button(mainframe, text='=', width=10, command=equalkey)
equals.grid(column=2, row=3)
add = ttk. Button(mainframe, text='+', width=10, command=lambda: operators(' + '))
add.grid(column=0, row=4)
subtract = ttk. Button(mainframe, text='-', width=10, command=lambda: operators(' - '))
subtract.grid(column=1, row=4)
multiply = ttk. Button(mainframe, text='*', width=10, command=lambda: operators(' * '))
multiply.grid(column=2, row=4)
divide = ttk. Button(mainframe, text='/', width=10, command=lambda: operators(' / '))
divide.grid(column=0, row=5)
openp = ttk. Button(mainframe, text='(', width=10, command=lambda: keyfunc('('))
openp.grid(column=1, row=5)
closep = ttk. Button(mainframe, text=')', width=10, command=lambda: keyfunc(')'))
closep.grid(column=2, row=5)
bspace = ttk. Button(mainframe, text='backspace', width=10, command=backspace)
bspace.grid(column=0, row=6)
clear = ttk. Button(mainframe, text='clear', width=10, command=clearscreen)
clear.grid(column=1, row=6)
point = ttk. Button(mainframe, text='.', width=10, command=pointkey)
point.grid(column=2, row=6)
for child in mainframe.winfo_children():
child.grid_configure(padx=2, pady=2)
calc.mainloop()
@ariesabao
Copy link

Python rocks \m/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment