mirror of
https://github.com/python/cpython.git
synced 2025-07-31 07:04:42 +00:00
Eliminated the need to use ttk.__loadtk__ and the problems related it.
This commit is contained in:
parent
92c3b2190b
commit
8e5e438d21
2 changed files with 40 additions and 34 deletions
|
@ -29,32 +29,41 @@ import Tkinter
|
||||||
|
|
||||||
_flatten = Tkinter._flatten
|
_flatten = Tkinter._flatten
|
||||||
|
|
||||||
# Verify if Tk is new enough to not need Tile checking
|
# Verify if Tk is new enough to not need the Tile package
|
||||||
_REQUIRE_TILE = True if Tkinter.TkVersion < 8.5 else False
|
_REQUIRE_TILE = True if Tkinter.TkVersion < 8.5 else False
|
||||||
|
|
||||||
def _loadttk(loadtk):
|
def _load_tile(master):
|
||||||
# This extends the default Tkinter.Tk._loadtk method so we can be
|
if _REQUIRE_TILE:
|
||||||
# sure that ttk is available for use, or not.
|
import os
|
||||||
def _wrapper(self):
|
tilelib = os.environ.get('TILE_LIBRARY')
|
||||||
loadtk(self)
|
if tilelib:
|
||||||
|
# append custom tile path to the the list of directories that
|
||||||
|
# Tcl uses when attempting to resolve packages with the package
|
||||||
|
# command
|
||||||
|
master.tk.eval(
|
||||||
|
'global auto_path; '
|
||||||
|
'lappend auto_path {%s}' % tilelib)
|
||||||
|
|
||||||
if _REQUIRE_TILE:
|
master.tk.eval('package require tile') # TclError may be raised here
|
||||||
import os
|
master._tile_loaded = True
|
||||||
tilelib = os.environ.get('TILE_LIBRARY')
|
|
||||||
if tilelib:
|
|
||||||
# append custom tile path to the the list of directories that
|
|
||||||
# Tcl uses when attempting to resolve packages with the package
|
|
||||||
# command
|
|
||||||
self.tk.eval('global auto_path; '
|
|
||||||
'lappend auto_path {%s}' % tilelib)
|
|
||||||
self.tk.eval('package require tile') # TclError may be raised here
|
|
||||||
|
|
||||||
return _wrapper
|
|
||||||
|
|
||||||
# Store the original Tkinter.Tk._loadtk before replacing it just in case
|
def _setup_master(master=None):
|
||||||
# someone wants to restore it.
|
"""If master is not None, itself is returned. If master is None,
|
||||||
__loadtk__ = Tkinter.Tk._loadtk
|
the default master is returned if there is one, otherwise a new
|
||||||
Tkinter.Tk._loadtk = _loadttk(Tkinter.Tk._loadtk)
|
master is created and returned.
|
||||||
|
|
||||||
|
If it is not allowed to use the default root and master is None,
|
||||||
|
RuntimeError is raised."""
|
||||||
|
if master is None:
|
||||||
|
if Tkinter._support_default_root:
|
||||||
|
master = Tkinter._default_root or Tkinter.Tk()
|
||||||
|
else:
|
||||||
|
raise RuntimeError(
|
||||||
|
"No master specified and Tkinter is "
|
||||||
|
"configured to not support default root")
|
||||||
|
return master
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _format_optdict(optdict, script=False, ignore=None):
|
def _format_optdict(optdict, script=False, ignore=None):
|
||||||
|
@ -366,12 +375,11 @@ class Style(object):
|
||||||
_name = "ttk::style"
|
_name = "ttk::style"
|
||||||
|
|
||||||
def __init__(self, master=None):
|
def __init__(self, master=None):
|
||||||
if master is None:
|
master = _setup_master(master)
|
||||||
if Tkinter._support_default_root:
|
|
||||||
master = Tkinter._default_root or Tkinter.Tk()
|
if not getattr(master, '_tile_loaded', False):
|
||||||
else:
|
# Load tile now, if needed
|
||||||
raise RuntimeError("No master specified and Tkinter is "
|
_load_tile(master)
|
||||||
"configured to not support default master")
|
|
||||||
|
|
||||||
self.master = master
|
self.master = master
|
||||||
self.tk = self.master.tk
|
self.tk = self.master.tk
|
||||||
|
@ -548,6 +556,10 @@ class Widget(Tkinter.Widget):
|
||||||
active, disabled, focus, pressed, selected, background,
|
active, disabled, focus, pressed, selected, background,
|
||||||
readonly, alternate, invalid
|
readonly, alternate, invalid
|
||||||
"""
|
"""
|
||||||
|
master = _setup_master(master)
|
||||||
|
if not getattr(master, '_tile_loaded', False):
|
||||||
|
# Load tile now, if needed
|
||||||
|
_load_tile(master)
|
||||||
Tkinter.Widget.__init__(self, master, widgetname, kw=kw)
|
Tkinter.Widget.__init__(self, master, widgetname, kw=kw)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -4,15 +4,9 @@ import unittest
|
||||||
import os
|
import os
|
||||||
import _tkinter
|
import _tkinter
|
||||||
from test import test_support
|
from test import test_support
|
||||||
from Tkinter import Tk, Tcl
|
from Tkinter import Tcl
|
||||||
from _tkinter import TclError
|
from _tkinter import TclError
|
||||||
|
|
||||||
# Restore Tkinter.Tk._loadtk that may have been overridden by ttk.
|
|
||||||
# If this is not done then this test may fail for reasons related
|
|
||||||
# to ttk only (like failing to load the tile package).
|
|
||||||
from ttk import __loadtk__
|
|
||||||
Tk._loadtk = __loadtk__
|
|
||||||
|
|
||||||
|
|
||||||
class TkinterTest(unittest.TestCase):
|
class TkinterTest(unittest.TestCase):
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue