【发布时间】:2021-11-01 12:16:32
【问题描述】:
我有一个简单的 GUI,有两个输入和退出按钮。输入的 default_value = 0.00。我想以输入只能是浮点格式、'.2f' 和
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()
但是当用户删除“first_input”的内容并决定填充“second_input”时,前一个仍然为空。如何防止它,例如当用户将输入留空时恢复默认值。
我尝试过做类似的事情:
if values['first_input'] == '':
window['firs_input'].update['0.00']
但这不起作用,因为它不会让用户删除内容。例如当他想从 3 更改为 4 时,他立即删除 3 个 0.00 后。
【问题讨论】:
-
请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
标签: python pysimplegui