Use of Tk from within Spyder Cause a Freeze

2,726 views
Skip to first unread message

Eric Shain

unread,
Mar 21, 2016, 8:16:34 PM3/21/16
to spyder
Using Spyder2 with Python 3.5 on a Macintosh, I'm having trouble with Tk. Specifically I'm trying to use Tk to generate a open file dialog. Here is what I type:

import tkinter as tk
from tkinter.filedialog import askopenfilename

root = tk.Tk()

At this point, everything freezes. I get a little spinning icon and I have to force quit. This all works (and more) from a separate ipython console but either typed interactively or run from a file within spyder, it causes spyder to freeze up.

Any idea of how to fix this? I should mention I'm using the Anaconda installed version of Spyder2.

Many thanks,

Eric

Eric Shain

unread,
Mar 25, 2016, 1:58:30 PM3/25/16
to spyder
Its been several days and no one has commented on my question. Is there a better venue to post this?

Eric

Carlos Córdoba

unread,
Mar 25, 2016, 2:00:05 PM3/25/16
to spyd...@googlegroups.com
Hi,

You need to use the IPython Console to run your code, but before doing it, please run this command there:

    %gui tk


Cheers,
Carlos

El 25/03/16 a las 12:55, Eric Shain escribió:
--
You received this message because you are subscribed to the Google Groups "spyder" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spyderlib+...@googlegroups.com.
To post to this group, send email to spyd...@googlegroups.com.
Visit this group at https://groups.google.com/group/spyderlib.
For more options, visit https://groups.google.com/d/optout.

Eric Shain

unread,
Apr 3, 2016, 11:39:55 PM4/3/16
to spyder
While I appreciate the response, it isn't all that satisfying. Are you saying that Tk code is never going to work in Spyder? I like developing in Spyder because of its various features. Having to switch to a different editor if I want to use Tk would be annoying.

Eric

Nikolay Karelin

unread,
Apr 4, 2016, 11:00:34 AM4/4/16
to spyder
Hello Eric,

I've successfully run your code in Spyder (well, after adding root.mainloop() after your code) without any freezing.

The only change I've made was to invoke Run/Configure (or press F6) and select new console or dedicated interpreter.

After trying advice from Carlos - everything were also fine in existing IPython console within Spyder...

Best,
Nikolay

Carlos Córdoba

unread,
Apr 4, 2016, 11:26:42 AM4/4/16
to spyd...@googlegroups.com
Hi Eric,

It seems you don't understand how GUI development work. The command I mentioned before, i.e.

    %gui tk

is necessary to activate something called the Tk event loop. That's what allows you running Python files that create Tk graphical interfaces without blocking the IPython Console, i.e. it allows you to keep running things in the console while your interface is running in the background.

That's not a Spyder limitation, it's somethings that needs to be done for every graphical application you're trying to develop and every toolkit you're using (because Tk is not the only GUI toolkit available :-).

However, there's a way to set the Tk event loop without running that little command. For that you need to go to

    Tools > Preferences > IPython Console > Graphics > Graphics backend

and select

    Tk

there.


Cheers,
Carlos

El 03/04/16 a las 13:59, Eric Shain escribió:

Eric Shain

unread,
Apr 4, 2016, 12:37:14 PM4/4/16
to spyder
Thanks, it makes more sense now.

Eric
Reply all
Reply to author
Forward
0 new messages