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