How to print selected Table Element within the same window?

Viewed 50

I am working on a simple executable which allows me to select a table element.

Currently, when i select a table element, it produces a popup notifying that i have selected the particular element

However, i wish to print the element out in the large empty space next to the table each time i select an element, however the solutions i found didn't exactly fit what i have in mind. Is there a solution to my predicament?

import PySimpleGUI as sg

choices = ('Windows Enterprise 10','Windows Server 19','MacOS','Ubuntu','Debian')

layout = [  [sg.Text('Pick an OS')],
            [sg.Listbox(choices, size=(20, 5), key='-OS-', enable_events=True)] ]

window = sg.Window('Pick an OS', layout,size=(500,200))

while True:                  # the event loop
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    if values['-OS-']:    # if something is highlighted in the list
        sg.popup(f"The OS you selected is: {values['-OS-'][0]}")
window.close()
2 Answers

Put another element to show the result beside your sg.Listbox, then update that element with value of sg.Listbox. Element can be sg.Text which need to split message to lines by yourself, or sg.Multiline.

import PySimpleGUI as sg

choices = (
    'Windows Enterprise 10',
    'Windows Server 19',
    'MacOS',
    'Ubuntu',
    'Debian',
)

sg.theme('DarkBlue3')
sg.set_options(font=("Courier New", 12))

layout = [
    [sg.Text('Pick an OS')],
    [sg.Listbox(choices, size=(22, 5), key='-OS-', enable_events=True),
     sg.Text('', size=(40, 2), font=("Courier New", 12, 'bold'), key='-OUTPUT-')],
]

window = sg.Window('Pick an OS', layout)

while True:                  # the event loop
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    elif event == '-OS-':
        text = f"You have selected: {values['-OS-'][0]}\nOperating System"
        window['-OUTPUT-'].update(value=text)

window.close()
import PySimpleGUI as sg

choices = ('Windows Enterprise 10','Windows Server 19','MacOS','Ubuntu','Debian')
layout = [[sg.Text('Pick an OS')],
            [sg.Listbox(choices, size=(20, 5), key='-OS-', enable_events=True), 
            sg.Text(key='OUT', size=(30,5), text_color='#000000',background_color='#ffffff')]]

window = sg.Window('Pick an OS', layout,size=(500,200))

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED:
        break
    else:
        choice = values['-OS-']
        window['OUT'].update(f'You have selected:\n{choice}')
window.close()
Related