import PySimpleGUI as sg
import math
def calculate(input_str):
try:
# 如果输入字符串包含x^2,则计算平方
if 'x^2' in input_str:
input_str = input_str.replace('x^2', '**2')
# 如果输入字符串包含%,则计算百分比
if '%' in input_str:
input_str = input_str.replace('%', '/100')
result = eval(input_str)
return result
except Exception as e:
return "Error"
layout = [
[sg.Text('', key='text', size=(36,10), font=('宋体',15), background_color='white', text_color='blue', relief='ridge', border_width=10)],
[sg.Button('%',size=(10,2),key='%'),sg.Button('CE',size=(10,2),key="CE"),sg.Button('C',size=(10,2),key='C',button_color="red"),sg.Button('return',size=(10,2),key="return")],
[sg.Button('1/x',size=(10,2),key="1/x"),sg.Button('x^2',size=(10,2),key="x^2"),sg.Button('ASR',size=(10,2),key="ASR"),sg.Button('/',size=(10,2),key="/")],
[sg.Button('7',size=(10,2),key="7"),sg.Button('8',size=(10,2),key="8"),sg.Button('9',size=(10,2),key="9"),sg.Button('*',size=(10,2),key="*")],
[sg.Button('4',size=(10,2),key="4"),sg.Button('5',size=(10,2),key="5"),sg.Button('6',size=(10,2),key="6"),sg.Button('-',size=(10,2),key="-")],
[sg.Button('1',size=(10,2),key="1"),sg.Button('2',size=(10,2),key="2"),sg.Button('3',size=(10,2),key="3"),sg.Button('+',size=(10,2),key="+")],
[sg.Button('+/-',size=(10,2),key="+/-"),sg.Button('0',size=(10,2),key="0"),sg.Button('.',size=(10,2),key="."),sg.Button('=',size=(10,2),key="=",button_color='blue')]
]
window = sg.Window('Pseudo calculator',layout)
# 初始化输入字符串和历史记录
input_str = ''
history = []
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Exit':
break
# 处理数字按钮
if event in ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']:
input_str += event
window['text'].update(input_str)
# 处理运算符
elif event in ['+', '-', '*', '/']:
if input_str: # 避免在没有输入时直接输入运算符
input_str += ' ' + event + ' '
window['text'].update(input_str)
# 处理等于号
elif event == '=':
try:
# 计算并显示结果
result = calculate(input_str)
history.append(f"{input_str} = {result}")
window['text'].update(result)
input_str = str(result) # 更新输入为结果,以便继续计算
except Exception as e:
window['text'].update("Error")
# 处理清除输入
elif event == 'C':
input_str = ''
window['text'].update(input_str)
# 处理全部清除(CE)
elif event == 'CE':
if input_str:
input_str = input_str[:-1] # 删除最后一个字符
window['text'].update(input_str)
# 特殊功能
elif event == '%': # 计算百分比
try:
current_num = float(input_str)
result = current_num / 100
input_str = str(result)
window['text'].update(input_str)
except ValueError:
window['text'].update("Error")
elif event == 'x^2': # 平方
try:
num = float(input_str)
result = num ** 2
input_str = str(result)
window['text'].update(input_str)
except ValueError:
window['text'].update("Error")
elif event == '1/x': # 倒数
try:
num = float(input_str)
if num != 0:
result = 1 / num
input_str = str(result)
window['text'].update(input_str)
else:
window['text'].update("Error: Division by zero")
except ValueError:
window['text'].update("Error")
# 注意:'ASR' 功能未明确,通常可能指'求根'或'高级设置'等,这里假设为平方根
elif event == 'ASR': # 平方根
try:
num = float(input_str)
if num >= 0:
result = math.sqrt(num)
input_str = str(result)
window['text'].update(input_str)
else:
window['text'].update("Error: Negative number for square root")
except ValueError:
window['text'].update("Error")
elif event == '+/-': # 正负号转换
if input_str and input_str.replace('.', '', 1).isdigit():
input_str = '-' + input_str if not input_str.startswith('-') else input_str[1:]
window['text'].update(input_str)
elif event == '.':
if '.' not in input_str or input_str[-1] != '.': # 确保不会连续添加小数点
input_str += event
window['text'].update(input_str)
window.close()
涉及到了客户端页面制作