Python IDLE清空窗口的实例


Posted in Python onJune 25, 2018

使用Python IDLE时发现并没有清空当前窗口的快捷键,也没有像 clear 这样的命令,使用非常不便。

新建一个 ClearWindow.py脚本,源码如下:

"""

Clear Window Extension
Version: 0.2

Author: Roger D. Serwy
    roger.serwy@gmail.com

Date: 2009-06-14

It provides "Clear Shell Window" under "Options"
with ability to undo.

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

    # fix iomark 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
    # work around 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()
    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)

拷贝ClearWindow.py文件,放在Python安装目录Python XXX\Lib\idlelib下面(XXX为你的python版本,比如2.7)。

记事本打开Python XXX\Lib\idlelib目录下的config-extensions.def(IDLE扩展的配置文件), 为防止出错,你可以在打开它之前先copy一个备份 。

修改config-extensions.def ,在末尾添加如下内容,然后保存退出:

[ClearWindow]
enable=1
enable_editor=0
enable_shell=1
[ClearWindow_cfgBindings]
clear-window=<Control-Key-;>

打开Python的IDLE,options选项中就可以看到增加了Clear shell window ctrl+;。

在IDLE输入代码,然后按Ctrl+;(是指Ctrl和;),发现刚输入代码可以被清除了。

快捷键Ctrl+;,可修改成其他键,将

clear-window=<Control-Key-;>

里的Control和;修改成其他键即可。

以上这篇Python IDLE清空窗口的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
linux系统使用python获取cpu信息脚本分享
Jan 15 Python
利用Python爬取可用的代理IP
Aug 18 Python
python xml.etree.ElementTree遍历xml所有节点实例详解
Dec 04 Python
老生常谈python的私有公有属性(必看篇)
Jun 09 Python
Python基于生成器迭代实现的八皇后问题示例
May 23 Python
Tensorflow卷积神经网络实例
May 24 Python
python 递归深度优先搜索与广度优先搜索算法模拟实现
Oct 22 Python
对python的bytes类型数据split分割切片方法
Dec 04 Python
python模块和包的应用BASE_PATH使用解析
Dec 14 Python
使用keras实现densenet和Xception的模型融合
May 23 Python
详解使用Python写一个向数据库填充数据的小工具(推荐)
Sep 11 Python
Python爬虫实战之爬取携程评论
Jun 02 Python
Python设置在shell脚本中自动补全功能的方法
Jun 25 #Python
PyCharm代码整体缩进,反向缩进的方法
Jun 25 #Python
Python代码块批量添加Tab缩进的方法
Jun 25 #Python
对python中for、if、while的区别与比较方法
Jun 25 #Python
详解Django+Uwsgi+Nginx的生产环境部署
Jun 25 #Python
python 字符串和整数的转换方法
Jun 25 #Python
python range()函数取反序遍历sequence的方法
Jun 25 #Python
You might like
第十四节--命名空间
2006/11/16 PHP
php mysql数据库操作分页类
2008/06/04 PHP
关于PHP的curl开启问题探讨
2014/04/08 PHP
php给一组指定关键词添加span标签的方法
2015/03/31 PHP
PHP中类属性与类静态变量的访问方法示例
2016/07/13 PHP
PHP常见数组排序方法小结
2018/08/20 PHP
PHP开启目录引索+fancyindex漂亮目录浏览带搜索功能
2019/09/23 PHP
ie 调试javascript的工具
2009/04/29 Javascript
javascript与CSS复习(《精通javascript》)
2010/06/29 Javascript
利用JavaScript检测CPU使用率自己写的
2014/03/22 Javascript
javascript获取select值的方法分析
2015/07/02 Javascript
jQuery采用连缀写法实现的折叠菜单效果
2015/09/18 Javascript
详解jQuery Mobile自定义标签
2016/01/06 Javascript
详解js前端代码异常监控
2017/01/11 Javascript
jQuery实现导航回弹效果
2017/02/27 Javascript
JavaScript字符串转数字的5种方法及遇到的坑
2018/07/16 Javascript
对angularJs中2种自定义服务的实例讲解
2018/09/30 Javascript
webpack配置proxyTable时pathRewrite无效的解决方法
2018/12/13 Javascript
Vue结合后台导入导出Excel问题详解
2019/02/19 Javascript
JS+CSS实现过渡特效
2021/01/02 Javascript
python模块之StringIO使用示例
2015/04/08 Python
Python yield 使用浅析
2015/05/28 Python
Python3.5集合及其常见运算实例详解
2019/05/01 Python
Python中新式类与经典类的区别详析
2019/07/10 Python
python怎么删除缓存文件
2020/07/19 Python
python 实现百度网盘非会员上传超过500个文件的方法
2021/01/07 Python
科尔士百货公司官网:Kohl’s
2016/07/11 全球购物
英国精品买手店:Browns Fashion
2016/09/29 全球购物
迷你分体式空调:SoGoodToBuy
2018/08/07 全球购物
世界上最大的乐谱选择:Sheet Music Plus
2020/01/18 全球购物
编辑找工作求职信分享
2014/01/03 职场文书
绩效考核实施方案
2014/03/18 职场文书
学校标语口号大全
2015/12/26 职场文书
土木工程生产实习心得体会
2016/01/22 职场文书
大学生创业计划书
2019/06/24 职场文书
HTML5来实现本地文件读取和写入的实现方法
2021/05/25 HTML / CSS