PySimpleGui: How to enter text in the text box?

孤人 提交于 2021-01-28 23:21:27

问题


I am learning PySimpleGui by referring the tutorials at

Link-1 and Link-2

I need to add buttons to my layout to enter a value, then display the value in adjoining textbox

So far, i have been able to create the buttons and the textboxes.

Following is my code:-

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()

However, i am not able to display the entered value in the text box. I have added a comment in the code (which gives an error for now) where i need to update the text box with entered value.

Please help!

Edit: I was able to display the value in a popup, but i need to display in the text box


回答1:


I figured it out,

Following code serves my purpose:-

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()


来源:https://stackoverflow.com/questions/60808158/pysimplegui-how-to-enter-text-in-the-text-box

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!