我正在学习Python中的基本GUI,我遇到了一个示例示例,用于从Stack Overflow上的文件资源管理器中读取文件名。
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing
filename = askopenfilename() # show an "Open" dialog box and return the path to the selected file
print(filename)
当我尝试在IDLE中运行它时,这个特殊的脚本工作正常,但是如果我在Windows 7中从命令提示符处尝试的话,它也没有运行。
Python版本:2.7。这是我得到的输出错误。
>>> from Tkinter import Tk
>>> from tkFileDialog import askopenfilename
>>> Tk().withdraw()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\Lib\lib-tk\Tkinter.py", line 1685, in __init__
self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: Can't find a usable init.tcl in the following directories:
C:/Python27/lib/tcl8.5 D:/PyProj/lib/tcl8.5 D:/lib/tcl8.5 D:/PyProj/library D:/library D:/tcl8.5.2/library D:/tcl8.5.2/library
This probably means that Tcl wasn't installed properly
任何指向我在这里失踪的指针都会有很大的帮助。
答案 0 :(得分:10)
如果您在Windows上使用Virtualenv,我在此处找到了一个解决方案:https://github.com/pypa/virtualenv/issues/93
我复制了&#34; tcl&#34;从C:\ Python27 \到新的Virtualenv的根目录,Tkinter.Tk()显示一个新窗口而不会抛出异常。
我在Windows 7上运行Python 2.7。
答案 1 :(得分:7)
安装Activestate Python和TCL后遇到类似的问题。我发现以下页面为我解决了问题:ActiveState Python install problem。修复方法是将C:\Python27\tcl
的内容复制到C:\Python27\Lib
。
另一个可能的解决方案(由用户i-shenl在同一问题的另一个ActiveState thread中给出)是将environment variable $TCL_LIBRARY
设置为指向tcl库文件夹(“C :/ Python27 / tcl“,在问题中)。如果您在系统范围或帐户范围内设置(通过System Properties),则会影响使用TCL库的其他程序(如果已安装)。如果您正在使用Powershell,则可以在profile中设置此变量,以限制其对从shell运行的程序的影响。
答案 2 :(得分:6)
您只需要将两个文件夹从tcl文件夹复制到Lib文件夹
tcl8.5和tk8.5
答案 3 :(得分:3)
我使用virtualenvwrapper为64位Python 2.7在Ubuntu 17.04上遇到了同样的问题
中添加了tk和tcl库路径workon your-env-name
gedit $VIRTUAL_ENV/bin/postactivate
找到tk和tcl库路径。在postactivate脚本中,使用适当的路径导出TK_LIBRARY和TCL_LIBRARY。使用修改后的路径将此行添加到脚本中:
TK_LIBRARY=/home/kamil/anaconda2/pkgs/tk-8.5
TKPATH=/home/kamil/anaconda2/pkgs/tk-8.5
TCL_LIBRARY=/home/kamil/anaconda2/lib/tcl8.5
export TCL_LIBRARY TK_LIBRARY TKPATH
deactivate
和workon your-env-name
。答案 4 :(得分:1)
如果您在python -m venv NAME
种虚拟环境中遇到此类错误(并且您实际上已在系统中安装了tcl),那么您需要像Kamil一样导出与suggested类似的路径Czerski在之前的文章中为virtualenv。
import tkinter root = tkinter.Tk() print(root.tk.exprstring('$tcl_library')) print(root.tk.exprstring('$tk_library'))
bin/activate
并找到它们export PATH
的位置并在此之后插入(从步骤1插入正确的路径):TCL_LIBRARY="/tcl/path/from/step/1" TK_LIBRARY="/tk/path/from/step/1" TKPATH="/tk/path/from/step/1" export TCL_LIBRARY TK_LIBRARY TKPATH
deactivate source bin/activate
“Tcl缺失” - 错误应该消失。
答案 5 :(得分:0)
IDLE可能正在设置TCL所需的路径。要找出IDLE正在使用的路径,请比较IDLE和没有IDLE的sys.path的输出。然后,您可以使用环境变量或以编程方式添加init.tcl的位置。参见Xenomorph建议。
答案 6 :(得分:0)
您需要做的就是将tcl 8.6
中的tcl 8.5
和tcl
从Lib
文件复制到python
中的Python-tcl-tcl8.5
文件中。
Python-Lib
至import {Injectable} from '@angular/core';
import {ActivatedRoute, NavigationEnd, Router} from "@angular/router";
import {Observable} from "rxjs";
import {first, filter, map, switchMap} from "rxjs/operators";
@Injectable({providedIn: 'root'})
export class RouteService {
constructor(
private route: ActivatedRoute,
private router: Router
){}
public getActivatedRouteParameter(): Observable<any>
{
let params
params = this.router.events.pipe(
filter(e => e instanceof NavigationEnd),
map((): ActivatedRoute => {
let route = this.route;
while (route.firstChild)
{
route = route.firstChild;
}
return route;
}),
filter((route: ActivatedRoute) => route.outlet === 'primary'),
switchMap((route: ActivatedRoute) => route.paramMap) , first()
);
return params;
}
}
答案 7 :(得分:0)
转到存储所有python依赖项的目录
示例:
Python37
-DLLs
-Doc
-etc
-include
-Lib
-libs
-Scripts
-tcl
-python.exe
转到tcl文件夹,复制tcl8.5和tk8.5文件夹 将这些文件夹粘贴到Lib文件夹中
此解决方案适用于Windows 10用户