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 相关文章推荐
使用PDB简单调试Python程序简明指南
Apr 25 Python
python安装mysql-python简明笔记(ubuntu环境)
Jun 25 Python
判断网页编码的方法python版
Aug 12 Python
Python爬虫实例爬取网站搞笑段子
Nov 08 Python
将tensorflow的ckpt模型存储为npy的实例
Jul 09 Python
对python 命令的-u参数详解
Dec 03 Python
django连接oracle时setting 配置方法
Aug 29 Python
详解Django admin高级用法
Nov 06 Python
python构造函数init实例方法解析
Jan 19 Python
python解析xml文件方式(解析、更新、写入)
Mar 05 Python
Python读取多列数据以及用matplotlib制作图表方法实例
Sep 23 Python
python绘制箱型图
Apr 27 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/10/09 PHP
phpwind中的数据库操作类
2007/01/02 PHP
PHP 第二节 数据类型之字符串类型
2012/04/28 PHP
PHP对象的浅复制与深复制的实例详解
2017/10/26 PHP
thinkphp5 加载静态资源路径与常量的方法
2017/12/24 PHP
常用一些Javascript判断函数
2012/08/14 Javascript
javascript实现控制文字大中小显示
2015/04/28 Javascript
JS组件Bootstrap实现弹出框和提示框效果代码
2015/12/08 Javascript
JavaScript文本框脚本编写的注意事项
2016/01/25 Javascript
Bootstrap Chart组件使用教程
2016/04/28 Javascript
JS查找英文文章中出现频率最高的单词
2017/03/20 Javascript
理解Angular的providers给Http添加默认headers
2017/07/04 Javascript
JavaScript实现购物车基本功能
2017/07/21 Javascript
Angular 容器部署的方法
2018/04/17 Javascript
微信小程序开发之tabbar图标和颜色的实现
2018/10/17 Javascript
layui点击按钮页面会自动刷新的解决方案
2019/10/25 Javascript
微信小程序如何加载数据库真实数据的实现
2020/03/04 Javascript
微信小程序12行js代码自己写个滑块功能(推荐)
2020/07/15 Javascript
微信小程序实现可长按移动控件
2020/11/01 Javascript
测试、预发布后用python检测网页是否有日常链接
2014/06/03 Python
python+Django+apache的配置方法详解
2016/06/01 Python
深入浅析python with语句简介
2018/04/11 Python
TensorFlow 滑动平均的示例代码
2018/06/19 Python
对python中的os.getpid()和os.fork()函数详解
2019/08/08 Python
python文件操作的简单方法总结
2019/11/07 Python
Python实现RGB与HSI颜色空间的互换方式
2019/11/27 Python
Spartoo英国:欧洲最大的网上鞋店
2016/09/13 全球购物
STAUD官方网站:洛杉矶独有的闲适风格
2019/04/11 全球购物
什么是托管函数?托管函数有什么用?
2014/06/15 面试题
家长通知书教师评语
2014/04/17 职场文书
爱之链教学反思
2014/04/30 职场文书
电子商务专业求职信范文
2015/03/19 职场文书
2016同学毕业寄语大全
2015/12/04 职场文书
高一军训口号
2015/12/25 职场文书
MySQL为id选择合适的数据类型
2021/06/07 MySQL
实现AJAX异步调用和局部刷新的基本步骤
2022/03/17 Javascript