13

I am running a script that prompts the user for a file. There is no gui except for the file browser that opens up. I have 2 options: browse for file, or select entire folder using askdirectory(). The latter opens on top of all other windows, but the first one opens under everything, I have to minimize other windows to find it.

Here is the method I'm using for these operations

from Tkinter import Tk
from tkFileDialog import askdirectory, askopenfilename

root = Tk()
root.withdraw()

self.inpath = askdirectory()  # To open entire folder
Path = askopenfilename()      # Open single file

root.destroy()   # This is the very last line in my main script.

This is everything Tk related in my code. askdirectory opens on top, askopenfilename doesn't.

Is there a way to force it to open on top?

Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
J_matts
  • 171
  • 1
  • 1
  • 4

5 Answers5

10

root.wm_attributes('-topmost', 1) did it for me. I found it in another SO thread to be honest :-).

seesharp
  • 47
  • 11
5

I want to share that the following lines worked superbly in my case. But I had to use both window.wm_attributes('-topmost', 1) and window=parent to make this work, see below:

import tkinter as tk
from tkinter import filedialog
window = tk.Tk()
window.wm_attributes('-topmost', 1)
window.withdraw()   # this supress the tk window

   filename = filedialog.askopenfilename(parent=window,
                                  initialdir="",
                                  title="Select A File",
                                  filetypes = (("Text files", "*.txt"), ("All files", "*")))
# Here, window.wm_attributes('-topmost', 1) and "parent=window" argument help open the dialog box on top of other windows
Ashok
  • 51
  • 1
  • 2
4

I had the same problem. For me it works with:

file = filedialog.askopenfilename(parent=root)

So, the file dialog gets in front of toplevel window without uncomment root.attributes("-topmost", True)

Bùi Đức Khánh
  • 3,975
  • 6
  • 27
  • 43
2

I had the same issue of the file dialog window opening below my current window but I couldn't reproduce the issue with your code (in Python 2 or 3).

This is the minimal example where the issue occurs (context is Windows 10, Python 3, script is called from Idle, and note the input function:

File dialog opens below:

from tkinter import filedialog, Tk

root = Tk()
root.withdraw()
input("\nType anything> ")

file =  filedialog.askopenfilename()

To open file dialog on top, both root.lift() or root.attributes("-topmost", True) work (but the latter is specific for Windows)

from tkinter import filedialog, Tk

root = Tk()
#root.attributes("-topmost", True) # this also works
root.lift()
root.withdraw()
input("\nType anything> ")

file =  filedialog.askopenfilename()
user2314737
  • 27,088
  • 20
  • 102
  • 114
0

I'm running python 3.x so there is a difference in code, but both opened on top for me. Try giving it focus, it should put in on top.

self.inpath.focus()

I'm not sure if it's gonna work, since I cant reproduce the problem.

Jozef Méry
  • 357
  • 5
  • 15