You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

tksupport.py 2.0KB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. This module integrates Tkinter with twisted.internet's mainloop.
  5. Maintainer: Itamar Shtull-Trauring
  6. To use, do::
  7. | tksupport.install(rootWidget)
  8. and then run your reactor as usual - do *not* call Tk's mainloop(),
  9. use Twisted's regular mechanism for running the event loop.
  10. Likewise, to stop your program you will need to stop Twisted's
  11. event loop. For example, if you want closing your root widget to
  12. stop Twisted::
  13. | root.protocol('WM_DELETE_WINDOW', reactor.stop)
  14. When using Aqua Tcl/Tk on macOS the standard Quit menu item in
  15. your application might become unresponsive without the additional
  16. fix::
  17. | root.createcommand("::tk::mac::Quit", reactor.stop)
  18. @see: U{Tcl/TkAqua FAQ for more info<http://wiki.tcl.tk/12987>}
  19. """
  20. from twisted.internet import task
  21. from twisted.python.compat import _PY3
  22. if _PY3:
  23. import tkinter.simpledialog as tkSimpleDialog
  24. import tkinter.messagebox as tkMessageBox
  25. else:
  26. import tkSimpleDialog, tkMessageBox
  27. _task = None
  28. def install(widget, ms=10, reactor=None):
  29. """Install a Tkinter.Tk() object into the reactor."""
  30. installTkFunctions()
  31. global _task
  32. _task = task.LoopingCall(widget.update)
  33. _task.start(ms / 1000.0, False)
  34. def uninstall():
  35. """Remove the root Tk widget from the reactor.
  36. Call this before destroy()ing the root widget.
  37. """
  38. global _task
  39. _task.stop()
  40. _task = None
  41. def installTkFunctions():
  42. import twisted.python.util
  43. twisted.python.util.getPassword = getPassword
  44. def getPassword(prompt = '', confirm = 0):
  45. while 1:
  46. try1 = tkSimpleDialog.askstring('Password Dialog', prompt, show='*')
  47. if not confirm:
  48. return try1
  49. try2 = tkSimpleDialog.askstring('Password Dialog', 'Confirm Password', show='*')
  50. if try1 == try2:
  51. return try1
  52. else:
  53. tkMessageBox.showerror('Password Mismatch', 'Passwords did not match, starting over')
  54. __all__ = ["install", "uninstall"]