Issue #27239: idlelib.macosx.isXyzTk functions initialize as needed.
diff --git a/Lib/idlelib/macosx.py b/Lib/idlelib/macosx.py
index 9d75631..4e4dcd6 100644
--- a/Lib/idlelib/macosx.py
+++ b/Lib/idlelib/macosx.py
@@ -7,13 +7,14 @@
 
 _tk_type = None
 
-def _initializeTkVariantTests(root):
+def _init_tk_type(idleroot=None):
     """
     Initializes OS X Tk variant values for
     isAquaTk(), isCarbonTk(), isCocoaTk(), and isXQuartz().
     """
     global _tk_type
     if sys.platform == 'darwin':
+        root = idleroot or tkinter.Tk()
         ws = root.tk.call('tk', 'windowingsystem')
         if 'x11' in ws:
             _tk_type = "xquartz"
@@ -23,6 +24,8 @@
             _tk_type = "cocoa"
         else:
             _tk_type = "carbon"
+        if not idleroot:
+            root.destroy
     else:
         _tk_type = "other"
 
@@ -30,7 +33,8 @@
     """
     Returns True if IDLE is using a native OS X Tk (Cocoa or Carbon).
     """
-    assert _tk_type is not None
+    if not _tk_type:
+        _init_tk_type()
     return _tk_type == "cocoa" or _tk_type == "carbon"
 
 def isCarbonTk():
@@ -38,21 +42,24 @@
     Returns True if IDLE is using a Carbon Aqua Tk (instead of the
     newer Cocoa Aqua Tk).
     """
-    assert _tk_type is not None
+    if not _tk_type:
+        _init_tk_type()
     return _tk_type == "carbon"
 
 def isCocoaTk():
     """
     Returns True if IDLE is using a Cocoa Aqua Tk.
     """
-    assert _tk_type is not None
+    if not _tk_type:
+        _init_tk_type()
     return _tk_type == "cocoa"
 
 def isXQuartz():
     """
     Returns True if IDLE is using an OS X X11 Tk.
     """
-    assert _tk_type is not None
+    if not _tk_type:
+        _init_tk_type()
     return _tk_type == "xquartz"
 
 def tkVersionWarning(root):
@@ -232,7 +239,7 @@
     isAquaTk(), isCarbonTk(), isCocoaTk(), isXQuartz() functions which
     are initialized here as well.
     """
-    _initializeTkVariantTests(root)
+    _init_tk_type(root)
     if isAquaTk():
         hideTkConsole(root)
         overrideRootMenu(root, flist)