天天看點

設定python下的IDLE清屏快捷鍵(windows)

設定python下的IDLE清屏快捷鍵(windows)

最近在學習python的時候遇到一個頭疼的問題,就是在IDLE下程式設計的時候不能像windows或者Linux的終端一樣,”Ctrl+l”的快捷鍵實作清屏的操作,于是在網上找了一下資料,最後在知乎上發現最好的方法是需要自己配置,其中我做了些許修改,具體的過程如下:

1、配置檔案内容:

"""

ClearWindow Extension

Version:0.2

Author:Roger D. Serwy

        [email protected]

Date:2009-06-14

Itprovides "Clear Shell Window" under "Options"

withability to undo.

Add theselines to config-extensions.def

[ClearWindow]

enable=1

enable_editor=0

enable_shell=1

[ClearWindow_cfgBindings]

clear-window=<Control-Key-l>

"""

classClearWindow:

    menudefs = [

        ('options', [None,

               ('ClearShell 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)

        # fixiomark 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

        # workaround 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()

        ifself.text.compare('insert', '<', 'iomark'):

            self.text.mark_set('insert', 'end-1c')

        self.editwin.set_line_and_column()

    def clear_window(self,event):

        # removeundo delegator

        undo = self.editwin.undo

        self.editwin.per.removefilter(undo)

        # clearthe window, but preserve current command

        self.text.delete(1.0, "iomark linestart")

        ifself.text.compare('insert', '<', 'iomark'):

            self.text.mark_set('insert', 'end-1c')

        self.editwin.set_line_and_column()

        # restoreundo delegator

        self.editwin.per.insertfilter(undo)

2、拷貝以上内容,記住命名為ClearWindow.py放在Python安裝目錄Python XXX\Lib\idlelib下面(XXX為你的python版本)。 

3、記事本打開Python XXX\Lib\idlelib目錄下的config-extensions.def(IDLE擴充的配置檔案), 為防止出錯,你可以在打開它之前先copy一個備份 。 

4、修改config-extensions.def ,在末尾添加如下内容,然後儲存退出:

[ClearWindow]

enable=1

enable_editor=0

enable_shell=1

[ClearWindow_cfgBindings]

clear-window=<Control-Key-;>

//可以直接設定為<Control-Key-l>即“Ctrl+l”快捷鍵

5、打開Python的IDLE,options選項中就可以看到增加了Clear shell window ctrl+;。 

6、在IDLE輸入代碼,然後按Ctrl+l(是指Ctrl和l),發現剛輸入代碼可以被清除了。

說明:

快捷鍵Ctrl+;,可修改成其他鍵,将“clear-window=<Control-Key-;>”裡的Control和;修改成其他鍵即可。經試驗,Control換成Alter時,會啟動不了IDLE,換成Shift時,快捷鍵不起作用,因為Shift+l,直接變成輸入l了。

注:這裡有個很坑的地方就是下載下傳儲存的配置檔案名一定要與在config-extensions.def中添加的[ClearWindow]内容中的名字一緻,要不然找不到配置檔案的,注意大小寫!!!