14

I'm using OS X. I'm double clicking my script to run it from Finder. This script imports and runs the function below.

I'd like the script to present a Tkinter open file dialog and return a list of files selected.

Here's what I have so far:

def open_files(starting_dir):
    """Returns list of filenames+paths given starting dir"""
    import Tkinter
    import tkFileDialog

    root = Tkinter.Tk()
    root.withdraw()  # Hide root window
    filenames = tkFileDialog.askopenfilenames(parent=root,initialdir=starting_dir)
    return list(filenames)

I double click the script, terminal opens, the Tkinter file dialog opens. The problem is that the file dialog is behind the terminal.

Is there a way to suppress the terminal or ensure the file dialog ends up on top?

Thanks, Wes

Wes
  • 185
  • 1
  • 2
  • 5
  • This may help: http://stackoverflow.com/questions/1810497/hide-console-for-tkinter-app-on-osx – Gary Kerr Jul 30 '10 at 21:24
  • Thank you I'll look into using that in the long term. Right now this program is very simple and is iterating quickly. I was looking to find a quick method of solving this annoyance. – Wes Aug 05 '10 at 16:44

6 Answers6

15

For anybody that ends up here via Google (like I did), here is a hack I've devised that works in both Windows and Ubuntu. In my case, I actually still need the terminal, but just want the dialog to be on top when displayed.

# Make a top-level instance and hide since it is ugly and big.
root = Tkinter.Tk()
root.withdraw()

# Make it almost invisible - no decorations, 0 size, top left corner.
root.overrideredirect(True)
root.geometry('0x0+0+0')

# Show window again and lift it to top so it can get focus,
# otherwise dialogs will end up behind the terminal.
root.deiconify()
root.lift()
root.focus_force()

filenames = tkFileDialog.askopenfilenames(parent=root) # Or some other dialog

# Get rid of the top-level instance once to make it actually invisible.
root.destroy()
Wei Li Jiang
  • 151
  • 1
  • 3
6

Use AppleEvents to give focus to Python. Eg:

import os

    os.system('''/usr/bin/osascript -e 'tell app "Finder" to set frontmost of process "Python" to true' ''')
username
  • 18,800
  • 11
  • 41
  • 45
  • 1
    The Tk mechanisms (which seem to boil down to `focus_set()` and `focus_force()` on the root or parent window) may work on Linux or elsewhere, but they don't work on Mac OS X. This was the only thing I've found that works on Mac. – Jonathan Eunice Apr 02 '15 at 16:27
4

I had this issue with the window behind Spyder:

root = tk.Tk()
root.overrideredirect(True)
root.geometry('0x0+0+0')
root.focus_force()
FT = [("%s files" % ftype, "*.%s" % ftype), ('All Files', '*.*')]
ttl = 'Select File'
File = filedialog.askopenfilename(parent=root, title=ttl, filetypes=FT)
root.withdraw()
user1889297
  • 464
  • 5
  • 13
4

None of the other answers above worked for me 100% of the time. In the end, what worked for me was adding 2 attibutes: -alpha and -topmost This will force the window to be always on top, which was what I wanted.

import tkinter as tk

root = tk.Tk()
# Hide the window
root.attributes('-alpha', 0.0)
# Always have it on top
root.attributes('-topmost', True)
file_name = tk.filedialog.askopenfilename(  parent=root, 
                                            title='Open file',
                                            initialdir=starting_dir,
                                            filetypes=[("text files", "*.txt")])
# Destroy the window when the file dialog is finished
root.destroy()
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
1

filenames = tkFileDialog.askopenfilenames(parent=root,initialdir=starting_dir)

Well parent=root is enough for making tkFileDialog on top. It simply means that your root is not on top, try making root on top and automatically tkFileDialog will take top of the parent.

Miraj50
  • 4,257
  • 1
  • 21
  • 34
0

Try the focus_set method. For more, see the Dialog Windows page in PythonWare's An Introduction to Tkinter.

GreenMatt
  • 18,244
  • 7
  • 53
  • 79
  • Thank you. I read it over. I'm not sure how I can use the set_focus() method on the built in TK file dialog window? – Wes Aug 05 '10 at 16:48