tksupport.py 1.9 KB

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. import tkinter.messagebox as tkMessageBox
  21. import tkinter.simpledialog as tkSimpleDialog
  22. from twisted.internet import task
  23. _task = None
  24. def install(widget, ms=10, reactor=None):
  25. """Install a Tkinter.Tk() object into the reactor."""
  26. installTkFunctions()
  27. global _task
  28. _task = task.LoopingCall(widget.update)
  29. _task.start(ms / 1000.0, False)
  30. def uninstall():
  31. """Remove the root Tk widget from the reactor.
  32. Call this before destroy()ing the root widget.
  33. """
  34. global _task
  35. _task.stop()
  36. _task = None
  37. def installTkFunctions():
  38. import twisted.python.util
  39. twisted.python.util.getPassword = getPassword
  40. def getPassword(prompt="", confirm=0):
  41. while 1:
  42. try1 = tkSimpleDialog.askstring("Password Dialog", prompt, show="*")
  43. if not confirm:
  44. return try1
  45. try2 = tkSimpleDialog.askstring("Password Dialog", "Confirm Password", show="*")
  46. if try1 == try2:
  47. return try1
  48. else:
  49. tkMessageBox.showerror(
  50. "Password Mismatch", "Passwords did not match, starting over"
  51. )
  52. __all__ = ["install", "uninstall"]