python简单计算器

附赠源码:

import PySimpleGUI as sg
import re

listsNumber = [
    ['AC', '(', ')', '%'],
    [1, 2, 3, '+'],
    [4, 5, 6, '-'],
    [7, 8, 9, 'x'],
    [0, '.', '=', '÷'],
]

layout = [
    [sg.Text(font=('黑体', '10'), key='-msg-')],
    [sg.Multiline(
        '',
        key='-show-',
        s=(22, 10),
        font=('黑体', 14),
        border_width=1,
        expand_x=True,
        expand_y=True,
        pad=((0, 0), (5, 8)),
        background_color='black',
        text_color='white',
        no_scrollbar=True
    )],
    [[sg.Button(
        v,
        button_color='black' if v != 'AC' else 'red',
        pad=((2, 2), (2, 2)),
        size=(2, 1.5),
        font=('黑体', '12')

    ) for v in values] for values in listsNumber],
    [sg.Text(font=('黑体', '9'), key='-disassemble-')]
]

window = sg.Window(
    'http://www.seedtree.cn simpleGUI',
    layout,
    font=('宋体', 14)
)

hideVale = []  # 输入队列
errMsg = '输入错误,请重新输入!Input error, please re-enter'

def checkInput(strVal):
    if strVal in ['(', ')', '%', '+', '-', 'x', '÷', '.']:
        return True
    else:
        return False


# 是否需要清楚计算输入框内容
clearInputShow = False

while True:
    event, values = window.read()
    print(event)
    window['-msg-'].update('')
    print(clearInputShow)
    if clearInputShow and event is not None:
        window['-show-'].update(value='')
        clearInputShow = False
    # 符合重复输入
    if len(hideVale) > 0 and checkInput(hideVale[-1]) and checkInput(event):
        if checkInput(hideVale[-1]) and event == '(':
            print('#(#')
        elif hideVale[-1] == ')':
            print('#)#')
        else:
            continue
    if event == 'AC':
        window['-show-'].update("")
        hideVale = []
    if event in list('0123456789()+-.'):
        if len(hideVale) <= 0:
            hideVale.append(event)
        else:
            pattern = r"^[+-]?\d+(\.\d+)?$"
            if event in list('0123456789.') and re.match(pattern, hideVale[-1]):
                hideVale[-1] = '{}{}'.format(hideVale[-1], event)
            else:
                hideVale.append(event)
        window['-show-'].update(window['-show-'].get() + event)
    if event == 'x':
        window['-show-'].update(window['-show-'].get() + 'x')
        hideVale.append('*')
    if event == '÷':
        window['-show-'].update(window['-show-'].get() + '÷')
        hideVale.append('/')
    if event == '%':
        try:
            lastVal = hideVale[-1]
            window['-show-'].update(window['-show-'].get() + '%')
            del hideVale[-1]
            hideVale.append('({}/100)'.format(lastVal))
        except:
            window['-msg-'].update(errMsg, text_color='red')
            window['-show-'].update('')
            hideVale = []
    if event == '=':
        clearInputShow = True
        try:
            v = ''
            for i in hideVale:
                v += i
            window['-disassemble-'].update(v)
            window['-show-'].update(value="{} = {}".format(window['-show-'].get(), eval(v)))
        except:
            window['-msg-'].update(errMsg, text_color='red')
            window['-show-'].update('')
        finally:
            clearInputShow = True
            hideVale = []
    print(hideVale)
    if event is None:
        break

window.close()

你可能感兴趣的:(工具,python,开发语言,计算器)