Web Analytics Made Easy - Statcounter
Skip to content

Values Dictionary

Recall this is our basic design pattern.

event, values = window.read()

The variable 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:

layout = [  [sg.Text('Auto-Numbered Keys')],
            [sg.Input(key='-INPUT 1-')],
            [sg.Input(key='-INPUT 2-')],
            [sg.Button('Go')]  ]

If I were to type these values into the Input elements:"

image

and click the "Go" button, then the values dictionary will look like this:

{'-INPUT 1-': 'First input', '-INPUT 2-': 'Second input'}

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:

values['-INPUT 1-']

Key Format Coding Convention

Recall in the section on Coding Conventions that the variable name recommended for the values dictionary is values. Another coding convention mentioned is the 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.

values['-INPUT 1-']

Auto-Numbered Keys

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...

import PySimpleGUI as sg

layout = [  [sg.Text('Auto-Numbered Keys')],
            [sg.Input()],
            [sg.Input()],
            [sg.Button('Go')]  ]

window = sg.Window('Auton-Numbered Keys', layout)

while True:
    event, values = window.read()
    print(event, values)
    if event == sg.WIN_CLOSED or event == 'Exit':
        break

window.close()

image

will print this line of text on the console when the "Go" button is pressed:

Go {0: 'first input element', 1: 'second input element'}

Because no keys were explicitly defined in the layout, auto-numbered keys were added and can been in the values dictionary that was printed.

Dictionary Entries

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::

  • string
  • list
  • 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.

The 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)

Input Validation

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-')],
           [sg.Button('Add')]  ]

window = sg.Window('Values Dictionary', layout)

while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Exit':
        break
    if event == 'Add':
        total = int(values['-INPUT 1-']) + int(values['-INPUT 2-'])
        window['-OUT-'].update(total)
window.close()

as long as your user enters integers.

image

But typing the letter 'a' into the first Input element generates this error when clicking the "Add" button:

Traceback (most recent call last):
  File "C:\Users\mike\AppData\Roaming\JetBrains\PyCharm2020.2\scratches\scratch_679.py", line 13, in <module>
    total = int(values['-INPUT 1-']) + int(values['-INPUT 2-'])
ValueError: invalid literal for int() with base 10: 'a'

Adding a try statement is a quick and dirty way to get pas this crash:

    if event == 'Add':
        try:
            total = int(values['-INPUT 1-']) + int(values['-INPUT 2-'])
            window['-OUT-'].update(total)
        except:
            pass        # could clear the fields here or display a message