I noticed a problem with .withdraw()
while working with filedialogs in combination with pygame.
When using .withdraw() on the root window, the tkinter filedialog also disappears from the taskbar. This means that when the filedialog loses focus (like when you interact with another (pygame in my case) window by clicking it), the filedialog disappears and cannot be reopened by clicking the icon in your taskbar, but it's still active in the background, which permanently paused my pygame app. ("softlocking" the user)
The solution is to use iconify()
instead of withdraw
, which, as the name implies, does not remove the icon from the taskbar (allowing the user to reopen the window!), but it does minimise the window.
I ended up with this, after some experimentation:
import tkinter
from tkinter import filedialog
root = tkinter.Tk() # } execute these lines once, to "initialise" tkinter
root.iconify() # }
def prompt_files():
try:
file_paths = filedialog.askopenfilenames()
root.iconify()
except tkinter.TclError: # this error was raised once or twice while testing, just in case return an empty list
return []
else:
return file_paths
def stop_tk(): # call this when you're done selecting files, to stop tkinter from running mainloop in the background
root.destroy()
It's not flawless, but I've not been able to find anything better.