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

open_file_dialog return object instead path string #1483

Closed
matteotanghe opened this issue May 9, 2022 · 3 comments
Closed

open_file_dialog return object instead path string #1483

matteotanghe opened this issue May 9, 2022 · 3 comments
Labels
bug A crash or error in behavior. documentation An improvement required in the project's documentation.

Comments

@matteotanghe
Copy link

matteotanghe commented May 9, 2022

Describe the bug
If I want to have a path with the function open_file_dialog or select_folder_dialog it returns to me, the object.

To Reproduce
When I call open_file_dialog(), in my case, it return something like <toga_gtk.dialogs.OpenFileDialog object at 0x7f5e96c373c8>
Do I made something wrong ?
I make the function call like that :
self.filename = self.main_window.open_file_dialog("title", initial_directory=None, multiselect=True, file_types=['png', 'jpg']) print(self.filename)

Expected behavior
If I read the doc, normally the function open_file_dialog(title, initial_directory=None, file_types=None, multiselect=False, on_result=None)returns a string, in this case, the path. And this is what I need.

Environment:

  • Operating System: Linux Ubuntu
  • Python version: Python 3.6.9
  • Software versions:
    • Briefcase: 0.3.6

It's little bit strange, maybe I made something wrong, but normally I easy the retrieve a path and I don't understand why it doesn't return me the same thing that the documentation.

@matteotanghe matteotanghe added the bug A crash or error in behavior. label May 9, 2022
@samschott
Copy link
Member

@matteotanghe, you are not doing anything wrong, this is a shortcoming of the current documentation. The dialog methods all create the dialog and then immediately return a dialog object, such as <toga_gtk.dialogs.OpenFileDialog object ...> on Gtk. This means: they return before the user actual interacts with the dialog to select a file, click a button, etc., so that the method does not block the execution of the program.

You can await the returned object to get the actual result, in your case a string -- if you created the dialog inside a function defined with async def. For example:

async def select_file(self):
    path = await self.open_file_dialog()
    print("User selected:", path)

See https://docs.python.org/3/library/asyncio.html if you're not yet familiar with async programming in Python.

Alternatively, you can provide a function which will be called with the resulting path once the user has selected a file:

def print_path(window, path):
    print("User selected:", path)

def select_file(self):
    self.open_file_dialog(on_return= print_path)

@freakboy3742, I was expecting exactly such reports to come up after reading through #1464 :)

@matteotanghe
Copy link
Author

Thank you, @samschott, I understand better now, how it's works and why :) .
Have a good day

@freakboy3742
Copy link
Member

@samschott Yeah - it was somewhat inevitable; but also an unavoidable consequence of moving to async dialogs.

@matteotanghe Thanks for the report; I'll leave this ticket open as a marker that the docstrings need to be updated.

@freakboy3742 freakboy3742 added the documentation An improvement required in the project's documentation. label May 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A crash or error in behavior. documentation An improvement required in the project's documentation.
Projects
None yet
Development

No branches or pull requests

3 participants