写在前面
pyshell.py这一文件实在是过于庞大了。所以,我应该会分好多次对其进行分析。如下图所示,这是在PyCharm中所显示的pyshell.py的文件结构。今天我们要分析的是PyShellEditorWindow类。
文件作用
对于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>>"