如何根据需要设置输入文本pysimplegui

how to set a input text as required pysimplegui

我有一个布局和一个输入文本,我希望输入文本不要留空 我搜索了所有 PySimpleGUI 文档,但没有看到如何根据需要设置输入文本

这是我的代码

layout = [[sg.Text('enter your license code')],
              [sg.InputText()], # I wan't this input to be required
              [sg.Submit('OK'), sg.Cancel('Cancel')]]
    window = sg.Window('invalid License', layout, icon="logo.ico")
    while True:
        event, values = window.read()
        if event == 'Cancel' or event == sg.WIN_CLOSED:
            break  # exit button clicked
    window.close()
    license_input = values[0]
    read_configs('license.txt')
    lic = "license.txt"
    with open(lic, 'r+') as f:
        text = f.read()
        text = re.sub(license_code, license_input, text)
        f.seek(0)
        f.write(text)
        f.truncate()

弹出窗口需要 license_code window

  • 'Cancel' 按钮不需要并已删除
  • 'Close' 的 window 按钮被 enable_close_attempted_event=Truesg.WINDOW_CLOSE_ATTEMPTED_EVENT 禁用未处理,否则当 'Close' 按钮时它将首先销毁 window点击了。
  • 通过方法get读取和return输入元素的当前值,或者使用values[element]如果不是window事件的'Close'按钮.
  • 仅当输入元素的值不是空字符串时才中断事件循环。
import PySimpleGUI as sg

layout = [
    [sg.Text('enter your license code')],
    [sg.InputText(key='-INPUT-')],
    [sg.Submit('OK')],
]
window = sg.Window('invalid License',
    layout,
    #icon="logo.ico",
    enable_close_attempted_event=True)

while True:
    event, values = window.read()
    print(event, values)
    license_code = window['-INPUT-'].get().strip()
    if event == 'OK' and license_code:
        break

window.close()
print(license_code)