PySimpleGui:如何在文本框中输入文本?

PySimpleGui: How to enter text in the text box?

我正在参考

上的教程学习 PySimpleGui

Link-1 and Link-2

我需要在布局中添加按钮以输入值,然后在相邻的文本框中显示该值

到目前为止,我已经能够创建按钮和文本框。

以下是我的代码:-

import PySimpleGUI as sg

layout = [[sg.Text('Enter Value:')],
          [sg.Input(do_not_clear=False), sg.Text('Value selected is:'), sg.Text(sg.InputText(""), key='_USERNAME_')],
          [sg.Button('Enter'), sg.Exit()],
          [sg.Text('List Of Values:')],
          [sg.Listbox(values=('value1', 'value2', 'value3'), size=(30, 2), key='_LISTBOX_')]]

window = sg.Window('My Application', layout)

while True:
    event, values = window.Read()
    print(event, values)
    if event is None or event == 'Exit':
        break
    if event == 'Enter':
        window.Element('_LISTBOX_').Update(values=[event, values, 'new value 3'])
        #window.Element('_USERNAME_').Update(values=[values]) #need to update the text box with value entered
window.Close()

但是,我无法在文本框中显示输入的值。 我在代码中添加了一条注释(现在给出错误),我需要用输入的值更新文本框。

请帮忙!

编辑:我能够在弹出窗口中显示值,但我需要在文本框中显示

我想通了,

以下代码符合我的目的:-

import PySimpleGUI as sg

layout = [[sg.Text('Enter Value:')],
          [sg.Input(do_not_clear=False), sg.T('Not Selected ', size=(52,1), justification='left',text_color='red', background_color='white', key='_USERNAME_')],
          [sg.Button('Enter'), sg.Exit()],
          [sg.Text('List Of Values:')],
          [sg.Listbox(values=('value1', 'value2', 'value3'), size=(30, 2), key='_LISTBOX_')]]

window = sg.Window('My Application', layout)

while True:
    event, values = window.Read()
    print(event, values)
    if event is None or event == 'Exit':
        break
    if event == 'Enter':
        window.Element('_LISTBOX_').Update(values=[event, values, 'new value 3'])
        window.FindElement('_USERNAME_').Update(values[0])
window.Close()

您可以直接更新元素,方法是使用 window 对象上的键引用它们:

例如根据您的更新

window['_LISTBOX_'].Update(values=[event, values, 'new value 3'])
window['_USERNAME_'].Update(values[0])