test_loadtk.py 1.5 KB

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