初學python時,許多人會選擇使用python自帶的idle作為練習的ide。然而,默認的代碼配色和語法高亮主題可能并不適合所有人。
那么,能否將IDLE配置成像Sublime Text那樣的主題呢?
答案是肯定的。
效果圖如下:
立即學習“Python免費學習筆記(深入)”;
如何實現呢?別著急,按照以下步驟逐一進行即可。首先,需要找到名為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。
# for Linux vi ~/.idlerc/config-highlight.cfg
在Windows系統下,可以使用以下命令:
notepad %USERPROFILE%.idlercconfig-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
tango這個主題就是文章開頭展示的主題效果,酷吧?不過,修改了config-highlight.cfg文件后,事情還沒有結束。還需要在Python IDLE中選擇我們的主題。如果已經打開Python IDLE,請保存必要文件并重新啟動IDLE。啟動后,依次選擇菜單上的Options – Configure IDLE…
選擇Highlighting選項卡,然后在select a Custom Theme中選擇剛才配置的兩個主題之一,這里我選擇了tango。
完成配置后,點擊Apply或者OK,查看效果。有人可能會問字體如何配置?這很簡單,在Highlighting選項卡旁邊有一個Fonts/Tabs選項卡,可以用來配置字體和縮進寬度:
完成這些步驟后,一個全新的界面就應該呈現了。如果看不到效果,請重啟Python IDLE。