Pysimplegui: sg.In() , sg.FilesBrowse() reads Browse button as input value

Created on 6 Jun 2019  路  6Comments  路  Source: PySimpleGUI/PySimpleGUI

After updating to latest version seems the code for taking file inputs doesn't work anymore

event, (filename,) = sg.Window('Get filename example'). Layout([[sg.Text('Filename')], [sg.Input(), sg.FileBrowse()], [sg.OK(), sg.Cancel()] ]).Read()

It produces ValueError: too many values to unpack (expected 1).
Upon investigating a bit it seems that in 3.39.0 the Browse button originating from FileBrowse() is read as an input. Downgrading to 3.29.0 fixed the issue. Tested with FileInput() as well.

documentation

Most helpful comment

The old version returns value as a list. The new version returns value as a dictionary. This is what I tried.

Textbox value = c:\files\a.txt

A. psg v3.28

e, v = window.Read()
print(v)
['c:/files/a.txt']

B. psg v3.37

e, v = window.Read()
print(v)
{0: 'c:/files/a.txt', 'Browse': 'c:/files/a.txt'}

The following would cause a ValueError in the new version as indicated by @DKatarakis
e, (v,) = window.Read()

All 6 comments

Everything is working fine. Just don't write ALL-IN-ONE-LINE code.

Now.

It produces ValueError: too many values to unpack (expected 1).
It's not a PySimpleGUI problem, @DKatarakis.

Here is your code in normal form:

import PySimpleGUI as sg

layout = [
    [sg.Text('Filename')],
    [sg.Input(), sg.FileBrowse()],
    [sg.OK(), sg.Cancel()]
]


window = sg.Window('Simple App, Really simple.', layout)

while True:
    event, values = window.Read()
    if event is None or event == 'Exit': break

    print(event, values)

    if event == 'Button1':
        pass
    if event == 'Button2':
        pass
    if event == 'Button3':
        pass


window.Close()

The old version returns value as a list. The new version returns value as a dictionary. This is what I tried.

Textbox value = c:\files\a.txt

A. psg v3.28

e, v = window.Read()
print(v)
['c:/files/a.txt']

B. psg v3.37

e, v = window.Read()
print(v)
{0: 'c:/files/a.txt', 'Browse': 'c:/files/a.txt'}

The following would cause a ValueError in the new version as indicated by @DKatarakis
e, (v,) = window.Read()

The problem is that Buttons started to return values too. This created an extra entry and because it's a button it switched the return values over to a dictionary.

If you reference v[0] it will give you your value (same as a list), but it's held in a dictionary.

I'm puzzled why the button is returning a value now. Lemme read the release notes and see if I can determine what the purpose was. I have a feeling it's due to something with the calendar widget or other specialty button.

The old version returns value as a list. The new version returns value as a dictionary. This is what I tried.

Textbox value = c:\files\a.txt

A. psg v3.28

e, v = window.Read()
print(v)
['c:/files/a.txt']

B. psg v3.37

e, v = window.Read()
print(v)
{0: 'c:/files/a.txt', 'Browse': 'c:/files/a.txt'}

The following would cause a ValueError in the new version as indicated by @DKatarakis
e, (v,) = window.Read()

Yes indeed. I apologize for not specifying properly. Thanks for your input.

Upon further examination, there was a change a while back to Button return values across all 4 ports. This was a fundamental change to buttons. They now have return values. This poses a real problem for applications that attempt to "unpack" the results as it's not a list.

I'll discourage direct unpacking in the Documentation. You can access the return values as if the values were a list using [], you just can't unpack them directly.

Sorry about this!

_Breaking existing code upon upgrading is a cardinal sin to me so I'm really very sorry._ You'll need to modify your code access the returned values in 2 steps instead of 1.

Your 1-line program became 2 I'm afraid

event, values = sg.Window('Get filename example'). Layout([[sg.Text('Filename')], [sg.Input(), sg.FileBrowse()], [sg.OK(), sg.Cancel()] ]).Read()
filename = values[0]
Was this page helpful?
0 / 5 - 0 ratings

Related issues

MikeTheWatchGuy picture MikeTheWatchGuy  路  6Comments

thelou1s picture thelou1s  路  4Comments

mozesa picture mozesa  路  4Comments

lucasea777 picture lucasea777  路  3Comments

yogesh-aggarwal picture yogesh-aggarwal  路  3Comments