Thanks to visit codestin.com
Credit goes to github.com

Skip to content

RuntimeError when trying to call input inside key_press_event. #10061

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
paulbrodersen opened this issue Dec 20, 2017 · 3 comments
Closed

RuntimeError when trying to call input inside key_press_event. #10061

paulbrodersen opened this issue Dec 20, 2017 · 3 comments

Comments

@paulbrodersen
Copy link

Bug report

Bug summary

Calling input inside a key_press_event results either in a RuntimeError or causes the terminal and canvas to freeze. Presumably, this is because the input to input triggers further on_key_press calls (although this issue also appears to happen when the figure is not in focus so I am actually not sure if that is the reason). I am looking for a work around.

Code for reproduction

import matplotlib.pyplot as plt

plt.ion()

fig, ax = plt.subplots()

def on_key_press(event):
    if event.key == 'enter':
        ans = input('Provide some text to print and press enter:\n')
        # ans = 'foo'
        ax.text(event.xdata, event.ydata, ans)
        fig.canvas.draw_idle()

cid = fig.canvas.mpl_connect('key_press_event', on_key_press)

Actual outcome

Traceback (most recent call last):
  File "/home/paul/.virtualenvs/test3_netgraph/lib/python3.5/site-packages/matplotlib/cbook/__init__.py", line 389, in process
    proxy(*args, **kwargs)
  File "/home/paul/.virtualenvs/test3_netgraph/lib/python3.5/site-packages/matplotlib/cbook/__init__.py", line 227, in __call__
    return mtd(*args, **kwargs)
  File "/home/paul/src/netgraph/netgraph/input_block.py", line 16, in on_key_press
    ans = input('Provide some text to print and press enter:\n')
RuntimeError: can't re-enter readline

Expected outcome

The expected outcome can be simulated by commenting out the input call and uncommenting the ans = 'foo' line in the MWE.

Matplotlib version

  • Operating system: Ubuntu 17.04
  • Matplotlib version: 2.1.0
  • Matplotlib backend (print(matplotlib.get_backend())): TkAgg
  • Python version: 3.5.3
  • Jupyter version (if applicable): na
  • Other libraries: numpy, ipython and their dependencies

Installed everything via pip.

@tacaswell
Copy link
Member

If you are doing this in a terminal, you are using readline for the prompt and it is sitting there waiting for the user to type. input is also going to use readline to try and get user input. When you hit a key, Tk fires an event (in c) that eventually ends up calling your python function (from inside the tk event loop). You now have two places in your code trying use readline in the same process which is slightly un-defined (when you type into the terminal does your input get the text or the python repl (or both?!)). The exception is coming from a c-extension in the cpython source:

https://github.com/python/cpython/blob/0a37a30037073a4a9ba45e560c8445048e5f2ba2/Parser/myreadline.c#L298-L302

Sorry this is a very rambling answer.

@paulbrodersen
Copy link
Author

Thanks a lot for the response. I figured that it would be something along the lines that you described but it is good to know the details. Do you have any suggestions how to circumvent the issue? The only thing that I could come up with is to temporarily disconnect on_key_press inside on_key_press. But that does not seem to be possible, as far as I can tell.

@timhoffm
Copy link
Member

AFAICS there's no simple solution. In any case, this is not actionable from the Matplotlib side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants