You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
filedialog.askdirectory "Cancel" behaves differently under Linux and Windows (Wine) #103878
Native Linux binary and Windows executable (under Wine) have different behaviors for tkinter's filedialog.askdirectory. Clicking "Cancel" results in a return value of () (empty tuple) under Linux, and "" for the Windows executable.
The underlying Tcl/Tk command tk_chooseDirectory (as well as tk_getOpenFile and tk_getSaveFile) has implementations for each platform. From Tcl/Tk’s perspective, clicking “cancel” causes an empty string value to be returned regardless of platform. However incidental differences between these implementations are causing the returned empty string value to either be unshared and untyped on Win32 and Aqua, or highly shared and often having type (such as "list", since the empty string is the empty Tcl list) on X11. And in general Tkinter does not automatically convert a Tcl value to a Python string if it has a type (other than "string").
It would perhaps be more Pythonic to return None as a special value. Some other dialogs already return None if cancelled (for example askyesnocancel() and askstring()).
The [...] functions when called create a modal, native look-and-feel dialog, wait for the user’s selection, then return the selected value(s) or None to the caller.
The [...] functions when called create a modal, native look-and-feel dialog, wait for the user’s selection, then return the selected value(s) or None to the caller.
I think it might be incidental that askopenfilename(), askopenfilenames(), asksaveasfilename(), and askdirectory() are documented as possibly returning None.
The documentation there describes not just functions which return file/directory names (e.g. askopenfilename()), but also functions which do return either file objects or None (e.g. askopenfile()). When this documentation was written several years ago, both kinds of ask… functions were grouped together, and then the verbiage about returning None was added in response to this review comment from Mark Roseman (TkDocs.com author):
I would suggest putting the ask* functions first, since those are the preferred interface. For each, mention that these are modal dialogs, also that return value may be None.
…even though only some (not all) of the ask… functions return None in practice.
Should the documented behavior still be considered the correct behavior?
Assuming the documentation is the correct behavior, in my code I just wrap those functions like:
defaskopenfilename(*args, **kwargs) ->str|None:
""" Same of tkfile.askopenfilename Add a workaround for https://github.com/python/cpython/issues/103878. """res=filedialog.askopenfilename(*args, **kwargs)
returnresifreselseNone
In case this is what we want, probably it is sufficient to add something like that on:
The blanket "or None" wording quoted above was already corrected by the gh-86726 documentation overhaul, which now documents each function's real cancelled value (in main, 3.14 and 3.13).
PR #152429 adds a note advising callers to test the result for truth rather than comparing it with a specific empty value, since its exact type ("", (), [] or None) is not stable across platforms and Tk versions.
That covers the documentation side. Normalizing the return type itself — so that e.g. askdirectory() consistently returns "" rather than () — is a separate, behavior-changing fix for a future release; it remains open here (see also PR #111195).
PR #152435 normalizes the cancelled result on main: '' for askopenfilename()/asksaveasfilename()/askdirectory(), and an empty tuple for askopenfilenames(), dropping the platform-dependent ()/b''.
It supersedes #111195 by deciding single-vs-multiple from the multiple option rather than the result type, so askopenfilenames()'s legitimate empty tuple is preserved. The doc half is #152429.
Activity
ronaldoussoren commentedon Apr 29, 2023
On macOS this returns
"".chrstphrchvz commentedon May 3, 2023
The underlying Tcl/Tk command
tk_chooseDirectory(as well astk_getOpenFileandtk_getSaveFile) has implementations for each platform. From Tcl/Tk’s perspective, clicking “cancel” causes an empty string value to be returned regardless of platform. However incidental differences between these implementations are causing the returned empty string value to either be unshared and untyped on Win32 and Aqua, or highly shared and often having type (such as "list", since the empty string is the empty Tcl list) on X11. And in general Tkinter does not automatically convert a Tcl value to a Python string if it has a type (other than "string").chrstphrchvz commentedon May 4, 2023
A similar issue was already addressed for
tkinter.colorchooser.Chooser: #48017pythongh-103878: Ensure canceled file dialog returns `""`
serhiy-storchaka commentedon Jan 18, 2024
It would perhaps be more Pythonic to return
Noneas a special value. Some other dialogs already returnNoneif cancelled (for exampleaskyesnocancel()andaskstring()).gcerretani commentedon Oct 27, 2025
From https://docs.python.org/3/library/dialog.html:
chrstphrchvz commentedon Nov 1, 2025
I think it might be incidental that
askopenfilename(),askopenfilenames(),asksaveasfilename(), andaskdirectory()are documented as possibly returningNone.The documentation there describes not just functions which return file/directory names (e.g.
askopenfilename()), but also functions which do return either file objects orNone(e.g.askopenfile()). When this documentation was written several years ago, both kinds ofask…functions were grouped together, and then the verbiage about returningNonewas added in response to this review comment from Mark Roseman (TkDocs.com author):…even though only some (not all) of the
ask…functions returnNonein practice.Should the documented behavior still be considered the correct behavior?
gcerretani commentedon Nov 4, 2025
Assuming the documentation is the correct behavior, in my code I just wrap those functions like:
In case this is what we want, probably it is sufficient to add something like that on:
cpython/Lib/tkinter/filedialog.py
Line 313 in 25a2a60
cpython/Lib/tkinter/filedialog.py
Line 337 in 25a2a60
cpython/Lib/tkinter/filedialog.py
Line 364 in 25a2a60
serhiy-storchaka commentedon May 24, 2026
The documentation is incorrect and should be changed to match the behavior. It is too late to change the behavior of the released Python versions.
After fixing the documentation we can change the type of the returning value for future releases, to make it more stable and consistent.
serhiy-storchaka commentedon Jun 27, 2026
The blanket "or
None" wording quoted above was already corrected by the gh-86726 documentation overhaul, which now documents each function's real cancelled value (in main, 3.14 and 3.13).PR #152429 adds a note advising callers to test the result for truth rather than comparing it with a specific empty value, since its exact type (
"",(),[]orNone) is not stable across platforms and Tk versions.That covers the documentation side. Normalizing the return type itself — so that e.g.
askdirectory()consistently returns""rather than()— is a separate, behavior-changing fix for a future release; it remains open here (see also PR #111195).serhiy-storchaka commentedon Jun 27, 2026
PR #152435 normalizes the cancelled result on
main:''foraskopenfilename()/asksaveasfilename()/askdirectory(), and an empty tuple foraskopenfilenames(), dropping the platform-dependent()/b''.It supersedes #111195 by deciding single-vs-multiple from the
multipleoption rather than the result type, soaskopenfilenames()'s legitimate empty tuple is preserved. The doc half is #152429.pythongh-103878: Fix the documented cancelled value of askopenfiles()
pythongh-103878: Drop "an empty list" from the cancelled-value list
pythongh-103878: Return a consistent empty value from cancelled file …
gh-103878: Document that the cancelled file dialog value is only falsy (
[3.13] gh-103878: Document that the cancelled file dialog value is on…
[3.14] gh-103878: Document that the cancelled file dialog value is on…
[3.15] gh-103878: Document that the cancelled file dialog value is on…
pythongh-103878: Make askopenfiles() return a list consistently
gh-103878: Return a consistent empty value from cancelled file dialogs (