Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TKinter tkFileDialog.askopenfilename Always behind other windows

Tags:

python

tkinter

I want to create a simple TKinter file selection dialog with a function that I will use from other scripts and not a wider GUI.

My current code is:

# Select a single file and return the full path as a string
def select_file(data_dir):

    chdir(data_dir)

    root = Tkinter.Tk()
    root.withdraw()

    file_path = tkFileDialog.askopenfilename()

    return file_path

When I run this the file dialog is always behind other windows. If I have Spyder maximised, it opens behind it so I have to minimise.

There are a few questions related to this, but I've been unable to get any of the suggested code to work, so apologies if this is viewed as a duplicate question.

Ben

like image 812
BMichell Avatar asked Nov 08 '22 20:11

BMichell


1 Answers

Just have to use root.deiconify() after file_path = tkFileDialog.askopenfilename()

But it's a bad idea to create a new Tk here.

like image 68
Clodion Avatar answered Nov 15 '22 13:11

Clodion