MCPcopy Create free account
hub / github.com/ipython/ipython / inputhook

Function inputhook

IPython/terminal/pt_inputhooks/tk.py:46–90  ·  view source on GitHub ↗

Inputhook for Tk. Run the Tk eventloop until prompt-toolkit needs to process the next input.

(inputhook_context)

Source from the content-addressed store, hash-verified

44import tkinter
45
46def 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()

Callers

nothing calls this directly

Calls 2

wait_using_filehandlerFunction · 0.85
wait_using_pollingFunction · 0.85

Tested by

no test coverage detected