import PySimpleGUI as sg
class Button():
def __init__(self, text, color=None, border=None):
self.text = text
self.color = color
self.border = border
def Create(self):
return sg.ReadFormButton(self.text, button_color=self.color, border_width=self.border)
layout = [[sg.Txt('' * 10)],
[sg.Text('', size=(15, 1), font=('Helvetica', 18), text_color='red', key='input')],
[sg.Txt('' * 10)],
[Button('(', ('black', 'orange')).Create(), Button(')', ('black', 'orange')).Create(),
Button('c', ('black', 'orange')).Create(), Button('«', ('black', 'orange')).Create()],
[Button('7', ('black', 'yellow')).Create(), Button('8', ('black', 'yellow')).Create(),
Button('9', ('black', 'yellow')).Create(), Button('÷', ('black', 'orange')).Create()],
[Button('4', ('black', 'yellow')).Create(), Button('5', ('black', 'yellow')).Create(),
Button('6', ('black', 'yellow')).Create(), Button('x', ('black', 'orange')).Create()],
[Button('1', ('black', 'yellow')).Create(), Button('2', ('black', 'yellow')).Create(),
Button('3', ('black', 'yellow')).Create(), Button('-', ('black', 'orange')).Create()],
[Button('.', ('black', 'orange')).Create(), Button('0', ('black', 'yellow')).Create(),
Button('=', ('black', 'orange')).Create(), Button('+', ('black', 'orange')).Create()],
]
form = sg.FlexForm('13411_CALCULATOR', default_button_element_size=(5, 2), auto_size_buttons=False, grab_anywhere=False)
form.Layout(layout)
Equal = ''
List_Op_Error = ['+', '-', '*', '/', '(']
while True:
button, value = form.Read()
if button is 'c':
Equal = ''
form.FindElement('input').Update(Equal)
elif button is '«':
Equal = Equal[:-1]
form.FindElement('input').Update(Equal)
elif len(Equal) == 16:
pass
elif str(button) in '1234567890+-().':
Equal += str(button)
form.FindElement('input').Update(Equal)
elif button is 'x':
Equal += '*'
form.FindElement('input').Update(Equal)
elif button is '÷':
Equal += '/'
form.FindElement('input').Update(Equal)
elif button is '=':
for i in List_Op_Error:
if '*' is Equal[0] or '/' is Equal[0] or ')' is Equal[0] or i is Equal[-1]:
Answer = "Error Operation"
break
elif Equal == '6001012630187':
Answer = 'Apisit.Khomcharoen'
break
elif '/0' in Equal or '*/' in Equal or '/*' in Equal:
Answer = "Error Operation"
break
elif '(' in Equal:
if ')' not in Equal:
Answer = "Error Operation"
break
elif '(' not in Equal:
if ')' in Equal:
Answer = "Error Operation"
break
else:
Answer = str("%0.2f" % (eval(Equal)))
if '.0' in Answer:
Answer = str(int(float(Answer)))
form.FindElement('input').Update(Answer)
Equal = Answer
elif button is 'Quit' or button is None:
break