[python-win32] How to detect User pressing escape, or clicking cancel in call to win32gui.GetOpenFileNameW()

Mark Hammond mhammond at skippinet.com.au
Wed Mar 14 02:37:30 CET 2007


> I'm trying to understand how to detect a user pressing Escape, or
> clicking on Cancel when win32gui.GetOpenFileNameW() is called.
>
> When I press escape when the FileOpen dialogue is present, I see the
> following error with Python2.5/PWin32 Build 210
>
> error: (0, 'GetOpenFileNameW', 'No error message is available')
>
> Would someone explain what this means, or how to detect the
> above situations?
>
> My call looks like this
>
>
> filter='Patches'
>         customfilter='All Files'
>         unicodeFilenames=None
>         unicodeFilenames, customfilter, flags =
> win32gui.GetOpenFileNameW(Filter=filter,
>         CustomFilter=customfilter,
>         FilterIndex=1,
>         File='*.pch',
>         InitialDir=os.getcwd(),
>         Title='Select Input Files',
>         Flags=win32con.OFN_ALLOWMULTISELECT|win32con.OFN_EXPLORER,
>         DefExt='.pch')

As currently implemented, you must detect cancel by trapping win32gui.error,
and checking the hresult is zero.  While this is slightly ugly, it is more
convenient than returning None, as in the usual case there are 3 return
values.  Returning (None, None, None) might have been possible, but was not
implemented, and I'm reluctant to change the semantics of the return value
in this case.  I'll document this.

Mark



More information about the Python-win32 mailing list