Tkinter filedialog window disappears when I hide tkinter main window and click something else than the filedialog

72 Views Asked by At

I have a code that hides the tkinter main window and opens a tkinter.filedialog window:

import tkinter
import tkinter.filedialog as fd
import pandas as pd

tkinter.Tk().withdraw() # hide the main window 
excel_file_path = fd.askopenfilename()
if excel_file_path == "":
    quit() # user clicked cancel ---> terminate program execution
df = pd.read_excel(excel_file_path) 
# process df ...

The problem is that when the askopenfilename() box is open, if I click something else than the box, for example I click my excel sheet or anything else on the window but the dialog box, the dialog box disappears. The program is still running, but I cannot do anything. ctrl-C does not help. The only thing I can do is click ctrl-alt-del and kill the process from task manager.

I tried to change tkinter.Tk().withdraw() to tkinter.Tk().iconify(). This helps and removing withdraw() altogether helps as well, but when I do that, the main window is visible.

What I want to do is to hide the main window and stop the dialog box from disappearing. How can I do that?

0

There are 0 best solutions below