[关闭]
@SuHongjun 2020-04-29T14:08:17.000000Z 字数 1929 阅读 172

为IDLE增加清屏功能

Python


把下面的代码存为D:\Program Files\Python36\Lib\idlelib\ClearWindow.py

  1. #ClearWindow.py
  2. """
  3. Clear Window Extension
  4. Version: 0.2
  5. Author: Roger D. Serwy
  6. roger.serwy@gmail.com
  7. Date: 2009-06-14
  8. It provides "Clear Shell Window" under "Options"
  9. with ability to undo.
  10. Add these lines to config-extensions.def
  11. [ClearWindow]
  12. enable=1
  13. enable_editor=0
  14. enable_shell=1
  15. [ClearWindow_cfgBindings]
  16. clear-window=<Control-Key-l>
  17. """
  18. class ClearWindow:
  19. menudefs = [
  20. ('options', [None,
  21. ('Clear Shell Window', '<<clear-window>>'),
  22. ]),]
  23. def __init__(self, editwin):
  24. self.editwin = editwin
  25. self.text = self.editwin.text
  26. self.text.bind("<<clear-window>>", self.clear_window2)
  27. self.text.bind("<<undo>>", self.undo_event) # add="+" doesn't work
  28. def undo_event(self, event):
  29. text = self.text
  30. text.mark_set("iomark2", "iomark")
  31. text.mark_set("insert2", "insert")
  32. self.editwin.undo.undo_event(event)
  33. # fix iomark and insert
  34. text.mark_set("iomark", "iomark2")
  35. text.mark_set("insert", "insert2")
  36. text.mark_unset("iomark2")
  37. text.mark_unset("insert2")
  38. def clear_window2(self, event): # Alternative method
  39. # work around the ModifiedUndoDelegator
  40. text = self.text
  41. text.undo_block_start()
  42. text.mark_set("iomark2", "iomark")
  43. text.mark_set("iomark", 1.0)
  44. text.delete(1.0, "iomark2 linestart")
  45. text.mark_set("iomark", "iomark2")
  46. text.mark_unset("iomark2")
  47. text.undo_block_stop()
  48. if self.text.compare('insert', '<', 'iomark'):
  49. self.text.mark_set('insert', 'end-1c')
  50. self.editwin.set_line_and_column()
  51. def clear_window(self, event):
  52. # remove undo delegator
  53. undo = self.editwin.undo
  54. self.editwin.per.removefilter(undo)
  55. # clear the window, but preserve current command
  56. self.text.delete(1.0, "iomark linestart")
  57. if self.text.compare('insert', '<', 'iomark'):
  58. self.text.mark_set('insert', 'end-1c')
  59. self.editwin.set_line_and_column()
  60. # restore undo delegator
  61. self.editwin.per.insertfilter(undo)

在D:\Program Files\Python36\Lib\idlelib目录下,修改config-extensions.def(建议先copy一份到桌面,防止操作出错),在其末尾加上:

  1. [ClearWindow]
  2. enable=1
  3. enable_editor=0
  4. enable_shell=1
  5. [ClearWindow_cfgBindings]
  6. clear-window=<Control-Key-l>

重启IDLE,Options菜单下便有了清屏功能,快捷键ctrl+L

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注