Python Tkinter throwing Tcl error

北城以北 提交于 2020-12-29 09:47:28

问题


I am learning basic GUI in Python, and I came across a sample example to read file name from file explorer on 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)

This particular script is working fine when I am trying to run it in IDLE, but the same is not running if I am trying from command prompt in windows 7.

Python Version: 2.7. Here is the output error which I get.

>>> 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

Any pointer to what I am missing here can be of great help.


回答1:


In case you are using Virtualenv on Windows I found a solution here: https://github.com/pypa/virtualenv/issues/93

I copied the "tcl" folder from C:\Python27\ over to the root of the new Virtualenv, Tkinter.Tk() shows a new window without throwing an exception.

I am running Python 2.7 on Windows 7.




回答2:


You just need to copy two folders from tcl folder to the Lib folder

tcl8.5 and tk8.5




回答3:


Hit a similar problem after installing Activestate Python and TCL. I found the following page solved the problem for me: ActiveState Python install problem. The fix was to copy the contents of C:\Python27\tcl into C:\Python27\Lib.

Another potential solution (given by user i-shenl in a different ActiveState thread on the same issue) is to set the environment variable $TCL_LIBRARY to point to the tcl library folder ("C:/Python27/tcl", in the question). If you set this system-wide or account-wide (via System Properties), it will affect other programs that use a TCL Library (if any are installed). If you're using Powershell, you can set this variable in your profile to limit its affects to programs run from the shell.




回答4:


I hit the same problem on Ubuntu 17.04 with virtualenvwrapper for 64 bit Python 2.7

I add tk and tcl library paths in local postactivate script

  1. Go to your virtualenv: workon your-env-name
  2. Edit local postactiave script with your favourite editor, for ex: gedit $VIRTUAL_ENV/bin/postactivate
  3. Locate tk and tcl library paths. In postactivate script, export TK_LIBRARY and TCL_LIBRARY with appropriate paths. Add this lines to your script with modified paths:

    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

  4. Restart your virtualenv: deactivate and workon your-env-name again.



回答5:


If you are hitting this kind of error in a python -m venv NAME kind of virtual environment (and you actually have tcl installed in your system), then you need to export the paths similarly as suggested by Kamil Czerski in a previous post for virtualenv.

  1. To find out what are your TK and TCL paths, run a python script outside of the venv (source):
import tkinter
root = tkinter.Tk()   
print(root.tk.exprstring('$tcl_library'))   
print(root.tk.exprstring('$tk_library'))
  1. Open your venv configuration file bin/activate and find the place where they export PATH and insert after this (insert correct paths from step 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
  1. Deactivate (if it was activated) and source again your venv:
deactivate  
source bin/activate

The "Tcl missing"-error should be gone.




回答6:


IDLE is probably setting the path required for TCL. To find out what path is being used by IDLE, compare the output of sys.path from IDLE and without IDLE. Then you can add the location of init.tcl either using an environment variable or programatically. See Xenomorph suggestion.




回答7:


All you need to do is copy tcl 8.6 and tcl 8.5 from tcl file to Lib file on in python. Python-tcl-tcl8.5 to Python-Lib




回答8:


Go to directory in which all of your python dependencies are stored

Example:

Python37 
  -DLLs
  -Doc
  -etc
  -include
  -Lib
  -libs
  -Scripts
  -tcl
  -python.exe

Go to tcl folder, copy the tcl8.5 and tk8.5 folder Paste these folders in the Lib folder

This solution works for Windows 10 users



来源:https://stackoverflow.com/questions/29320039/python-tkinter-throwing-tcl-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!