Aug-08-2018, 02:08 PM
(This post was last modified: Aug-08-2018, 02:46 PM by PySimpleGUI.)
(Aug-08-2018, 11:12 AM)jfong Wrote: If values is a dictionary, you can write (suppose 'address' is a key):
print(values['address'])
I think I understood your point. I was trying to mitigate with existing capabilities.
How would you suggest that the 'address' dictionary entry be created? How does the user tell PySimpleGUI that the second field is 'address'?
Something like this?
g.InputText(key='address')That adds a bit more code to every form.
But, perhaps I could do a hybrid approach where BOTH types of return values are available. The "simplified" way and the more elegant way.
Every input field would have a new keyword, "key" that can be added if desired. That will enable that field to be addressed in the return values as a dictionary lookup. I'll add a new flag already to the "read form" calls so that the return values are either a list or a dictionary.
Thanks for the suggestions... keep 'em coming!
How does something like this look to you?
import PySimpleGUI as sg form = sg.FlexForm('Simple data entry form', use_dictionary=True) # begin with a blank form layout = [ [sg.Text('Please enter your Name, Address, Phone')], [sg.Text('Name', size=(15, 1)), sg.InputText('1', key='name')], [sg.Text('Address', size=(15, 1)), sg.InputText('2', key='address')], [sg.Text('Phone', size=(15, 1)), sg.InputText('3', key='phone')], [sg.Submit(), sg.Cancel()] ] button, values = form.LayoutAndRead(layout) sg.MsgBox(button, values['name'], values['address'], values['phone']) print(values)The code is pretty much done.
You set the indicator that you're working with dictionary return values when you create the form. Keys for each input field are set when the form is laid out as shown in the sample.
If it looks good, I'll check the code in shortly for you to try.
DONE!
Return values as a dictionary feature officially done and posted to GitHub. It'll be part of release 2.8 that I'll post to PyPI after everything has been tested more. Afraid of breaking something at the moment.
There is a new demo, Demo_Dictionary.py, that contains above code as a template.