Inputhook for Tk. Run the Tk eventloop until prompt-toolkit needs to process the next input.
(inputhook_context)
| 44 | import tkinter |
| 45 | |
| 46 | def inputhook(inputhook_context): |
| 47 | """ |
| 48 | Inputhook for Tk. |
| 49 | Run the Tk eventloop until prompt-toolkit needs to process the next input. |
| 50 | """ |
| 51 | # Get the current TK application. |
| 52 | root = tkinter._default_root |
| 53 | |
| 54 | def wait_using_filehandler(): |
| 55 | """ |
| 56 | Run the TK eventloop until the file handler that we got from the |
| 57 | inputhook becomes readable. |
| 58 | """ |
| 59 | # Add a handler that sets the stop flag when `prompt-toolkit` has input |
| 60 | # to process. |
| 61 | stop = [False] |
| 62 | def done(*a): |
| 63 | stop[0] = True |
| 64 | |
| 65 | root.createfilehandler(inputhook_context.fileno(), _tkinter.READABLE, done) |
| 66 | |
| 67 | # Run the TK event loop as long as we don't receive input. |
| 68 | while root.dooneevent(_tkinter.ALL_EVENTS): |
| 69 | if stop[0]: |
| 70 | break |
| 71 | |
| 72 | root.deletefilehandler(inputhook_context.fileno()) |
| 73 | |
| 74 | def wait_using_polling(): |
| 75 | """ |
| 76 | Windows TK doesn't support 'createfilehandler'. |
| 77 | So, run the TK eventloop and poll until input is ready. |
| 78 | """ |
| 79 | while not inputhook_context.input_is_ready(): |
| 80 | while root.dooneevent(_tkinter.ALL_EVENTS | _tkinter.DONT_WAIT): |
| 81 | pass |
| 82 | # Sleep to make the CPU idle, but not too long, so that the UI |
| 83 | # stays responsive. |
| 84 | time.sleep(.01) |
| 85 | |
| 86 | if root is not None: |
| 87 | if hasattr(root, 'createfilehandler'): |
| 88 | wait_using_filehandler() |
| 89 | else: |
| 90 | wait_using_polling() |
nothing calls this directly
no test coverage detected