Python IDLE源码分析-pyshell.py(二)

本文主要分析了IDLE中pyshell.py文件的PyShellEditorWindow类,该类在编辑窗口的基础上增加了断点处理功能。文章详细探讨了类的构造函数、断点相关成员、颜色设置及事件绑定,并提到了tkinter的Text部件方法。附录讨论了未使用的event参数和奇怪的self.io成员。
摘要由CSDN通过智能技术生成

写在前面

    pyshell.py这一文件实在是过于庞大了。所以,我应该会分好多次对其进行分析。如下图所示,这是在PyCharm中所显示的pyshell.py的文件结构。今天我们要分析的是PyShellEditorWindow类。


pyshell.py文件结构



文件作用

    对于IDLE的编辑窗口,idlelib的其他文件中有一个非常复杂的类叫做EditorWindow。这个类提供的窗口功能是新建或打开文件后的那个窗口,是对Python文件进行编辑的。相比之下,shell窗口有许多额外的功能。所以,在pyshell.py中,PyShellEditorWindow类继承了原来的EditorWindow,增添了许多独特的功能。


文件源码

class PyShellEditorWindow(EditorWindow):

    def __init__(self, *args):
        # 初始化函数。实例化时自动调用。
        # 清空断点列表。
        self.breakpoints = []
        EditorWindow.__init__(self, *args)
        # 绑定tk事件。
        self.text.bind("<<set-breakpoint-here>>", self.set_breakpoint_here)
        self.text.bind("<<clear-breakpoint-here>>", self.clear_breakpoint_here)
        self.text.bind("<<open-python-shell>>", self.flist.open_shell)
		# 存储断点列表的文件。
        self.breakpointPath = os.path.join(idleConf.userdir, 'breakpoints.lst')
        # 一旦文件有所改变,就需要存储断点列表。
        def filename_changed_hook(old_hook=self.io.filename_change_hook, self=self):
            self.restore_file_breaks()
            old_hook()
        self.io.set_filename_change_hook(filename_changed_hook)
        if self.io.filename:
            self.restore_file_breaks()
        self.color_breakpoint_text()

	# 定义了新的右键弹出菜单。此处是列表,实现在后面。
    rmenu_specs = [
        ("Cut", "<<cut>>", "rmenu_check_cut"),
        ("Copy", "<<copy>>", "rmenu_check_copy"),
        ("Paste", "<<paste>>", "rmenu_check_paste"),
        (None, None, None),
        ("Set Breakpoint", "<<set-breakpoint-here>>", None),
        ("Clear Breakpoint", "<<clear-breakpoint-here>>"
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值