Skip to content

Commit 2747972

Browse files
author
guilherme.polo
committed
Eliminated the need to use ttk.__loadtk__ and the problems related it.
git-svn-id: http://svn.python.org/projects/python/trunk@69404 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 91f2950 commit 2747972

2 files changed

Lines changed: 42 additions & 36 deletions

File tree

Lib/lib-tk/ttk.py

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,41 @@
2929

3030
_flatten = Tkinter._flatten
3131

32-
# Verify if Tk is new enough to not need Tile checking
32+
# Verify if Tk is new enough to not need the Tile package
3333
_REQUIRE_TILE = True if Tkinter.TkVersion < 8.5 else False
3434

35-
def _loadttk(loadtk):
36-
# This extends the default Tkinter.Tk._loadtk method so we can be
37-
# sure that ttk is available for use, or not.
38-
def _wrapper(self):
39-
loadtk(self)
40-
41-
if _REQUIRE_TILE:
42-
import os
43-
tilelib = os.environ.get('TILE_LIBRARY')
44-
if tilelib:
45-
# append custom tile path to the the list of directories that
46-
# Tcl uses when attempting to resolve packages with the package
47-
# command
48-
self.tk.eval('global auto_path; '
49-
'lappend auto_path {%s}' % tilelib)
50-
self.tk.eval('package require tile') # TclError may be raised here
51-
52-
return _wrapper
35+
def _load_tile(master):
36+
if _REQUIRE_TILE:
37+
import os
38+
tilelib = os.environ.get('TILE_LIBRARY')
39+
if tilelib:
40+
# append custom tile path to the the list of directories that
41+
# Tcl uses when attempting to resolve packages with the package
42+
# command
43+
master.tk.eval(
44+
'global auto_path; '
45+
'lappend auto_path {%s}' % tilelib)
46+
47+
master.tk.eval('package require tile') # TclError may be raised here
48+
master._tile_loaded = True
49+
50+
51+
def _setup_master(master=None):
52+
"""If master is not None, itself is returned. If master is None,
53+
the default master is returned if there is one, otherwise a new
54+
master is created and returned.
55+
56+
If it is not allowed to use the default root and master is None,
57+
RuntimeError is raised."""
58+
if master is None:
59+
if Tkinter._support_default_root:
60+
master = Tkinter._default_root or Tkinter.Tk()
61+
else:
62+
raise RuntimeError(
63+
"No master specified and Tkinter is "
64+
"configured to not support default root")
65+
return master
5366

54-
# Store the original Tkinter.Tk._loadtk before replacing it just in case
55-
# someone wants to restore it.
56-
__loadtk__ = Tkinter.Tk._loadtk
57-
Tkinter.Tk._loadtk = _loadttk(Tkinter.Tk._loadtk)
5867

5968

6069
def _format_optdict(optdict, script=False, ignore=None):
@@ -366,12 +375,11 @@ class Style(object):
366375
_name = "ttk::style"
367376

368377
def __init__(self, master=None):
369-
if master is None:
370-
if Tkinter._support_default_root:
371-
master = Tkinter._default_root or Tkinter.Tk()
372-
else:
373-
raise RuntimeError("No master specified and Tkinter is "
374-
"configured to not support default master")
378+
master = _setup_master(master)
379+
380+
if not getattr(master, '_tile_loaded', False):
381+
# Load tile now, if needed
382+
_load_tile(master)
375383

376384
self.master = master
377385
self.tk = self.master.tk
@@ -548,6 +556,10 @@ def __init__(self, master, widgetname, kw=None):
548556
active, disabled, focus, pressed, selected, background,
549557
readonly, alternate, invalid
550558
"""
559+
master = _setup_master(master)
560+
if not getattr(master, '_tile_loaded', False):
561+
# Load tile now, if needed
562+
_load_tile(master)
551563
Tkinter.Widget.__init__(self, master, widgetname, kw=kw)
552564

553565

Lib/test/test_tcl.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,9 @@
44
import os
55
import _tkinter
66
from test import test_support
7-
from Tkinter import Tk, Tcl
7+
from Tkinter import Tcl
88
from _tkinter import TclError
99

10-
# Restore Tkinter.Tk._loadtk that may have been overridden by ttk.
11-
# If this is not done then this test may fail for reasons related
12-
# to ttk only (like failing to load the tile package).
13-
from ttk import __loadtk__
14-
Tk._loadtk = __loadtk__
15-
1610

1711
class TkinterTest(unittest.TestCase):
1812

0 commit comments

Comments
 (0)