|
29 | 29 |
|
30 | 30 | _flatten = Tkinter._flatten |
31 | 31 |
|
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 |
33 | 33 | _REQUIRE_TILE = True if Tkinter.TkVersion < 8.5 else False |
34 | 34 |
|
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 |
53 | 66 |
|
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) |
58 | 67 |
|
59 | 68 |
|
60 | 69 | def _format_optdict(optdict, script=False, ignore=None): |
@@ -366,12 +375,11 @@ class Style(object): |
366 | 375 | _name = "ttk::style" |
367 | 376 |
|
368 | 377 | 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) |
375 | 383 |
|
376 | 384 | self.master = master |
377 | 385 | self.tk = self.master.tk |
@@ -548,6 +556,10 @@ def __init__(self, master, widgetname, kw=None): |
548 | 556 | active, disabled, focus, pressed, selected, background, |
549 | 557 | readonly, alternate, invalid |
550 | 558 | """ |
| 559 | + master = _setup_master(master) |
| 560 | + if not getattr(master, '_tile_loaded', False): |
| 561 | + # Load tile now, if needed |
| 562 | + _load_tile(master) |
551 | 563 | Tkinter.Widget.__init__(self, master, widgetname, kw=kw) |
552 | 564 |
|
553 | 565 |
|
|
0 commit comments