简体   繁体   English

在 PySimpleGui 的输入字段中更新名称

[英]Update name in inputfield in PySimpleGui

I am creating a template maker and it's working fine.我正在创建一个模板制作器,它工作正常。 My supervisor asked me to make a autofill button for her and a few other persons.我的主管要求我为她和其他几个人制作一个自动填充按钮。 If she want's to make a template, her name will be automaticly filled in at the inputfield just by clicking a button.如果她想制作一个模板,只需单击一个按钮,她的名字就会自动填写在输入字段中。

The code for the inputfield for the name looks like this:名称输入字段的代码如下所示:

[sg.Text("Name"), sg.InputText(key="NAME", do_not_clear=False)]

And the button for the template for 'Person-1' looks like this: 'Person-1' 模板的按钮如下所示:

[sg.Button('Template Person-1')]

The while loop looks like this: while 循环如下所示:

 while True:
        event, values = spv_window.read()
        if event == sg.WIN_CLOSED or event == "Exit":
            break
        elif event == 'Template Person-1':
            spv_window['NAME'].Update(values['NAME'], 'Person-1')
        elif event == "Maak template":
            doc.render(values)
            output_path = Path(values["-IN-"]) / f"{values['NAME']}-{values['TOPIC']}.docx"
            doc.save(output_path)
            sg.popup("Template gegenereerd", f"zie: {output_path}")

Can someone explain to me what's going wrong?有人可以向我解释发生了什么问题吗? Thank you for your time!感谢您的时间!

User Settings to remember previously entered values用户设置以记住以前输入的值

Another approach to having an "Auto-fill" button is to fill the value with whatever the last entered value is.使用“自动填充”按钮的另一种方法是使用最后输入的值填充值。 This will basically customize the GUI for each person with you needing to make a special feature or save a special set of settings for each person.这将基本上为每个人定制 GUI,您需要为每个人制作特殊功能或保存一组特殊设置。

In this example, the "Name" field defaults to the last entered value.在此示例中,“名称”字段默认为最后输入的值。

import PySimpleGUI as sg

layout = [  [sg.Text('My Form')],
            [sg.Text('Name:'), sg.Input(default_text=sg.user_settings_get_entry('-name-', ''), key='-NAME-')],
            [sg.Button('Go'), sg.Button('Exit')]  ]

window = sg.Window('Window Title', layout)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    if event == 'Go':
        # Save the Name entered and use it next time to auto-fill
        sg.user_settings_set_entry('-name-', values['-NAME-'])

window.close()

在此处输入图像描述

If it's a field that the person is unlikely to change, then it'll keep being set to the same value every time the program is executed.如果这是一个人不太可能更改的字段,那么每次执行程序时都会将其设置为相同的值。

Another approach is to make templates.另一种方法是制作模板。 If you do, then the PySimpleGUI User Settings API can still be handy for implementing this feature.如果你这样做了,那么 PySimpleGUI 用户设置 API 仍然可以方便地实现此功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM