Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Tkinter throwing Tcl error

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.

like image 812
charan Avatar asked Mar 28 '15 17:03

charan


People also ask

What is TCL error in Python?

You may see error messages written to the shell when you close the game window. This is because when you close the window, the code is breaking out of the while loop, and Python is complaining about it. The is exactly the error you are seeing when you run that code and then close the window.

What are TCL Tk and tkinter in Python?

Tkinter is the Python interface to Tk, which is the GUI toolkit for Tcl/Tk. Tcl (pronounced as tickle) is a scripting language often used in testing, prototyping, and GUI development. Tk is an open-source, cross-platform widget toolkit used by many different programming languages to build GUI programs.

What is the difference between TCL and Tk?

Tcl is a general purpose multi-paradigm system programming language. It is a scripting language that aims at providing the ability for applications to communicate with each other. On the other hand, Tk is a cross platform widget toolkit used for building GUI in many languages.

What does Tk () do in tkinter?

In order to create a tkinter application, we generally create an instance of tkinter frame, i.e., Tk(). It helps to display the root window and manages all the other components of the tkinter application. We can initialize the tkinter instance by assigning the variable to it.


2 Answers

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.

like image 143
Brian A Avatar answered Sep 23 '22 01:09

Brian A


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

tcl8.5 and tk8.5

like image 28
Kudehinbu Oluwaponle Avatar answered Sep 22 '22 01:09

Kudehinbu Oluwaponle