How do I make this Python extension work on windows (clear IDLE)?

597 views Asked by At

http://bugs.python.org/issue6143

I know something similar has been asked a thousand times, which to me indicates that the issue could do with being addressed by the designers.

I have searched and searched and finally found a solution that looks viable (see link above), but I am a complete beginner with Python and need help with how to make it work. I've tried adding the code in the comments section to the config-extensions.def file, and also running the file from IDLE, but no "clear screen" option appears on any menu, nor ctrl+l keyboard shortcut. BTW I'm using python 3.4 on windows.

I'm actually very surprised and even shocked that this functionality is not included as standard for this reason: Python is often recommended as a beginners' language and beginners (including me and also the students I intend to teach this language to once I've learned it) get OVERWHELMED by lots of not-understood text on the screen (syntax errors etc.) What we need as probably one of the most important features is some kind of "panic button" to clear away the mess and let us try again with a blank slate. Closing the application an re-opening it is serious overkill so I can't fathom why this hasn't been considered. If anyone knows who to contact to get this changed I think is is well worth emphasizing the importance of this issue, since it could really be a deal breaker in choosing a language for educational purposes.

However, my main concern here is to get some help with making this work so I can get on and have some fun programming! Any help much appreciated.

1

There are 1 answers

0
Robin Andrews On BEST ANSWER

Ok, I found the answer to this (and it took two fairly computer literate people a while to work out):

Put the following code from the comments section of ClearWindow.py into config-extensions.def (C:\Python34\Lib\idlelib...)

[ClearWindow]
enable=1
enable_editor=0
enable_shell=1
[ClearWindow_cfgBindings]
clear-window=<Control-Key-l>

Next, in the same folder (C:\Python34\Lib\idlelib) put the whole ClearWindow.py file. Ta Da! File pasted below for convenience:

"""

Clear Window Extension
Version: 0.2

Author: Roger D. Serwy
        [email protected]

Date: 2009-06-14

It provides "Clear Shell Window" under "Options"
with ability to undo.

Add these lines to config-extensions.def

[ClearWindow]
enable=1
enable_editor=0
enable_shell=1
[ClearWindow_cfgBindings]
clear-window=<Control-Key-l>


"""

class ClearWindow:

    menudefs = [
        ('options', [None,
               ('Clear Shell Window', '<<clear-window>>'),
       ]),]

    def __init__(self, editwin):
        self.editwin = editwin
        self.text = self.editwin.text
        self.text.bind("<<clear-window>>", self.clear_window2)

        self.text.bind("<<undo>>", self.undo_event)  # add="+" doesn't work

    def undo_event(self, event):
        text = self.text

        text.mark_set("iomark2", "iomark")
        text.mark_set("insert2", "insert")
        self.editwin.undo.undo_event(event)

        # fix iomark and insert
        text.mark_set("iomark", "iomark2")
        text.mark_set("insert", "insert2")
        text.mark_unset("iomark2")
        text.mark_unset("insert2")


    def clear_window2(self, event): # Alternative method
        # work around the ModifiedUndoDelegator
        text = self.text
        text.undo_block_start()
        text.mark_set("iomark2", "iomark")
        text.mark_set("iomark", 1.0)
        text.delete(1.0, "iomark2 linestart")
        text.mark_set("iomark", "iomark2")
        text.mark_unset("iomark2")
        text.undo_block_stop()
        if self.text.compare('insert', '<', 'iomark'):
            self.text.mark_set('insert', 'end-1c')
        self.editwin.set_line_and_column()

    def clear_window(self, event):
        # remove undo delegator
        undo = self.editwin.undo
        self.editwin.per.removefilter(undo)

        # clear the window, but preserve current command
        self.text.delete(1.0, "iomark linestart")
        if self.text.compare('insert', '<', 'iomark'):
            self.text.mark_set('insert', 'end-1c')
        self.editwin.set_line_and_column()

        # restore undo delegator
        self.editwin.per.insertfilter(undo)