Recall this is our basic design pattern.
values is the "Values Dictionary" is the second item returned from calling
window.read(). This is how you'll get all of the current values of the elements that are in your window. Most GUI frameworks require you to ask for each widget's value using a "get" method. PySimpleGUI simplified this process by giving you all of the values in a dictionary so that you can use the dictionary instead of making a call to get each element's value.
The Dictionary Keys
The element's keys that you specified when you created your layout are the keys that will be present in the values dictionary. If you've never used a dictionary in Python, now is an excellent time to give them a try!
If I have a layout with this definition:
If I were to type these values into the
and click the "Go" button, then the values dictionary will look like this:
Each of the
Input elements has an entry in the values dictionary, using a key that matches the key defined in the layout.
To retrieve the "value" of the first
Input element, then I would write:
Key Format Coding Convention
The combination of these 2 coding conventions makes the task of finding the use of the values dictionary very easy to find in any PySimpleGUI program.
If you fail to add a key to an element that is returned in the values dictionary, then an auto-numbered integer key will be used. These auto-numbered keys are created when you make your window. It's best to not rely on these numerical keys. If you are going to reference an element's key, such as looking up an item in the values dictionary, then be explicit by adding a key to your element. If you add new elements without keys ahead of ones you previously were using, then their keys will be different due to the new elements changing the numbering.
For example, this window...
will print this line of text on the console when the "Go" button is pressed:
Because no keys were explicitly defined in the layout, auto-numbered keys were added and can been in the values dictionary that was printed.
The values in the dictionary will vary depending on the element being represented. Each element's value dictionary entry format is described in the description of the Elements Section
The most common data types you'll see for entries in the values dictionary are::
- Any - the type provided in the layout is returned
String and list are easily understood. For example, for an
Input element, you'll always get a string in the values dictionary. A
Listbox has a list of items chosen in the values dictionary.
Spin element is an example of one capable of returning "Any" type. If you provided a float value of 3.14 as one of the items that can be chosen, then that exact item will be returned in the values dictionary. It will appear as a string in the window, but the returned value will be a float if you specified a float. If you provided an object, then that object will be returned, not a string representation of the object (which is what's shown in the window)
You may want to validate your inputs prior to using them or adding protection so that your code doesn't crash if a user inputs data that would generate an error.
This little calculation program works great...
import PySimpleGUI as sg
layout = [ [sg.Input(size=4, key='-INPUT 1-'), sg.Text(' + '), sg.Input(size=4, key='-INPUT 2-'), sg.Text('='), sg.Text(key='-OUT-')],
window = sg.Window('Values Dictionary', layout)
event, values = window.read()
if event == sg.WIN_CLOSED or event == 'Exit':
if event == 'Add':
total = int(values['-INPUT 1-']) + int(values['-INPUT 2-'])
as long as your user enters integers.
But typing the letter 'a' into the first
Input element generates this error when clicking the "Add" button:
try statement is a quick and dirty way to get pas this crash: