IDLE是Python自带的IDE开发环境,也是Python学习者使用最多的代码编辑器。但是IDLE默认的配置、代码配色及语法高亮主题确实用起来很不舒服。作为一个强迫症患者小编初学Python的时候花了很长时间才把IDLE调教成自己喜欢的样子。下面小编就和大家交流一下自己的IDLE设置。
修改IDLE配色和主题
【第一步】
首先要找到名为config-highlight.cfg的文件,这个文件位于哪里呢?我列了一份可以找到它的路径清单:
在Linux系列系统下路径为(~表示用户目录):
~/.idlerc/
在Windows XP下路径为:
C:\Documents and Settings\<用户名>\.idlerc\
在Windows 7下路径为:
C:\Users\<用户名>\.idlerc\
对于Windows可以直接打开开始运行或者在地址栏输入下面的路径确认即可:
%USERPROFILE%\.idlerc\
【第二步】
找到config-highlight.cfg文件后接下来就需要编辑它了,如果文件夹里没有这个文件我们可以新创建一个config-highlight.cfg。
当然习惯使用命令行的也可以使用下面的命令,在Linux系统下:
# for Linux
vi ~/.idlerc/config-highlight.cfg
在Windows系统下:
notepad %USERPROFILE%\.idlerc\config-highlight.cfg
现在我们应该已经打开config-highlight.cfg这个文件并做好编辑的准备了,比如说我可以通过修改这个文件为下面内容来实现两个名为Obsidian和tango的代码高亮主题
[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
[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
【第三步】
我们修改了config-highlight.cfg这个文件并不意味着事情结束了,我们还需要在Python IDLE中选择我们的主题,这时如果已经打开Python IDLE,请保存必要文件并重新开启IDLE,开启后依次选择菜单上Options – Configure IDLE…
选择Highlighting选项卡,然后Select选择a Custom Theme
完成配置后点击Apply或者OK,看看效果吧。
添加清屏功能
在学习和使用python的过程中,少不了要与Python IDLE打交道。但使用 Python IDLE 都会遇到一个常见而又懊恼的问题——要怎么清屏?
答案是为IDLE增加一个清屏的扩展ClearWindow就可以了(在Issue 6143: IDLE中可以看到这个扩展的说明)。
下面我说说安装使用的方法。
1、下载clearwindow.py(解压后格式为py结尾https://pic2-orsoon.mac89.com/2017/0504/20170504021215869.rar
)。
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=
5、打开Python的IDLE,options选项中就可以看到增加了Clear shell window ctrl+;。
6、在IDLE输入代码,然后按Ctrl+;(是指Ctrl和;),发现刚输入代码可以被清除了。
说明:
快捷键Ctrl+;,可修改成其他键,将“clear-window=
经试验,Control换成Alter时,会启动不了IDLE,换成Shift时,快捷键不起作用,因为Shift+l,直接变成输入L了。
修改IDLE字体
写代码没有一个好的字体怎么行,IDLE自带的字体实在看的蛋疼,幸好小编当年玩Linux的时候为了找一个好的字体废寝忘食,终于找到了几款舒服的程序员专用字体。
有童鞋要问IDLE字体怎么配置?这个容易,在Options – Configure IDLE – Highlighting选项卡旁边有个Fonts/Tabs选项卡,可以用来配置字体和缩进宽度的。
小编推荐的字体就是source code pro和日本的M+,这也是小编最喜欢的两个编程字体,习惯Windows或者使用中文比较多的也可以尝试一下雅黑consolas混合字体Yahei Consolas Hybrid,在Windows下显示效果非常不错,不过Linux下就差强人意了。
总结
修改完IDLE的配色、高亮主题、字体,又添加了常用的清屏功能,IDLE的基本调教就算大功告成可以正常使用了,当然要用的舒服还有很长的路要走,比如给IDLE安装各种pip扩展,readline插件或者pywin32这种功能更强大的IDE,小编就不再一一说明了,有需要的小伙伴可以去网上找一下IDLE扩展的介绍。