How to get the TkInter file selection dialog to work with IPython / Spyder?

I am trying to create a simple file selection dialog for one of my scripts and I was trying to use the code examples from this thread: A quick and easy file dialog in Python?

import Tkinter, tkFileDialog

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

file_path = tkFileDialog.askopenfilename()

      

I also tried using the easygui module that Tkinter uses to accomplish the same. In both cases, the above code hangs in the IPython console. I realize it has something to do with event loops, but I have no real experience with GUIs in Python.

Can anyone point me in the right direction on how to get the file selection dialog to work with IPython / Spyder. For the record, I am on Python 2.7.6 and IPython 2.4.1

+3


source to share


1 answer


Before running the above code, you need to set up the correct event loop (in this case Tk

), as you guessed right.

To do this, you need to run this command:

In [1]: %gui tk

      



and then run your code.

Note . To access the documentation about magic %gui

in Spyder you need to place your cursor in front of %gui

and click Ctrl+I

like this

In [1]: %gui<Ctrl+I>

      

+1


source







All Articles