Windows下Python IDLE背景色与语法高亮主题配置、行号显示、设置清屏

Windows下Python IDLE背景色与语法高亮主题配置、行号显示、设置清屏

一、Python IDLE背景色与语法高亮主题配置

在C:\Users\admin目录下新建.idlerc文件夹,然后再在里面新建文件config-highlight.cfg,并加入如下内容:

[Tango]
definition-foreground = #fce94f
error-foreground = #fa8072
string-background = #2e3436
keyword-foreground = #8cc4ff
normal-foreground = #ffffff
comment-background = #2e3436
hit-foreground = #ffffff
break-foreground = #000000
builtin-background = #2e3436
stdout-foreground = #eeeeec
cursor-foreground = #fce94f
hit-background = #2e3436
comment-foreground = #73d216
hilite-background = #edd400
definition-background = #2e3436
stderr-background = #2e3436
break-background = #2e3436
console-foreground = #87ceeb
normal-background = #2e3436
builtin-foreground = #ad7fa8
stdout-background = #2e3436
console-background = #2e3436
stderr-foreground = #ff3e40
keyword-background = #2e3436
string-foreground = #e9b96e
hilite-foreground = #2e3436
error-background = #2e3436

[desert]
definition-foreground = #98fb98
error-foreground = #ff0000
string-background = #333333
keyword-foreground = #cc6600
normal-foreground = #f0e68c
comment-background = #333333
hit-foreground = #ffffff
break-foreground = black
builtin-background = #333333
stdout-foreground = #eeeeee
cursor-foreground = #ffcc00
hit-background = #333333
comment-foreground = #87ceeb
hilite-background = gray
definition-background = #333333
stderr-background = #333333
break-background = #ffff55
console-foreground = #87ceeb
normal-background = #333333
builtin-foreground = #519e51
stdout-background = #333333
console-background = #333333
stderr-foreground = #ff3e40
keyword-background = #333333
string-foreground = #ffa0a0
hilite-foreground = #000000
error-background = #000000

[Obsidian]
definition-foreground = #678CB1
error-foreground = #FF0000
string-background = #293134
keyword-foreground = #93C763
normal-foreground = #E0E2E4
comment-background = #293134
hit-foreground = #E0E2E4
builtin-background = #293134
stdout-foreground = #678CB1
cursor-foreground = #E0E2E4
break-background = #293134
comment-foreground = #66747B
hilite-background = #2F393C
hilite-foreground = #E0E2E4
definition-background = #293134
stderr-background = #293134
hit-background = #000000
console-foreground = #E0E2E4
normal-background = #293134
builtin-foreground = #E0E2E4
stdout-background = #293134
console-background = #293134
stderr-foreground = #FB0000
keyword-background = #293134
string-foreground = #EC7600
break-foreground = #E0E2E4
error-background = #293134

可以选择Obsidian, Desert, 和Tango三种主题配置。
重启IDLE==>Options==>Configure IDLE ==>Highlighting,如下:
Windows下Python IDLE背景色与语法高亮主题配置、行号显示、设置清屏

二、IDLE行号设置

不如不设置,比较麻烦,而且每次都要多启动一个idlex.py的文件才可以,费事。不如换另一个文本编辑器如:Notepad++、Sublime Text等。
如果一定要的话,可以参考这篇文章。不过安装时需要注意,要把整个扩展包下下来,按教程安装完成后,还要先打开,idlex-1.18\idlex-1.18\idlex.py,运行idlex.py文件,才可以显示行号。否则没有反应。

三、IDLE清屏设置

第一步 将以下代码保存为ClearWindow.py,并放入Python安装目录下D:\Jock’s Program\Python3.7\Lib\idlelib(这个路径根据自己的电脑设定)

"""

Clear Window Extension
Version: 0.1

Author: Roger D. Serwy
        [email protected]

Date: 2009-05-22

It provides "Clear Shell Window" under "Options"

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_window)

    def clear_window2(self, event): # Alternative method
        # work around the ModifiedUndoDelegator
        text = self.text
        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")

        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)

第二步 在Python安装目录下D:\Jock’s Program\Python3.7\Lib\idlelib下找到config-extensions.def,用编辑器打开,在后面加上以下代码,然后重启IDLE即可。

# Windows下清屏
[ClearWindow]
enable=1
enable_editor=0
enable_shell=1
[ClearWindow_cfgBindings]
clear-window=<Control-Key-l>

参考来源

参考一
参考二