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