Issue #8716: Avoid crashes caused by Aqua Tk on OSX when attempting to run
test_tk or test_ttk_guionly under a username that is not currently logged
in to the console windowserver (as may be the case under buildbot or ssh).
diff --git a/Lib/test/test_tk.py b/Lib/test/test_tk.py
index ab979fa..f993c53 100644
--- a/Lib/test/test_tk.py
+++ b/Lib/test/test_tk.py
@@ -2,15 +2,11 @@
 # Skip test if _tkinter wasn't built.
 support.import_module('_tkinter')
 
-import tkinter
-from tkinter.test import runtktests
-import unittest
+# Skip test if tk cannot be initialized.
+from tkinter.test.support import check_tk_availability
+check_tk_availability()
 
-try:
-    tkinter.Button()
-except tkinter.TclError as msg:
-    # assuming tk is not available
-    raise unittest.SkipTest("tk not available: %s" % msg)
+from tkinter.test import runtktests
 
 def test_main(enable_gui=False):
     if enable_gui:
diff --git a/Lib/test/test_ttk_guionly.py b/Lib/test/test_ttk_guionly.py
index bff4fc1..b8c1a4c 100644
--- a/Lib/test/test_ttk_guionly.py
+++ b/Lib/test/test_ttk_guionly.py
@@ -5,6 +5,10 @@
 # Skip this test if _tkinter wasn't built.
 support.import_module('_tkinter')
 
+# Skip test if tk cannot be initialized.
+from tkinter.test.support import check_tk_availability
+check_tk_availability()
+
 from _tkinter import TclError
 from tkinter import ttk
 from tkinter.test import runtktests