I am trying to make a GUI for my app and ran into a problem:
using PySimpleGUI
I have to define layout at first and only then display the whole window. Right now the code is like this:
import PySimpleGUI as sg
layout = [[sg.Text('Input:')],
[sg.Input(do_not_clear=False)],
[sg.Button('Read'), sg.Exit()],
[sg.Text('Alternatives:')],
[sg.Listbox(values=('value1', 'value2', 'value3'), size=(30, 2))]]
window = sg.Window('Alternative items', layout)
while True:
event, values = window.Read()
if event is None or event == 'Exit':
break
print(values[0])
window.Close()
Is it possible to only show the Listbox
after the Read
button is pushed? because I would only get values for Listbox
after my input. Maybe it somehow possible to update the listbox with new values after button event?
It indeed is possible to update the listbox with new values after a button event. I only had to add a couple lines to your code to get this.
Anytime you wish to change values of Elements in an existing window, you will do so using the Element's update
method. Take a look at the package docs http://www.PySimpleGUI.org under the section on Updating Elements.
Hiding Elements is possible, but not recommended. Instead, create a new window and close the old one. There are a number of Demo Programs on the GitHub that show you how to do multiple windows.
import PySimpleGUI as sg
layout = [[sg.Text('Input:')],
[sg.Input(do_not_clear=False)],
[sg.Button('Read'), sg.Exit()],
[sg.Text('Alternatives:')],
[sg.Listbox(values=('value1', 'value2', 'value3'), size=(30, 2), key='_LISTBOX_')]]
window = sg.Window('Alternative items', layout)
while True:
event, values = window.read()
print(event, values)
if event == sg.WIN_CLOSED or event == 'Exit':
break
if event == 'Read':
window.Element('-LISTBOX-').update(values=['new value 1', 'new value 2', 'new value 3'])
window.close()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With