WhatTheClown
WhatTheClown

Reputation: 486

Is there a way to get inputs using their keys in Python's PySimpleGUI?

I would like to know how to get a value (Text Input Box) using it's key but I don't really know how.

I think you can use values['key_name_here'] but I am not sure. I have tried that but it didn't seem to work. (At least I tried it on a text label)

I also need to get the text from a label but so far I can't see how to do that.

Upvotes: 2

Views: 13562

Answers (1)

Alan
Alan

Reputation: 3042

If you go back to the basic introduction to PySimpleGUI (Jumpstart), this tells you how to get the Text Input Box value.

import PySimpleGUI as sg

sg.theme('DarkAmber')   # Add a touch of color
# All the stuff inside your window.
layout = [  [sg.Text('Some text on Row 1')],
            [sg.Text('Enter something on Row 2'), sg.InputText()],
            [sg.Button('Ok'), sg.Button('Cancel')] ]

# Create the Window
window = sg.Window('Window Title', layout)
# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel
        break
    print('You entered ', values[0])

window.close()

You can see from the initialisation the line window = sg.Window('Window Title', layout) passes a list (layout) to the variable "window".

In the event loop, the state of Text Input Box is handled by event, values = window.read(), passing the text to "values". Values is a list, not a dictionary, which is why you don't access it by values['Inp1']; you access it by the list index values[0] (presumably if there was more than one InputBox, the next one would be values[1]).

Alternately, once the event loop is started, you can get the InputBox value directly using .get(): print(f'Label 1 is {layout[1][1].get()}')

Getting the text of a label is a bit obscure. The answer is to use .DisplayText: print(f'Label 1 is {layout[0][0].DisplayText}')

An example using direct access:

import PySimpleGUI as sg

sg.theme('DarkAmber')   # Add a touch of color
# All the stuff inside your window.
layout = [  [sg.Text('Some text on Row 1')],
            [sg.Text('Enter something on Row 2'), sg.InputText()],
            [sg.Button('Ok'), sg.Button('Cancel')] ]

# Create the Window
window = sg.Window('Window Title', layout)
# Event Loop to process "events" and get the "values" of the inputs
while True:
    event, values = window.read()
    if event == sg.WIN_CLOSED or event == 'Cancel': # if user closes window or clicks cancel
        break
    print('You entered ', values[0])
    # This is a directly accessed Label
    print(f'Label 1 is {layout[0][0].DisplayText}')
    # This is directly accessed InputBox
    print(f'Label 1 is {layout[1][1].get()}')
    # Note that layout is a list of lists

window.close()

There are different ways in Python to build dynamic strings. One of the latest, and my personal choice usually, is f-strings (Formatted String Literals). These are marked by putting an 'f' in front of the string f'Label 1 is {layout[1][1].get()}'.

The order of widgets is defined by the layout. Each row is a list of widgets, and then all the lists are added to the layout container. This means the first row of widgets is layout[0]. The first widget on the first row is layout[0][0].

Upvotes: 5

Related Questions