Tk Source Code

View Ticket
Login
Ticket UUID: 35a8df9a4288a619b4592d16a9e133099100ab60
Title: MS-Win: tk_getOpenFile fails with "CoInitialize has not been called" if no toplevel has been mapped yet
Type: Bug Version: main
Submitter: serhiy.storchaka Created on: 2026-09-18 11:38:36
Subsystem: 37. [tk_get*File] Assigned To: oehhar
Priority: 5 Medium Severity: Important
Status: Closed Last Modified: 2026-09-29 19:09:52
Resolution: Fixed Closed By: oehhar
    Closed on: 2026-09-29 19:09:52
Description:

On Windows, tk_getOpenFile, tk_getSaveFile and tk_chooseDirectory fail with the error "CoInitialize has not been called." if they are called before any toplevel window has been mapped, e.g. from a script run by wish before the first update, or after wm withdraw . (a common idiom in Tkinter: root.withdraw(); filedialog.askopenfilename()).

Since [03ce8fe9d1] (removal of the XP dialogs) tkWinDialog.c no longer calls CoInitialize() itself. COM is initialized only in UpdateWrapper() in tkWinWm.c, which is called when a toplevel is mapped, so the dialogs work only after that.

8.6 and 9.0 are not affected.

Fixed on branch win-dialog-coinitialize in [cf189c70d7]: GetFileNameVista() calls CoInitialize() once per thread, as VistaFileDialogsAvailable() did before.

User Comments: oehhar added on 2026-09-29 19:09:52:

Here is my demo script "demo.tcl":

set res [catch {tk_getOpenFile -title Open} e d]
console show
update
puts $d

The command "wish9.1.exe demo.tcl" shows the issue for 9.1.0 and not for the bugfix branch:

-code 1 -level 0 -errorstack {INNER {invokeStk tk_getOpenFile -title Open}} -errorcode NONE -errorinfo {CoInitialize wurde nicht aufgerufen.
    while executing
"tk_getOpenFile -title Open"} -errorline 1

Merged to main by [fb0d94c6]

Thanks for all !

Bug closed