Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a TkInter file selection dialogbox 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: 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 uses Tkinter to do the same thing. In both cases, the code above hangs the IPython console. I gather that this has something to do with event loops, but I have no real experience with GUIs in Python.

Could someone point me in the right direction on how to get a dialogbox for file selection to work with IPython/Spyder. For the record, I am on Python 2.7.6 and IPython 2.4.1

like image 438
HamsterHuey Avatar asked Nov 28 '25 21:11

HamsterHuey


1 Answers

Before running the code above, you need to set the right event loop (in this case Tk), as you correctly guessed.

To do that you need to run this command:

In [1]: %gui tk

and then run your code.

Note: To access the documentation about the %gui magic in Spyder, you need to place the cursor in front of %gui and press Ctrl+I, like this

In [1]: %gui<Ctrl+I>
like image 134
Carlos Cordoba Avatar answered Dec 01 '25 11:12

Carlos Cordoba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!