相关文章推荐
粗眉毛的蚂蚁  ·  python tkinter | ...·  1 周前    · 
活泼的椰子  ·  self.tk.call(_flatten( ...·  2 天前    · 
高大的板栗  ·  tkinter中的askstring参数 ...·  6 小时前    · 
暗恋学妹的柑橘  ·  AttributeError: ...·  6 小时前    · 
悲伤的大熊猫  ·  Note_Spark_Day08:Spark ...·  4 月前    · 
路过的酱肘子  ·  Z%E5%BD%A9%E7%A5%A8%E6 ...·  1 年前    · 
腼腆的小刀  ·  文末送书 | ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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.

Are you certain that the version of python you use in idle is the same version being used on the command line? – Bryan Oakley Mar 28, 2015 at 17:12 @BryanOakley yes I am using the same version. Here is a snippet of the same IDLE: Python 2.7 (r27:82525, Jul 4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on win32 Python CmdLine: Python 2.7 (r27:82525, Jul 4 2010, 07:43:08) [MSC v.1500 64 bit (AMD64)] on win32 – charan Mar 28, 2015 at 17:15 This solved the problem for me. Windows 10, running Python in PowerShell, Python 3.6.3, tcl8.6, tk8.6. Got errors when trying to run matplotlib. After copying the folders it works. Thanks alot! – pandaseal Mar 25, 2020 at 12:32

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.

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.

Copying contents of that folder worked for me. All files/folders in the tcl subfolder began with tk or tcl and none in the destination lib subfolder did - I checked that before I did the copy in case I wanted to back it out again! – Rich Aug 9, 2016 at 13:10

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

  • Go to your virtualenv: workon your-env-name
  • Edit local postactiave script with your favourite editor, for ex: gedit $VIRTUAL_ENV/bin/postactivate
  • 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

  • Restart your virtualenv: deactivate and workon your-env-name again.
  • 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.

  • 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'))
    
    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
            

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.