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

Skip to content

Commit 9580609

Browse files
committed
Require _tkinter -- don't attempt to import tkinter when _tkinter does
not exist. All 8 uses of tkinter are replaced with _tkinter. Still create a variable tkinter though, because that is used by other modules importing Tkinter (e.g. tkinter.createfilehandler()). Also added a comment to the 'import _tkinter' line saying that if this fails, Python is not configured correctly.
1 parent 10efb05 commit 9580609

2 files changed

Lines changed: 20 additions & 50 deletions

File tree

Lib/lib-tk/Tkinter.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,15 @@
22

33
__version__ = "$Revision$"
44

5-
try:
6-
# See if modern _tkinter is present
7-
import _tkinter
8-
tkinter = _tkinter # b/w compat
9-
except ImportError:
10-
# No modern _tkinter -- try oldfashioned tkinter
11-
import tkinter
12-
if hasattr(tkinter, "__path__"):
13-
import sys, os
14-
# Append standard platform specific directory
15-
p = tkinter.__path__
16-
for dir in sys.path:
17-
if (dir not in p and
18-
os.path.basename(dir) == sys.platform):
19-
p.append(dir)
20-
del sys, os, p, dir
21-
from tkinter import tkinter
22-
TclError = tkinter.TclError
5+
import _tkinter # If this fails your Python is not configured for Tk
6+
tkinter = _tkinter # b/w compat for export
7+
TclError = _tkinter.TclError
238
from types import *
249
from Tkconstants import *
2510
import string; _string = string; del string
2611

27-
TkVersion = _string.atof(tkinter.TK_VERSION)
28-
TclVersion = _string.atof(tkinter.TCL_VERSION)
12+
TkVersion = _string.atof(_tkinter.TK_VERSION)
13+
TclVersion = _string.atof(_tkinter.TCL_VERSION)
2914

3015
######################################################################
3116
# Since the values of file event masks changed from Tk 4.0 to Tk 4.1,
@@ -667,7 +652,7 @@ def __init__(self, screenName=None, baseName=None, className='Tk'):
667652
baseName = os.path.basename(sys.argv[0])
668653
baseName, ext = os.path.splitext(baseName)
669654
if ext not in ('.py', 'pyc'): baseName = baseName + ext
670-
self.tk = tkinter.create(screenName, baseName, className)
655+
self.tk = _tkinter.create(screenName, baseName, className)
671656
try:
672657
# Disable event scanning except for Command-Period
673658
import MacOS
@@ -679,15 +664,15 @@ def __init__(self, screenName=None, baseName=None, className='Tk'):
679664
self.update()
680665
# Version sanity checks
681666
tk_version = self.tk.getvar('tk_version')
682-
if tk_version != tkinter.TK_VERSION:
667+
if tk_version != _tkinter.TK_VERSION:
683668
raise RuntimeError, \
684669
"tk.h version (%s) doesn't match libtk.a version (%s)" \
685-
% (tkinter.TK_VERSION, tk_version)
670+
% (_tkinter.TK_VERSION, tk_version)
686671
tcl_version = self.tk.getvar('tcl_version')
687-
if tcl_version != tkinter.TCL_VERSION:
672+
if tcl_version != _tkinter.TCL_VERSION:
688673
raise RuntimeError, \
689674
"tcl.h version (%s) doesn't match libtcl.a version (%s)" \
690-
% (tkinter.TCL_VERSION, tcl_version)
675+
% (_tkinter.TCL_VERSION, tcl_version)
691676
if TkVersion < 4.0:
692677
raise RuntimeError, \
693678
"Tk 4.0 or higher is required; found Tk %s" \

Lib/tkinter/Tkinter.py

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,15 @@
22

33
__version__ = "$Revision$"
44

5-
try:
6-
# See if modern _tkinter is present
7-
import _tkinter
8-
tkinter = _tkinter # b/w compat
9-
except ImportError:
10-
# No modern _tkinter -- try oldfashioned tkinter
11-
import tkinter
12-
if hasattr(tkinter, "__path__"):
13-
import sys, os
14-
# Append standard platform specific directory
15-
p = tkinter.__path__
16-
for dir in sys.path:
17-
if (dir not in p and
18-
os.path.basename(dir) == sys.platform):
19-
p.append(dir)
20-
del sys, os, p, dir
21-
from tkinter import tkinter
22-
TclError = tkinter.TclError
5+
import _tkinter # If this fails your Python is not configured for Tk
6+
tkinter = _tkinter # b/w compat for export
7+
TclError = _tkinter.TclError
238
from types import *
249
from Tkconstants import *
2510
import string; _string = string; del string
2611

27-
TkVersion = _string.atof(tkinter.TK_VERSION)
28-
TclVersion = _string.atof(tkinter.TCL_VERSION)
12+
TkVersion = _string.atof(_tkinter.TK_VERSION)
13+
TclVersion = _string.atof(_tkinter.TCL_VERSION)
2914

3015
######################################################################
3116
# Since the values of file event masks changed from Tk 4.0 to Tk 4.1,
@@ -667,7 +652,7 @@ def __init__(self, screenName=None, baseName=None, className='Tk'):
667652
baseName = os.path.basename(sys.argv[0])
668653
baseName, ext = os.path.splitext(baseName)
669654
if ext not in ('.py', 'pyc'): baseName = baseName + ext
670-
self.tk = tkinter.create(screenName, baseName, className)
655+
self.tk = _tkinter.create(screenName, baseName, className)
671656
try:
672657
# Disable event scanning except for Command-Period
673658
import MacOS
@@ -679,15 +664,15 @@ def __init__(self, screenName=None, baseName=None, className='Tk'):
679664
self.update()
680665
# Version sanity checks
681666
tk_version = self.tk.getvar('tk_version')
682-
if tk_version != tkinter.TK_VERSION:
667+
if tk_version != _tkinter.TK_VERSION:
683668
raise RuntimeError, \
684669
"tk.h version (%s) doesn't match libtk.a version (%s)" \
685-
% (tkinter.TK_VERSION, tk_version)
670+
% (_tkinter.TK_VERSION, tk_version)
686671
tcl_version = self.tk.getvar('tcl_version')
687-
if tcl_version != tkinter.TCL_VERSION:
672+
if tcl_version != _tkinter.TCL_VERSION:
688673
raise RuntimeError, \
689674
"tcl.h version (%s) doesn't match libtcl.a version (%s)" \
690-
% (tkinter.TCL_VERSION, tcl_version)
675+
% (_tkinter.TCL_VERSION, tcl_version)
691676
if TkVersion < 4.0:
692677
raise RuntimeError, \
693678
"Tk 4.0 or higher is required; found Tk %s" \

0 commit comments

Comments
 (0)