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 相关文章推荐
python使用电子邮件模块smtplib的方法
Aug 28 Python
django model去掉unique_together报错的解决方案
Oct 18 Python
Python 使用os.remove删除文件夹时报错的解决方法
Jan 13 Python
浅析Python3爬虫登录模拟
Feb 07 Python
Python画柱状统计图操作示例【基于matplotlib库】
Jul 04 Python
解决win64 Python下安装PIL出错问题(图解)
Sep 03 Python
pygame游戏之旅 python和pygame安装教程
Nov 20 Python
详解Python学习之安装pandas
Apr 16 Python
python实践项目之监控当前联网状态详情
May 23 Python
python实现将视频按帧读取到自定义目录
Dec 10 Python
OpenCV 之按位运算举例解析
Jun 19 Python
python 牛顿法实现逻辑回归(Logistic Regression)
Oct 15 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
判断是否为指定长度内字符串的php函数
2010/02/16 PHP
基于PHP+Ajax实现表单验证的详解
2013/06/25 PHP
PHP通过API获取手机号码归属地
2015/05/28 PHP
使用XHGui来测试PHP性能的教程
2015/07/03 PHP
Yii2压缩PHP中模板代码的输出问题
2018/08/28 PHP
PHP封装mysqli基于面向对象的mysql数据库操作类与用法示例
2019/02/25 PHP
ExtJS GTGrid 简单用户管理
2009/07/01 Javascript
JS连接SQL数据库与ACCESS数据库的方法实例
2013/11/21 Javascript
jQuery实现当按下回车键时绑定点击事件
2014/01/28 Javascript
javasctipt如何显示几分钟前、几天前等
2014/04/30 Javascript
javascript实现的元素拖动函数宿主为浏览器
2014/07/21 Javascript
简单谈谈javascript代码复用模式
2015/01/28 Javascript
Bootstrap每天必学之表单
2015/11/23 Javascript
python插入数据到列表的方法
2015/04/30 Python
简单讲解Python编程中namedtuple类的用法
2016/06/21 Python
Apache如何部署django项目
2017/05/21 Python
Python随机生成均匀分布在三角形内或者任意多边形内的点
2017/12/14 Python
PyQt5实现下载进度条效果
2018/04/19 Python
pip安装py_zipkin时提示的SSL问题对应
2018/12/29 Python
Python学习笔记之For循环用法详解
2019/08/14 Python
Django-simple-captcha验证码包使用方法详解
2020/11/28 Python
python xlsxwriter模块的使用
2020/12/24 Python
CSS3中线性颜色渐变的一些实现方法
2015/07/14 HTML / CSS
通过Canvas及File API缩放并上传图片完整示例
2013/08/08 HTML / CSS
浅谈利用缓存来优化HTML5 Canvas程序的性能
2015/05/12 HTML / CSS
迷你唐卡软皮鞋:Minnetonka Moccasin
2018/05/01 全球购物
Camper鞋西班牙官方网上商店:西班牙马略卡岛的鞋类品牌
2019/03/14 全球购物
中国央视网签名寄语
2014/01/18 职场文书
合伙协议书
2014/04/23 职场文书
大学竞选班长演讲稿
2014/04/24 职场文书
企业总经理任命书
2014/06/05 职场文书
关于清明节的演讲稿
2014/09/13 职场文书
2015年乡镇财政工作总结
2015/05/19 职场文书
2015年外贸业务员工作总结范文
2015/05/23 职场文书
2016年“5.12”国际护士节活动总结
2016/04/06 职场文书
公司员工违法违章行为检讨书
2019/06/24 职场文书