Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sg.In() , sg.FilesBrowse() reads Browse button as input value #1527

Closed
DKatarakis opened this issue Jun 6, 2019 · 6 comments
Closed

sg.In() , sg.FilesBrowse() reads Browse button as input value #1527

DKatarakis opened this issue Jun 6, 2019 · 6 comments
Labels
documentation Docs need updating

Comments

@DKatarakis
Copy link

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.

@DKatarakis DKatarakis changed the title sg.I() , sg.FilesBrowse() reads Browse button as input value sg.In() , sg.FilesBrowse() reads Browse button as input value Jun 6, 2019
@nngogol
Copy link

nngogol commented Jun 7, 2019

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

@fsmosca
Copy link

fsmosca commented Jun 7, 2019

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

@MikeTheWatchGuy
Copy link
Collaborator

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.

@DKatarakis
Copy link
Author

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.

@MikeTheWatchGuy
Copy link
Collaborator

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.

@MikeTheWatchGuy MikeTheWatchGuy added the documentation Docs need updating label Jun 7, 2019
@MikeTheWatchGuy
Copy link
Collaborator

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]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Docs need updating
Projects
None yet
Development

No branches or pull requests

5 participants