[英]Validating Input Value - PySimpleGUI
I have simple GUI with two inputs and exit button.我有两个输入和退出按钮的简单 GUI。 Inputs have default_value = 0.00.
输入的 default_value = 0.00。 I want to validate user change in the way that input can be only float format,'.2f' and <= 5.0.
我想以输入只能是浮点格式,'.2f' 和 <= 5.0 的方式验证用户更改。 Example from cookbook helps partially.
食谱中的示例部分有所帮助。 When I use this code:
当我使用此代码时:
import PySimpleGUI as sg
layout = [[sg.Input(key='first_input', enable_events=True, default_text='0.00')],
[sg.Input(key='second_input', enable_events=True, default_text='0.00')],
[sg.Button('Exit')]]
window = sg.Window('Main', layout)
while True:
event, values = window.read()
if event in ['Exit', sg.WIN_CLOSED]:
break
if event == 'first_input' and values['first_input']:
try:
in_as_float = float(values['first_input'])
if float(values['first_input']) > 5:
window['first_input'].update('5')
except:
if len(values['first_input']) == 1 and values['first_input'][0] == '-':
continue
window['first_input'].update(values['first_input'][:-1])
window.close()
But when user delete content of "first_input" and decide to fill "second_input" the previous one remains empty.但是当用户删除“first_input”的内容并决定填充“second_input”时,前一个仍然为空。 How to prevent it and for example back to default value when user left input empty.
如何防止它,例如当用户将输入留空时恢复到默认值。
I have tried to do someting like:我试图做类似的事情:
if values['first_input'] == '':
window['firs_input'].update['0.00']
but this do not work because it will not let to user delete content.但这不起作用,因为它不会让用户删除内容。 For example when he want to change from 3 to 4. After he delete 3 0.00 apears immediately.
例如当他想从 3 变成 4 时。他删除 3 后 0.00 立即出现。
Try to validate value by regex, maybe you can test it meet all requirements.尝试通过正则表达式验证值,也许您可以测试它是否满足所有要求。
import re
import PySimpleGUI as sg
def select(element):
element.Widget.select_range(0, 'end')
element.Widget.icursor('end')
def validate(text):
result = re.match(regex, text)
return False if result is None or result.group() != text else True
regex = "^[+-]?([0-5](\.(\d{0,2}))?)?$"
old = {'IN1':'0.00', 'IN2':'0.00'}
validate_inputs = ('IN1', 'IN2')
layout = [
[sg.Input('0.00', enable_events=True, key='IN1')],
[sg.Input('0.00', enable_events=True, key='IN2')],
[sg.Button('Exit')],
]
window = sg.Window('Title', layout, finalize=True)
select(window['IN1'])
for key in validate_inputs:
window[key].bind('<FocusIn>', ' IN')
window[key].bind('<FocusOut>', ' OUT')
while True:
event, values = window.read()
if event in ['Exit', sg.WIN_CLOSED]:
break
elif event in validate_inputs:
element, text = window[event], values[event]
if validate(text):
try:
v = float(text)
if v > 5:
element.update(old[event])
continue
except ValueError as e:
pass
old[event] = text
else:
element.update(old[event])
elif event.endswith(' IN'):
key = event.split()[0]
element, text = window[key], values[key]
select(element)
elif event.endswith(' OUT'):
key = event.split()[0]
element, text = window[key], values[key]
try:
v = float(text)
element.update(f'{v:.2f}')
except ValueError as e:
element.update('0.00')
window.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.