Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to give Tkinter file dialog focus

Tags:

python

tkinter

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

like image 813
Wes Avatar asked Jul 30 '10 20:07

Wes


People also ask

How do you display a file dialog for opening a file in Python?

Use the askopenfilename() function to display an open file dialog that allows users to select one file. Use the askopenfilenames() function to display an open file dialog that allows users to select multiple files.

What is Filedialog in tkinter Python?

filedialog — File selection dialogs. Source code: Lib/tkinter/filedialog.py. The tkinter. filedialog module provides classes and factory functions for creating file/directory selection windows.

What is Askdirectory tkinter?

The askdirectory() comes with filedialog class in tkinter. The askdirectory() method includes a dialog box that only allows directory and return directory path that the user selects. Using the askdirectory() method, first import the filedialog from the tkinter module. Python. Copy from tkinter import filedialog.


4 Answers

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()
like image 188
Wei Li Jiang Avatar answered Oct 03 '22 21:10

Wei Li Jiang


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' ''')
like image 44
username Avatar answered Oct 03 '22 19:10

username


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()
like image 31
user1889297 Avatar answered Oct 03 '22 19:10

user1889297


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.

like image 41
Mian Kashif Ali Avatar answered Oct 03 '22 21:10

Mian Kashif Ali