Updating gui items withing the process

2024/10/14 1:18:17

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?

Answer

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 sglayout = [[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':breakif event == 'Read':window.Element('-LISTBOX-').update(values=['new value 1', 'new value 2', 'new value 3'])
window.close()
https://en.xdnf.cn/q/69469.html

Related Q&A

UnicodeDecodeError with Djangos request.FILES

I have the following code in the view call..def view(request):body = u"" for filename, f in request.FILES.items():body = body + Filename: + filename + \n + f.read() + \nOn some cases I getU…

bifurcation diagram with python

Im a beginner and I dont speak english very well so sorry about that. Id like to draw the bifurcation diagram of the sequence : x(n+1)=ux(n)(1-x(n)) with x(0)=0.7 and u between 0.7 and 4.I am supposed …

How do I use nordvpn servers as python requests proxies

Dont ask how, but I parsed the server endpoints of over 5000 nordvpn servers. They usually are something like ar15.nordvpn.com for example. Im trying to use nordvpn servers as request proxies. I know i…

Python Proxy Settings

I was using the wikipedia module in which you can get the information that is present about that topic on wikipedia. When I run the code it is unable to connect because of proxy. When I connected PC to…

Docker - Run Container from Inside Container

I have two applications:a Python console script that does a short(ish) task and exits a Flask "frontend" for starting the console app by passing it command line argumentsCurrently, the Flask …

Way to run Maven from Python script?

(I am using Windows.)I am trying to run maven from a python script. I have this:import subprocessmvn="C:\\_home\\apache-maven-2.2.1\\bin\\mvn.bat --version" p = subprocess.Popen(mvn, shell=Tr…

Why does testing `NaN == NaN` not work for dropping from a pandas dataFrame?

Please explain how NaNs are treated in pandas because the following logic seems "broken" to me, I tried various ways (shown below) to drop the empty values.My dataframe, which I load from a C…

Python dynamic import methods from file [duplicate]

This question already has answers here:How can I import a module dynamically given its name as string?(10 answers)How can I import a module dynamically given the full path?(37 answers)Closed last yea…

Python list does not shuffle in a loop

Im trying to create an randomized list of keys by iterating:import randomkeys = [1, 2, 3, 4, 5] random.shuffle(keys) print keysThis works perfect. However, if I put it in a loop and capture the output:…

Python extract max value from nested dictionary

I have a nested dictionary of the form:{2015-01-01: {time: 8, capacity: 5}, 2015-01-02: {time: 8, capacity: 7},2015-01-03: {time: 8, capacity: 8} etc}The dictionary is created from a csv file using dic…