问题描述
我在其他问题中注意到 Spyder 中的 TKinter 存在(或曾经)几个问题.我已经在 IDLE 中使用它有一段时间了,但我正在转向 Spyder,并遇到了一些问题.
I noticed in other questions that there are (or were) several problems with TKinter in Spyder. I have been using it in IDLE for a while but I am moving to Spyder, and came upon some problems.
我在 Windows 7 Enterprise 上使用 Anaconda 1.8.4 的 Spyder 3.2.8 运行 Python 3.6.4.
当我尝试使用某些 TKinter 函数(如 filedialog.askdirectory
)时,Spyder 的控制台冻结.我一直在阅读不同的论坛,但仍然没有人遇到相同的问题或解决此问题的方法.这是一个简单的代码,可以在 IDLE 中运行,但不能在 SPYDER 中运行:
When I try to use some TKinter functions (like filedialog.askdirectory
) Spyder´s console freeze.I´ve been reading different forums but still no one has the same problem or a solution to this problem.Here is a simple code that would work in IDLE, but not in SPYDER:
import os
from tkinter import Tk, filedialog
Tk().withdraw()
print("Done WITHDRAW")
currentdir= os.getcwd()
print("Done GETCWD")
filename= filedialog.askdirectory(title="Select folder", initialdir=currentdir)
print("Done ASKDIRECTORY")
结果,我得到:
runfile('M:/Users/KPK2/.../hello.py', wdir='M:/Users/KPK2/...')
Done WITHDRAW
Done GETCWD
并且控制台一直在运行,等待 ASKDIRECTORY 弹出一个新窗口来选择一个文件.在 IDLE 中它工作得很好.有谁知道这可能是问题所在以及一些可能的解决方案?
And the console keeps running, waiting for the ASKDIRECTORY to pop a new window to select a file. In IDLE it works just fine.Does anyone know which could be the problem and some possible solution?
我阅读了其他线程解决方案,例如更新到 Spyder 3.0(我已经有 3.2.8)或更改首选项中控制台"的一些外部模块",但我的首选项窗口中没有这样的选项卡(不不知道该怎么做).
I read on other threads solutions like updating to Spyder 3.0 (I already have 3.2.8) or changing some "External Modules" for the "Console" in Preferences, but there is not such a tab on my Prefereneces window (don´t know how to do that otherwise).
谢谢.
推荐答案
在运行代码之前尝试在控制台运行此命令 %gui tk.
try running this command %gui tk at the console before running your code.
另一种方法是转到 Tools >首选项IPython 控制台 >图形 >图形后端
并在那里选择tkinter
.
The alternative is it go to Tools > Preferences > IPython Console > Graphics > Graphics backend
and select tkinter
there.
这对我有用:)答案在这里https://groups.google.com/forum/#!topic/spyderlib/rFJhJZgjZTE
This worked for me :)The answer was found here https://groups.google.com/forum/#!topic/spyderlib/rFJhJZgjZTE
这篇关于TKinter (filedialog.askdirectory) 冻结 Spyder 控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!