[Idle-dev] Extension to handle Cancel

Tal Einat taleinat at gmail.com
Fri Aug 17 00:42:43 CEST 2007

On 8/16/07, Douglas S. Blank <dblank at brynmawr.edu> wrote:
> I'm working on a plugin extension for IDLE that can run a test when
> Control+C is pressed. Unfortunately, Control+C (or <Cancel> in Tkinter
> bindings) is not handled as other key bindings are. Instead there is a
> cancel_callback in the pyshell instance that is called.
> So, I've tried to replace the cancel_callback method from inside the
> extension (I have a working version from an external main---but it is
> really hacky). However, it looks like editwin.flist.pyshell doesn't
> exist yet when the constructor of the extension is called.
> Is there any way for my extension module to have some code that gets
> called after the pyshell has been created?
> -Doug
> Sample code:
> class ExtensionTest:
>     def __init__(self, editwin):
>        # works for other windows, after the shell is made, but:
>        print editwin.flist.pyshell.cancel_callback, "does not exist yet"
>        # when constructed for the shell

Hi Doug,

First off - Why Ctrl+C? It seems the simplest solution would be to
bind to a different hotkey, unless you deliberately want to override
the ability to use Ctrl+C to raise KeyboardInterrupt exceptions.

The answer is to just use editwin, and not flist.pyshell. When PyShell
is initialized, it will load the extension (the extensions are loaded
once per EditorWindow instance). You can set the 'enable_editor'
config option to False (or zero) for the extension to cause it to be
loaded only for the PyShell window and not for file editing windows.

If you want to be 100% sure, you can do 'isinstance(PyShell.PyShell,
editwin)' to ensure that the window is a shell window (so that things
don't go wrong if a user changes the 'enable_editor' config option).

- Tal

More information about the IDLE-dev mailing list