Sandrocottus
Sandrocottus

Reputation: 533

PySimpleGui: How to enter text in the text box?

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

Upvotes: 4

Views: 8914

Answers (2)

Rae
Rae

Reputation: 135

You can update elements directly by referencing them using their key on the window object:

eg as per your updates

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

Upvotes: 4

Sandrocottus
Sandrocottus

Reputation: 533

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

Upvotes: 2

Related Questions