首页 > 其他分享 >PySimpleGUI 计算器

PySimpleGUI 计算器

时间:2023-01-27 18:10:10浏览次数:41  
标签:button update PySimpleGUI window values 计算器 input event

import PySimpleGUI as sg


def button(text):
return sg.Button(text, pad=(2, 2), size=(4, 2), font=('黑体', 18), button_color='blue')


layout = [ # 定义布局,确定行数
[sg.Text('', key='-show-')],
[sg.InputText('', size=(12, 2), font=('黑体', 28), key='-input-')],
[button(i) for i in ['AC', '(', ')', '%']],
[button(i) for i in ['1', '2', '3', '+']],
[button(i) for i in ['4', '5', '6', '-']],
[button(i) for i in ['7', '8', '9', '*']],
[button(i) for i in ['0', '.', '=', '/']]
]
window = sg.Window('计算器', layout) # 创建窗口
while True: # 事件循环
event, values = window.read() # 读取窗口,返回事件和值
print(event, values)
if event is None: # 窗口关闭,程序中止运行
break
if event in list('1234567890+-().'):
window['-input-'].update(values['-input-'] + event)
if event == '*':
window['-input-'].update(values['-input-'] + '*')
if event == '/':
window['-input-'].update(values['-input-'] + '/')
if event == '%':
try:
window['-input-'].update(eval(values['-input-'] + '/100')) # eval:接收运行一个字符串表达式,返回表达式的结果值。
except:
window['-input-'].update('')
window['-show-'].update('ERROR')
if event == '=':
try:
window['-input-'].update(eval(values['-input-']))
except:
window['-input-'].update('')
window['-show-'].update('ERROR')
window['-input-'].update('')
if event == 'AC':
window['-input-'].update('')
window['-show-'].update('')

window.close() # 窗口关闭

 



标签:button,update,PySimpleGUI,window,values,计算器,input,event
From: https://blog.51cto.com/u_15483689/6024163

相关文章