Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f8d579c

Browse files
committed
This hopefully fixes the problem of having to set PATH
in autoexec.bat in order to find the Tcl DLLs -- Tkinter calls FixTk which will hunt around in a few common places and then set PATH and try again, or else issue a big clarifying error message.
1 parent 8830319 commit f8d579c

2 files changed

Lines changed: 68 additions & 1 deletion

File tree

Lib/lib-tk/FixTk.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""Utility which tries to locate the Tcl/Tk 8.0 DLLs on Windows.
2+
3+
This is a no-op on other platforms.
4+
"""
5+
6+
# Error messages we may spit out
7+
8+
NO_TCL_MESSAGE = """\
9+
WHOOPS! I can't find a Tcl/Tk 8.0 installation anywhere.
10+
Please make sure that Tcl.Tk 8.0 is installed and that the PATH
11+
environment variable is set to include the Tcl/bin directory
12+
(or wherever TK80.DLL and TCL80.DLL are installed).
13+
If you don't know how to fix this, consider searching the Python FAQ
14+
for the error you get; post to the comp.lang.python if all else fails.
15+
Read the source file FixTk.py for details.
16+
"""
17+
18+
NO_TKINTER_MESSAGE = """\
19+
WHOOPS! Even though I think I have found a Tcl/Tk 8.0 installation,
20+
I can't seem to import the _tkinter extension module.
21+
I get the following exception:
22+
ImportError: %s
23+
If you don't know how to fix this, consider searching the Python FAQ
24+
for the error you get; post to the comp.lang.python if all else fails.
25+
Read the source file FixTk.py for details.
26+
"""
27+
28+
import sys
29+
if sys.platform == "win32":
30+
try:
31+
import _tkinter
32+
except ImportError:
33+
import os
34+
try:
35+
path = os.environ['PATH']
36+
except KeyError:
37+
path = ""
38+
python_exe = sys.executable
39+
python_dir = os.path.dirname(python_exe)
40+
program_files = os.path.dirname(python_dir)
41+
def tclcheck(dir):
42+
for dll in "tcl80.dll", "tk80.dll", "tclpip80.dll":
43+
if not os.path.isfile(os.path.join(dir, dll)):
44+
return 0
45+
return 1
46+
for tcldir in [program_files, "\\Program files", "\\",
47+
"C:\\Program Files", "D:\\Program Files"]:
48+
tcldir = os.path.join(tcldir, "Tcl", "bin")
49+
if tclcheck(tcldir):
50+
break
51+
else:
52+
tcldir = None
53+
if not tcldir:
54+
sys.stderr.write(NO_TCL_MESSAGE)
55+
else:
56+
if path and path[-1] != os.pathsep:
57+
path = path + os.pathsep
58+
path = path + tcldir
59+
os.environ["PATH"] = path
60+
os.putenv("PATH", path)
61+
try:
62+
import _tkinter
63+
except ImportError, message:
64+
sys.stderr.write(NO_TKINTER_MESSAGE % str(message))

Lib/lib-tk/Tkinter.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
__version__ = "$Revision$"
44

5-
import _tkinter # If this fails your Python is not configured for Tk
5+
import sys
6+
if sys.platform == "win32":
7+
import FixTk # Attempt to configure Tcl/Tk without requiring PATH
8+
import _tkinter # If this fails your Python may not be configured for Tk
69
tkinter = _tkinter # b/w compat for export
710
TclError = _tkinter.TclError
811
from types import *

0 commit comments

Comments
 (0)