r0x0r / pywebview

Build GUI for your Python program with JavaScript, HTML, and CSS
https://pywebview.flowrl.com
BSD 3-Clause "New" or "Revised" License
4.62k stars 537 forks source link

create_file_dialog return type inconsistent between windows and linux #576

Closed HackerIndustrial closed 4 years ago

HackerIndustrial commented 4 years ago

Specification

Description

When using the create file dialog the return value returns as a string on windows and a tuple on linux.

result = window.create_file_dialog(webview.SAVE_DIALOG,save_filename='file.json',file_types=file_types) print(type(result)) print("loaded file : " + str(result))

on windows 10 outputs : <class 'str'> loaded file : C:\Users\developer\Desktop\file.json

while on ubuntu : <class 'tuple'> loaded file : ('/media/share/project/tmp/file.json',)

Practicalities

r0x0r commented 4 years ago

It should always return a tuple. Care to provide a fix?

HackerIndustrial commented 4 years ago

thank you for the quick response. I am taking a look at the source code. I believe that linux is by default using the gtk platform while windows defaults to the winform platform correct?

on line 743 of webview/platforms/winforms.py

file_path = dialog.FileName

I took a look at the documentation for savefiledialog on winforms online : https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.savefiledialog?view=netcore-3.1

The documentation stated :+1:

FileName | Gets or sets a string containing the file name selected in the file dialog box.(Inherited from FileDialog)

there is a similar property that can be use

FileNames | Gets the file names of all selected files in the dialog box.

this returns a An array of type String, containing the file names of all selected files in the dialog box.

Option1: Moving forward we could manually add the string to a tuple after line 743 of of winforms.py.

Option2: we change the FileName to get FileNames property. This returns a string array and then we typecast it into a tuple.

What do you think of those options ?

PS. Apologies about the formatting

r0x0r commented 4 years ago

Sorry, I did not pay attention to the fact that is a save dialog. Save dialogs always return a single value, so it should be a string. Open dialogs, on other other hand, return a tuple, no matter how many files are selected.

So in this case the Linux implementation should be changed to return a single string.

HackerIndustrial commented 4 years ago

Oh ok that makes sense.

So gtk.py line 330 will have to be changed from

dialog.get_filenames() to dialog.get_filename()

as per https://lazka.github.io/pgi-docs/Gtk-3.0/classes/FileChooser.html#Gtk.FileChooser.get_filenames

I don't have that much experience with contributing to open source online. Is the easiest course of action to create a merge request ?

HackerIndustrial commented 4 years ago

I have forked and committed a fix for gtk

https://github.com/HackerIndustrial/pywebview/commit/7749f466c42efbfc30f92e5c401496cfbb7e1675

Will be adding a merge request here

r0x0r commented 4 years ago

Good job. Thank you!

HackerIndustrial commented 4 years ago

thanks for the quick response. Awesome project !!