-
-
Notifications
You must be signed in to change notification settings - Fork 31.3k
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
Tkinter appears not to pass file extension to file writing correctly #129534
Comments
This would be easier for someone to test and troubleshoot if you had a simpler example with no external dependencies. |
At the time of posting another test indicated that a workaround seemed possible for simpler files like csv and txt (I believe I overwrote and lost this version-- I should probably have been more methodical). But my intended usage is with dataframes, so I included those file types deliberately, and the bug persisted when using my preferred dataframe write methods no matter my attempts using the xlsxwriter to_excel() function and the pandas to_parquet() function. In retrospect, I'm pretty certain this was a fluke. A friend helped me discover a bit more though. It seems that when using tkinter's file selection dialog, appending the selected file extension will not occur unless a default extension has been specified. It would seem that a default file type being required to pass the selected file type would make sense if only one file type were permitted-- but this does seem like unintended behavior as it currently stands. Suggestion: |
Hi @elkym , I came across this issue and submitted a fix in PR #129682. The fix ensures that when no file extension is provided by the user, asksaveasfilename appends the first extension from the filetypes list, even if defaultextension is not explicitly set. I've tested this with multiple file types (.txt, .csv, .parquet, .xlsx), and the behavior is now more consistent with user expectations. Let me know if you have any thoughts or if there's anything else you'd like to see addressed! Thanks for reporting this! 🚀 |
@serhiy-storchaka This issue needs you opinion as to whether a tkinter change is needed or at least desirable and whether the PR is right approach. |
The There are also issues with the proposed implementation in PR #129682, but since we will not make any changes anyway, I am not going to discuss them here. |
Thank you for the clarification! I now understand that asksaveasfilename() is intended to stay a thin wrapper around Tk's tk_getSaveFile, and that any filename processing should be done at the application level. |
Bug description:
Requirements: pandas
Testing this on Windows 11, Python 3.13, pandas 2.2.3
Also tested on Python 3.11.9
File selection dialog custom class has added method _get_file_type for tkinter's asksaveasfilename for diagnostic prints.
The tkinter file dialog class has an internal method _fixresult-- this will call other methods and pass along the correct file ending, but somehow, my script will not append it.
If the user selects an existing file that has a file extension the ending is included in the file name, and the script correctly writes a file.
If the user manually adds a correct file extension from the list, then python treats that as the correct file extension and ignores the dropdown selection.
But if the user does not manually add a file ending by selection of an existing file, or entering data in the dialog box, no ending is passed to the function, and therefore the file fails to write.
Is this actually not an intended feature? My reading of the source code and documentation, along with some consulting of CoPilot, leads me to believe that the ending selected in the dropdown is intended to be appended to any filename that lacks an ending, but if I've misunderstood, then perhaps this should be reclassified as a new feature request. But my understanding is that it should do this. Help me out if you can. Thanks.
+-----------------------------+
| asksaveasfilename Function |
| |
| - Calls SaveAs.show() |
+-------------+---------------+
|
v
+-------------+---------------+
| SaveAs Class |
| |
| - Inherits from _Dialog |
| - Uses _Dialog.show() |
+-------------+---------------+
|
v
+-------------+---------------+
| _Dialog Class |
| |
| - Defines show() |
| - Calls tk.call() |
| - Passes result to |
| _fixresult() |
+-------------+---------------+
|
v
+-------------+---------------+
| tk.call Method |
| |
| - Executes Tcl command |
| - Returns selected file path |
+-------------+---------------+
|
v
+-------------+---------------+
| _fixresult Method |
| |
| - Processes result |
| - Ensures correct file path |
| and extension |
+-----------------------------+
CODE:
CPython versions tested on:
3.13
Operating systems tested on:
Windows
Linked PRs
The text was updated successfully, but these errors were encountered: