diff --git a/Lib/tkinter/__init__.py b/Lib/tkinter/__init__.py index 3bfeb7a017..ca8a124bb6 100644 --- a/Lib/tkinter/__init__.py +++ b/Lib/tkinter/__init__.py @@ -143,6 +143,22 @@ def _splitdict(tk, v, cut_minus=True, conv=None): dict[key] = value return dict +def _setup_master(master=None): + """If master is not None, itself is returned. If master is None, + the default master is returned if there is one, otherwise a new + 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 _support_default_root: + master = _default_root or Tk() + else: + raise RuntimeError( + "No master specified and tkinter is " + "configured to not support default root") + return master + class EventType(enum.StrEnum): KeyPress = '2' @@ -329,8 +345,7 @@ def __init__(self, master=None, value=None, name=None): if name is not None and not isinstance(name, str): raise TypeError("name must be a string") global _varnum - if not master: - master = _default_root + master = _setup_master(master) self._root = master._root() self._tk = master.tk if name: @@ -591,7 +606,7 @@ def get(self): def mainloop(n=0): """Run the main loop of Tcl.""" - _default_root.tk.mainloop(n) + _setup_master().tk.mainloop(n) getint = int @@ -602,7 +617,7 @@ def mainloop(n=0): def getboolean(s): """Convert true and false to integer values 1 and 0.""" try: - return _default_root.tk.getboolean(s) + return _setup_master().tk.getboolean(s) except TclError: raise ValueError("invalid literal for getboolean()") @@ -2521,13 +2536,7 @@ class BaseWidget(Misc): def _setup(self, master, cnf): """Internal function. Sets up information about children.""" - if _support_default_root: - global _default_root - if not master: - if not _default_root: - _default_root = Tk() - master = _default_root - self.master = master + self.master = master = _setup_master(master) self.tk = master.tk name = None if 'name' in cnf: @@ -4146,11 +4155,13 @@ def __init__(self, name=None, cnf={}, master=None, **kw): def image_names(): - return _default_root.tk.splitlist(_default_root.tk.call('image', 'names')) + master = _setup_master() + return master.tk.splitlist(master.tk.call('image', 'names')) def image_types(): - return _default_root.tk.splitlist(_default_root.tk.call('image', 'types')) + master = _setup_master() + return master.tk.splitlist(master.tk.call('image', 'types')) class Spinbox(Widget, XView): diff --git a/Lib/tkinter/ttk.py b/Lib/tkinter/ttk.py index 968fd54dce..db14131520 100644 --- a/Lib/tkinter/ttk.py +++ b/Lib/tkinter/ttk.py @@ -27,6 +27,7 @@ import tkinter from tkinter import _flatten, _join, _stringify, _splitdict +from tkinter import _setup_master as setup_master # Verify if Tk is new enough to not need the Tile package _REQUIRE_TILE = True if tkinter.TkVersion < 8.5 else False @@ -341,22 +342,6 @@ def tclobjs_to_py(adict): return adict -def setup_master(master=None): - """If master is not None, itself is returned. If master is None, - the default master is returned if there is one, otherwise a new - 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 - class Style(object): """Manipulate style database."""