1import os 2import sys 3import unittest 4import test.support as test_support 5from test.support import os_helper 6from tkinter import Tcl, TclError 7 8test_support.requires('gui') 9 10class TkLoadTest(unittest.TestCase): 11 12 @unittest.skipIf('DISPLAY' not in os.environ, 'No $DISPLAY set.') 13 def testLoadTk(self): 14 tcl = Tcl() 15 self.assertRaises(TclError,tcl.winfo_geometry) 16 tcl.loadtk() 17 self.assertEqual('1x1+0+0', tcl.winfo_geometry()) 18 tcl.destroy() 19 20 def testLoadTkFailure(self): 21 old_display = None 22 if sys.platform.startswith(('win', 'darwin', 'cygwin')): 23 # no failure possible on windows? 24 25 # XXX Maybe on tk older than 8.4.13 it would be possible, 26 # see tkinter.h. 27 return 28 with os_helper.EnvironmentVarGuard() as env: 29 if 'DISPLAY' in os.environ: 30 del env['DISPLAY'] 31 # on some platforms, deleting environment variables 32 # doesn't actually carry through to the process level 33 # because they don't support unsetenv 34 # If that's the case, abort. 35 with os.popen('echo $DISPLAY') as pipe: 36 display = pipe.read().strip() 37 if display: 38 return 39 40 tcl = Tcl() 41 self.assertRaises(TclError, tcl.winfo_geometry) 42 self.assertRaises(TclError, tcl.loadtk) 43 44 45if __name__ == "__main__": 46 unittest.main() 47