Files
hustoj/web/python-docs/library/idle.html
2025-03-09 17:10:56 +08:00

780 lines
69 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh_CN">
<head>
<meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />
<title>IDLE &#8212; Python 3.8.20 文档</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script src="../_static/jquery.js"></script>
<script src="../_static/underscore.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/language_data.js"></script>
<script src="../_static/translations.js"></script>
<script src="../_static/sidebar.js"></script>
<link rel="search" type="application/opensearchdescription+xml"
title="在 Python 3.8.20 文档 中搜索"
href="../_static/opensearch.xml"/>
<link rel="author" title="关于这些文档" href="../about.html" />
<link rel="index" title="索引" href="../genindex.html" />
<link rel="search" title="搜索" href="../search.html" />
<link rel="copyright" title="版权所有" href="../copyright.html" />
<link rel="next" title="其他图形用户界面GUI包" href="othergui.html" />
<link rel="prev" title="tkinter.scrolledtext --- 滚动文字控件" href="tkinter.scrolledtext.html" />
<link rel="canonical" href="https://docs.python.org/3/library/idle.html" />
<style>
@media only screen {
table.full-width-table {
width: 100%;
}
}
</style>
<link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
<script type="text/javascript" src="../_static/copybutton.js"></script>
<script type="text/javascript" src="../_static/menu.js"></script>
</head>
<body>
<div class="mobile-nav">
<input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
<label for="menuToggler" class="toggler__label">
<span></span>
</label>
<nav class="nav-content" role="navigation">
<a href="https://www.python.org/" class="nav-logo">
<img src="../_static/py.svg" alt="Logo"/>
</a>
<div class="version_switcher_placeholder"></div>
<form role="search" class="search" action="../search.html" method="get">
<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
<path fill-rule="nonzero"
d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z" fill="#444"></path>
</svg>
<input type="text" name="q" aria-label="快速搜索"/>
<input type="submit" value="转向"/>
</form>
</nav>
<div class="menu-wrapper">
<nav class="menu" role="navigation" aria-label="main navigation">
<div class="language_switcher_placeholder"></div>
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">IDLE</a><ul>
<li><a class="reference internal" href="#menus">目录</a><ul>
<li><a class="reference internal" href="#file-menu-shell-and-editor">文件菜单 (命令行和编辑器)</a></li>
<li><a class="reference internal" href="#edit-menu-shell-and-editor">编辑菜单(命令行和编辑器)</a></li>
<li><a class="reference internal" href="#format-menu-editor-window-only">格式菜单(仅 window 编辑器)</a></li>
<li><a class="reference internal" href="#run-menu-editor-window-only">运行菜单(仅 window 编辑器)</a></li>
<li><a class="reference internal" href="#shell-menu-shell-window-only">Shell 菜单(仅 window 编辑器)</a></li>
<li><a class="reference internal" href="#debug-menu-shell-window-only">调试菜单(仅 window 编辑器)</a></li>
<li><a class="reference internal" href="#options-menu-shell-and-editor">选项菜单(命令行和编辑器)</a></li>
<li><a class="reference internal" href="#window-menu-shell-and-editor">Window 菜单(命令行和编辑器)</a></li>
<li><a class="reference internal" href="#help-menu-shell-and-editor">帮助菜单(命令行和编辑器)</a></li>
<li><a class="reference internal" href="#context-menus">上下文菜单</a></li>
</ul>
</li>
<li><a class="reference internal" href="#editing-and-navigation">编辑和导航</a><ul>
<li><a class="reference internal" href="#editor-windows">编辑窗口</a></li>
<li><a class="reference internal" href="#key-bindings">按键绑定</a></li>
<li><a class="reference internal" href="#automatic-indentation">自动缩进</a></li>
<li><a class="reference internal" href="#completions">完成</a></li>
<li><a class="reference internal" href="#calltips">提示</a></li>
<li><a class="reference internal" href="#code-context">代码上下文</a></li>
<li><a class="reference internal" href="#python-shell-window">Python Shell 窗口</a></li>
<li><a class="reference internal" href="#text-colors">文本颜色</a></li>
</ul>
</li>
<li><a class="reference internal" href="#startup-and-code-execution">启动和代码执行</a><ul>
<li><a class="reference internal" href="#command-line-usage">命令行语法</a></li>
<li><a class="reference internal" href="#startup-failure">启动失败</a></li>
<li><a class="reference internal" href="#running-user-code">运行用户代码</a></li>
<li><a class="reference internal" href="#user-output-in-shell">Shell中的用户输出</a></li>
<li><a class="reference internal" href="#developing-tkinter-applications">开发 tkinter 应用程序</a></li>
<li><a class="reference internal" href="#running-without-a-subprocess">在没有子进程的情况下运行</a></li>
</ul>
</li>
<li><a class="reference internal" href="#help-and-preferences">帮助和偏好</a><ul>
<li><a class="reference internal" href="#help-sources">帮助源</a></li>
<li><a class="reference internal" href="#setting-preferences">首选项设置</a></li>
<li><a class="reference internal" href="#idle-on-macos">macOS 上的IDLE</a></li>
<li><a class="reference internal" href="#extensions">扩展</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="tkinter.scrolledtext.html"
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.scrolledtext</span></code> --- 滚动文字控件</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="othergui.html"
title="下一章">其他图形用户界面GUI</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/idle.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</nav>
</div>
</div>
<div id="outdated-warning" style="padding: .5em; text-align: center; background-color: #FFBABA; color: #6A0E0E;">
这个文档所针对的是一个已不再受支持的 Python 旧版本。
你应当升级版本,并阅读
<a href="/3/library/idle.html"> Python 当前稳定版本的文档</a>.
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
accesskey="I">索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="othergui.html" title="其他图形用户界面GUI包"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="tkinter.scrolledtext.html" title="tkinter.scrolledtext --- 滚动文字控件"
accesskey="P">上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="tk.html" accesskey="U">Tk图形用户界面(GUI)</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="idle">
<span id="id1"></span><h1>IDLE<a class="headerlink" href="#idle" title="永久链接至标题"></a></h1>
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/idlelib/">Lib/idlelib/</a></p>
<hr class="docutils" id="index-0" />
<p>IDLE 是 Python 所内置的开发与学习环境。</p>
<p>IDLE 具有以下特性:</p>
<ul class="simple">
<li><p>编码于 100% 纯正的 Python使用名为 <a class="reference internal" href="tkinter.html#module-tkinter" title="tkinter: Interface to Tcl/Tk for graphical user interfaces"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code></a> 的图形用户界面工具</p></li>
<li><p>跨平台:在 Windows、Unix 和 macOS 上工作近似。</p></li>
<li><p>提供输入输出高亮和错误信息的 Python 命令行窗口 (交互解释器)</p></li>
<li><p>提供多次撤销操作、Python 语法高亮、智能缩进、函数调用提示、自动补全等功能的多窗口文本编辑器</p></li>
<li><p>在多个窗口中检索,在编辑器中替换文本,以及在多个文件中检索(通过 grep</p></li>
<li><p>提供持久保存的断点调试、单步调试、查看本地和全局命名空间功能的调试器</p></li>
<li><p>配置、浏览以及其它对话框</p></li>
</ul>
<section id="menus">
<h2>目录<a class="headerlink" href="#menus" title="永久链接至标题"></a></h2>
<p>IDEL 具有两个主要窗口类型,分别是命令行窗口和编辑器窗口。用户可以同时打开多个编辑器窗口。对于 Windows 和 Linux 平台,都有各自的主菜单。如下记录的每个菜单标识着与之关联的窗口类型。</p>
<p>导出窗口,例如使用 编辑=&gt;在文件中查找 是编辑器窗口的的一个子类型。它们目前有着相同的主菜单,但是默认标题和上下文菜单不同。</p>
<p>在macOS上只有一个应用程序菜单。它根据当前选择的窗口动态变化。它具有一个IDLE菜单并且下面描述的某些条目已移动到符合Apple准则的位置。</p>
<section id="file-menu-shell-and-editor">
<h3>文件菜单 (命令行和编辑器)<a class="headerlink" href="#file-menu-shell-and-editor" title="永久链接至标题"></a></h3>
<dl class="simple">
<dt>新建文件</dt><dd><p>创建一个文件编辑器窗口。</p>
</dd>
<dt>打开...</dt><dd><p>使用打开窗口以打开一个已存在的文件。</p>
</dd>
<dt>近期文件</dt><dd><p>打开一个近期文件列表,选取一个以打开它。</p>
</dd>
<dt>打开模块...</dt><dd><p>打开一个已存在的模块 (搜索 sys.path</p>
</dd>
</dl>
<dl class="simple" id="index-1">
<dt>类浏览器</dt><dd><p>于当前所编辑的文件中使用树形结构展示函数、类以及方法。在命令行中,首先打开一个模块。</p>
</dd>
<dt>路径浏览</dt><dd><p>在树状结构中展示 sys.path 目录、模块、函数、类和方法。</p>
</dd>
<dt>保存</dt><dd><p>如果文件已经存在,则将当前窗口保存至对应的文件。自打开或上次保存之后经过修改的文件的窗口标题栏首尾将出现星号 * 。如果没有对应的文件,则使用“另存为”代替。</p>
</dd>
<dt>保存为...</dt><dd><p>使用“保存为”对话框保存当前窗口。被保存的文件将作为当前窗口新的对应文件。</p>
</dd>
<dt>另存为副本...</dt><dd><p>保存当前窗口至另一个文件,而不修改当前对应文件。</p>
</dd>
<dt>打印窗口</dt><dd><p>通过默认打印机打印当前窗口。</p>
</dd>
<dt>关闭</dt><dd><p>关闭当前窗口(如果未保存则询问)。</p>
</dd>
<dt>退出</dt><dd><p>关闭所有窗口并退出 IDLE如果未保存则询问</p>
</dd>
</dl>
</section>
<section id="edit-menu-shell-and-editor">
<h3>编辑菜单(命令行和编辑器)<a class="headerlink" href="#edit-menu-shell-and-editor" title="永久链接至标题"></a></h3>
<dl class="simple">
<dt>撤销操作</dt><dd><p>撤销当前窗口的最近一次操作。最高可以撤回 1000 条操作记录。</p>
</dd>
<dt>重做</dt><dd><p>重做当前窗口最近一次所撤销的操作。</p>
</dd>
<dt>剪切</dt><dd><p>复制选区至系统剪贴板,然后删除选区。</p>
</dd>
<dt>复制</dt><dd><p>复制选区至系统剪贴板。</p>
</dd>
<dt>粘贴</dt><dd><p>插入系统剪贴板的内容至当前窗口。</p>
</dd>
</dl>
<p>剪贴板功能也可用于上下文目录。</p>
<dl class="simple">
<dt>全选</dt><dd><p>选择当前窗口的全部内容。</p>
</dd>
<dt>查找...</dt><dd><p>打开一个提供多选项的查找窗口。</p>
</dd>
<dt>再次查找</dt><dd><p>重复上次搜索,如果结果存在。</p>
</dd>
<dt>查找选区</dt><dd><p>查找当前选中的字符串,如果存在</p>
</dd>
<dt>在文件中查找...</dt><dd><p>打开文件查找对话框。将结果输出至新的输出窗口。</p>
</dd>
<dt>替换...</dt><dd><p>打开 查找并替换 对话框。</p>
</dd>
<dt>前往行</dt><dd><p>将光标移到所请求行的开头并使该行可见。 对于超过文件尾的请求将会移到文件尾。 清除所有选区并更新行列状态。</p>
</dd>
<dt>提示完成</dt><dd><p>打开一个可滚动列表以允许选择现有的名称。 请参阅下面编辑与导航一节中的 <a class="reference internal" href="#completions"><span class="std std-ref">自动补全</span></a></p>
</dd>
<dt>展开文本</dt><dd><p>展开键入的前缀以匹配同一窗口中的完整单词;重复以获得不同的扩展。</p>
</dd>
<dt>显示调用贴士</dt><dd><p>在函数的右括号后,打开一个带有函数参数提示的小窗口。请参阅下面的“编辑和导航”部分中的 <a class="reference internal" href="#calltips"><span class="std std-ref">Calltips</span></a></p>
</dd>
<dt>显示周围括号</dt><dd><p>突出显示周围的括号。</p>
</dd>
</dl>
</section>
<section id="format-menu-editor-window-only">
<span id="format-menu"></span><h3>格式菜单(仅 window 编辑器)<a class="headerlink" href="#format-menu-editor-window-only" title="永久链接至标题"></a></h3>
<dl class="simple">
<dt>增加缩进</dt><dd><p>将选定的行右移缩进宽度默认为4个空格</p>
</dd>
<dt>减少缩进</dt><dd><p>将选定的行向左移动缩进宽度默认为4个空格</p>
</dd>
<dt>注释</dt><dd><p>在所选行的前面插入 ##。</p>
</dd>
<dt>取消注释</dt><dd><p>从所选行中删除开头的 或 ##。</p>
</dd>
<dt>制表符化</dt><dd><p><em>前导</em> 空格变成制表符。 注意我们建议使用4个空格来缩进Python代码。</p>
</dd>
<dt>取消制表符化</dt><dd><p><em>所有</em> 制表符转换为正确的空格数。</p>
</dd>
<dt>缩进方式切换</dt><dd><p>打开一个对话框,以在制表符和空格之间切换。</p>
</dd>
<dt>缩进宽度调整</dt><dd><p>打开一个对话框以更改缩进宽度。 Python社区接受的默认值为4个空格。</p>
</dd>
<dt>格式段落</dt><dd><p>在注释块或多行字符串或字符串中的选定行中重新格式化当前以空行分隔的段落。段落中的所有行的格式都将少于N列其中N默认为72。</p>
</dd>
<dt>删除尾随空格</dt><dd><p>通过将 str.rstrip 应用于每行包括多行字符串中的行删除行尾非空白字符之后的尾随空格和其他空白字符。除Shell窗口外在文件末尾删除多余的换行符。</p>
</dd>
</dl>
</section>
<section id="run-menu-editor-window-only">
<span id="index-2"></span><h3>运行菜单(仅 window 编辑器)<a class="headerlink" href="#run-menu-editor-window-only" title="永久链接至标题"></a></h3>
<dl class="simple" id="run-module">
<dt>运行模块</dt><dd><p>执行 <a class="reference internal" href="#check-module"><span class="std std-ref">检查模块</span></a> 。如果没有错误,重新启动 shell 以清理环境,然后执行模块。输出显示在 shell 窗口中。请注意,输出需要使用 “打印” 或 “写入”。执行完成后Shell 将保留焦点并显示提示。此时,可以交互地探索执行的结果。这类似于在命令行执行带有 <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-i</span> <span class="pre">file</span></code> 的文件。</p>
</dd>
</dl>
<dl class="simple" id="run-custom">
<dt>运行... 定制</dt><dd><p><a class="reference internal" href="#run-module"><span class="std std-ref">运行模块</span></a> 相同,但使用自定义设置运行该模块。<em>命令行参数</em> 扩展 <a class="reference internal" href="sys.html#sys.argv" title="sys.argv"><code class="xref py py-data docutils literal notranslate"><span class="pre">sys.argv</span></code></a> ,就像在命令行上传递一样。该模块可以在命令行管理程序中运行,而无需重新启动。</p>
</dd>
</dl>
<dl class="simple" id="check-module">
<dt>检查模块</dt><dd><p>检查 “编辑器” 窗口中当前打开的模块的语法。如果尚未保存该模块,则 IDLE会提示用户保存或自动保存如在 “空闲设置” 对话框的 “常规” 选项卡中所选择的那样。如果存在语法错误,则会在 “编辑器” 窗口中指示大概位置。</p>
</dd>
</dl>
<dl class="simple" id="python-shell">
<dt>Python Shell</dt><dd><p>打开或唤醒Python Shell窗口。</p>
</dd>
</dl>
</section>
<section id="shell-menu-shell-window-only">
<h3>Shell 菜单(仅 window 编辑器)<a class="headerlink" href="#shell-menu-shell-window-only" title="永久链接至标题"></a></h3>
<dl class="simple">
<dt>查看最近重启</dt><dd><p>将Shell窗口滚动到上一次Shell重启。</p>
</dd>
<dt>重启Shell</dt><dd><p>重启 shell 以清理环境,重置显示和异常处理。</p>
</dd>
<dt>上一条历史记录</dt><dd><p>循环浏览历史记录中与当前条目匹配的早期命令。</p>
</dd>
<dt>下一条历史记录</dt><dd><p>循环浏览历史记录中与当前条目匹配的后续命令。</p>
</dd>
<dt>中断执行</dt><dd><p>停止正在运行的程序。</p>
</dd>
</dl>
</section>
<section id="debug-menu-shell-window-only">
<h3>调试菜单(仅 window 编辑器)<a class="headerlink" href="#debug-menu-shell-window-only" title="永久链接至标题"></a></h3>
<dl class="simple">
<dt>跳转到文件/行</dt><dd><p>查看当前行。 以光标提示,且上一行为文件名和行号。 如果找到目标,如果文件尚未打开则打开该文件,并显示目标行。 使用此菜单项来查看异常回溯中引用的源代码行以及用文件中查找功能找到的行。 也可在 Shell 窗口和 Output 窗口的上下文菜单中使用。</p>
</dd>
</dl>
<dl class="simple" id="index-3">
<dt>调试器(切换)</dt><dd><p>激活后在Shell中输入的代码或从编辑器中运行的代码将在调试器下运行。在编辑器中可以使用上下文菜单设置断点。此功能不完整具有实验性。</p>
</dd>
<dt>堆栈查看器</dt><dd><p>在树状目录中显示最后一个异常的堆栈回溯,可以访问本地和全局。</p>
</dd>
<dt>自动打开堆栈查看器</dt><dd><p>在未处理的异常上切换自动打开堆栈查看器。</p>
</dd>
</dl>
</section>
<section id="options-menu-shell-and-editor">
<h3>选项菜单(命令行和编辑器)<a class="headerlink" href="#options-menu-shell-and-editor" title="永久链接至标题"></a></h3>
<dl class="simple">
<dt>配置 IDLE</dt><dd><p>打开配置对话框并更改以下各项的首选项字体、缩进、键绑定、文本颜色主题、启动窗口和大小、其他帮助源和扩展名。在MacOS上通过在应用程序菜单中选择首选项来打开配置对话框。有关详细信息请参阅帮助和首选项下的 <a class="reference internal" href="#preferences"><span class="std std-ref">首选项设置</span></a></p>
</dd>
</dl>
<p>大多数配置选项适用于所有窗口或将来的所有窗口。以下选项仅适用于活动窗口。</p>
<dl class="simple">
<dt>显示/隐藏代码上下文(仅 window 编辑器)</dt><dd><p>在编辑窗口顶部打开一个面板来显示在窗口顶部滚动的代码块上下文。 请参阅下文“编辑与导航”章节中的 <a class="reference internal" href="#code-context"><span class="std std-ref">代码上下文</span></a></p>
</dd>
<dt>显示/隐藏行号(仅 window 编辑器)</dt><dd><p>在编辑窗口左侧打开一个显示代码文本行编号的列。 默认为关闭显示,这可以在首选项中修改 (参见 <a class="reference internal" href="#preferences"><span class="std std-ref">设置首选项</span></a>)。</p>
</dd>
<dt>缩放/还原高度</dt><dd><p>在窗口的正常尺寸和最大高度之间进行切换。 初始尺寸默认为 40 行每行 80 字符,除非在配置 IDLE 对话框的通用选项卡中做了修改。 屏幕的最大高度由首次在屏幕上将缩小的窗口最大化的操作来确定。 改变屏幕设置可能使保存的高度失效。 此切换操作在窗口最大化状态下无效。</p>
</dd>
</dl>
</section>
<section id="window-menu-shell-and-editor">
<h3>Window 菜单(命令行和编辑器)<a class="headerlink" href="#window-menu-shell-and-editor" title="永久链接至标题"></a></h3>
<p>列出所有打开的窗口的名称;选择一个将其带到前台(必要时对其进行去符号化)。</p>
</section>
<section id="help-menu-shell-and-editor">
<h3>帮助菜单(命令行和编辑器)<a class="headerlink" href="#help-menu-shell-and-editor" title="永久链接至标题"></a></h3>
<dl class="simple">
<dt>关于 IDLE</dt><dd><p>显示版本,版权,许可证,荣誉等。</p>
</dd>
<dt>IDLE 帮助</dt><dd><p>显示此 IDLE 文档,详细介绍菜单选项,基本编辑和导航以及其他技巧。</p>
</dd>
<dt>Python 文档</dt><dd><p>访问本地Python文档如果已安装或启动Web浏览器并打开docs.python.org显示最新的Python文档。</p>
</dd>
<dt>海龟演示</dt><dd><p>使用示例Python代码和turtle绘图运行turtledemo模块。</p>
</dd>
</dl>
<p>可以在“常规”选项卡下的“配置IDLE”对话框中添加其他帮助源。有关“帮助”菜单选项的更多信息请参见下面的 <a class="reference internal" href="#help-sources"><span class="std std-ref">帮助源</span></a> 小节。</p>
</section>
<section id="context-menus">
<span id="index-4"></span><h3>上下文菜单<a class="headerlink" href="#context-menus" title="永久链接至标题"></a></h3>
<p>通过在窗口中右击(在 macOS 上则为按住 Control 键点击)来打开一个上下文菜单。 上下文菜单也具有编辑菜单中的标准剪贴板功能。</p>
<dl class="simple">
<dt>剪切</dt><dd><p>复制选区至系统剪贴板,然后删除选区。</p>
</dd>
<dt>复制</dt><dd><p>复制选区至系统剪贴板。</p>
</dd>
<dt>粘贴</dt><dd><p>插入系统剪贴板的内容至当前窗口。</p>
</dd>
</dl>
<p>编辑器窗口也具有断点功能。 设置了断点的行会被特别标记。 断点仅在启用调试器运行时有效。 文件的断点会被保存在用户的 <code class="docutils literal notranslate"><span class="pre">.idlerc</span></code> 目录中。</p>
<dl class="simple">
<dt>设置断点</dt><dd><p>在当前行设置断点</p>
</dd>
<dt>清除断点</dt><dd><p>清除当前行断点</p>
</dd>
</dl>
<p>shell 和输出窗口还具有以下内容。</p>
<dl class="simple">
<dt>跳转到文件/行</dt><dd><p>与调试菜单相同。</p>
</dd>
</dl>
<p>Shell 窗口也有一个输出折叠功能,参见下文的 <em>Python Shell 窗口</em> 小节。</p>
<dl class="simple">
<dt>压缩</dt><dd><p>如果将光标位于输出行上,则会折叠在上方代码和下方提示直到 'Squeezed text' 标签之间的所有输出。</p>
</dd>
</dl>
</section>
</section>
<section id="editing-and-navigation">
<span id="id2"></span><h2>编辑和导航<a class="headerlink" href="#editing-and-navigation" title="永久链接至标题"></a></h2>
<section id="editor-windows">
<h3>编辑窗口<a class="headerlink" href="#editor-windows" title="永久链接至标题"></a></h3>
<p>IDLE 可以在启动时打开编辑器窗口,这取决于选项设置和你启动 IDLE 的方式。 在此之后,请使用 File 菜单。 对于给定的文件只能打开一个编辑器窗口。</p>
<p>标题栏包含文件名称、完整路径,以及运行该窗口的 Python 和 IDLE 版本。 状态栏包含行号 ('Ln') 和列号 ('Col')。 行号从 1 开始;列号则从 0 开始。</p>
<p>IDE 会定扩展名为 .py* 的文件包含 Python 代码而其他文件不包含。 可使用 Run 菜单来运行 Python 代码。</p>
</section>
<section id="key-bindings">
<h3>按键绑定<a class="headerlink" href="#key-bindings" title="永久链接至标题"></a></h3>
<p>在本节中,'C' 是指 Windows 和 Unix 上的 <kbd class="kbd docutils literal notranslate">Control</kbd> 键,以及 macOS 上的 <kbd class="kbd docutils literal notranslate">Command</kbd> 键。</p>
<ul>
<li><p><kbd class="kbd docutils literal notranslate">Backspace</kbd> 向左删除; <kbd class="kbd docutils literal notranslate">Del</kbd> 向右删除</p></li>
<li><p><kbd class="kbd docutils literal notranslate">C-Backspace</kbd> 向左删除单词; <kbd class="kbd docutils literal notranslate">C-Del</kbd> 向右删除单词</p></li>
<li><p>方向键和 <kbd class="kbd docutils literal notranslate">Page Up</kbd>/<kbd class="kbd docutils literal notranslate">Page Down</kbd> 移动</p></li>
<li><p><kbd class="kbd docutils literal notranslate">C-LeftArrow</kbd><kbd class="kbd docutils literal notranslate">C-RightArrow</kbd> 按单词移动</p></li>
<li><p><kbd class="kbd docutils literal notranslate">Home</kbd>/<kbd class="kbd docutils literal notranslate">End</kbd> 跳转到行首/尾</p></li>
<li><p><kbd class="kbd docutils literal notranslate">C-Home</kbd>/<kbd class="kbd docutils literal notranslate">C-End</kbd> 跳转到文档首/尾</p></li>
<li><p>一些有用的Emacs绑定是从Tcl / Tk继承的</p>
<blockquote>
<div><ul class="simple">
<li><p><kbd class="kbd docutils literal notranslate">C-a</kbd> 行首</p></li>
<li><p><kbd class="kbd docutils literal notranslate">C-e</kbd> 行尾</p></li>
<li><p><kbd class="kbd docutils literal notranslate">C-k</kbd> 删除行(但未将其放入剪贴板)</p></li>
<li><p><kbd class="kbd docutils literal notranslate">C-l</kbd> 将插入点设为窗口中心</p></li>
<li><p><kbd class="kbd docutils literal notranslate">C-b</kbd> 后退一个字符而不删除该字符(通常你也可以用方向键进行此操作)</p></li>
<li><p><kbd class="kbd docutils literal notranslate">C-f</kbd> 前进一个字符而不删除该字符(通常你也可以用方向键进行此操作)</p></li>
<li><p><kbd class="kbd docutils literal notranslate">C-p</kbd> 向上一行(通常你也可以用方向键进行此操作)</p></li>
<li><p><kbd class="kbd docutils literal notranslate">C-d</kbd> 删除下一个字符</p></li>
</ul>
</div></blockquote>
</li>
</ul>
<p>标准的键绑定(例如 <kbd class="kbd docutils literal notranslate">C-c</kbd> 复制和 <kbd class="kbd docutils literal notranslate">C-v</kbd> 粘贴)仍会有效。 键绑定可在配置 IDLE 对话框中选择。</p>
</section>
<section id="automatic-indentation">
<h3>自动缩进<a class="headerlink" href="#automatic-indentation" title="永久链接至标题"></a></h3>
<p>在一个代码块开头的语句之后,下一行会缩进 4 个空格符(在 Python Shell 窗口中是一个制表符)。 在特定关键字之后break, return 等),下一行将不再缩进。 在开头的缩进中,按 <kbd class="kbd docutils literal notranslate">Backspace</kbd> 将会删除 4 个空格符。 <kbd class="kbd docutils literal notranslate">Tab</kbd> 则会插入空格符(在 Python Shell 窗口中是一个制表符),具体数量取决于缩进宽度。 目前Tab 键按照 Tcl/Tk 的规定设置为四个空格符。</p>
<p>另请参阅 <a class="reference internal" href="#format-menu"><span class="std std-ref">Format 菜单</span></a> 的缩进/取消缩进区的命令。</p>
</section>
<section id="completions">
<span id="id3"></span><h3>完成<a class="headerlink" href="#completions" title="永久链接至标题"></a></h3>
<p>当被请求并且可用时,将为模块名、类属性、函数或文件名提供补全。 每次请求方法将显示包含现有名称的补全提示框。 (例外情况参见下文的 Tab 补全。) 对于任意提示框,要改变被补全的名称和提示框中被高亮的条目,可以通过输入和删除字符、按 <kbd class="kbd docutils literal notranslate">Up</kbd>, <kbd class="kbd docutils literal notranslate">Down</kbd>, <kbd class="kbd docutils literal notranslate">PageUp</kbd>, <kbd class="kbd docutils literal notranslate">PageDown</kbd>, <kbd class="kbd docutils literal notranslate">Home</kbd><kbd class="kbd docutils literal notranslate">End</kbd> 键;或是在提示框中单击。 要关闭补全提示框可以通过 <kbd class="kbd docutils literal notranslate">Escape</kbd>, <kbd class="kbd docutils literal notranslate">Enter</kbd> 或按两次 <kbd class="kbd docutils literal notranslate">Tab</kbd> 键或是在提示框外单击。 在提示框内双击则将执行选择并关闭。</p>
<p>有一种打开提示框的方式是输入一个关键字符并等待预设的一段间隔。 此间隔默认为 2 秒;这可以在设置对话框中定制。 (要防止自动弹出,可将时延设为一个很大的豪秒数值,例如 100000000。 对于导入的模块名或者类和函数属性,请输入 '.'。 对于根目录下的文件名,请在开头引号之后立即输入 <a class="reference internal" href="os.html#os.sep" title="os.sep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.sep</span></code></a><a class="reference internal" href="os.html#os.altsep" title="os.altsep"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.altsep</span></code></a>。 (在 Windows 下,可以先指定一个驱动器。) 可通过输入目录名和分隔符来进入子目录。</p>
<p>除了等待,或是在提示框关闭之后,可以使用 Edit 菜单的 Show Completions 来立即打开一个补全提示框。 默认的热键是 <kbd class="kbd docutils literal notranslate">C-space</kbd>。 如果在打开提示框之前输入一某个名称的前缀,则将显示第一个匹配项或最接近的项。 结果将与在提示框已显示之后输入前缀时相同。 在一个引号之后执行 Show Completions 将会实例当前目录下的文件名而不是根目录下的。</p>
<p>在输入前缀后按 <kbd class="kbd docutils literal notranslate">Tab</kbd> 键的效果通常与 Show Completions 相同。 (如果未输入前缀则为缩进。) 但是,如果输入的前缀只有一个匹配项,则该匹配项会立即被添加到编辑器文本中而不打开补全提示框。</p>
<p>在字符串以外且开头不带 '.' 地输入前缀并执行 'Show Completions' 或按 <kbd class="kbd docutils literal notranslate">Tab</kbd> 键将打开一个包含关键字、内置名称和现有模块级名称的补全提示框。</p>
<p>当在编辑器(而非 Shell中编辑代码时可以通过运行你的代码并在此后不重启 Shell 来增加可用的模块级名称。 这在文件顶部添加了导入语句之后会特别有用。 这还会增加可用的属性补全。</p>
<p>补全提示框在初始时会排除以 '_' 打头的名称,对于模块还会排除未包括在 '__all__' 中的名称。 这些隐藏名称可通过在 '.' 之后输入 '_' 来访问,这在补全提示框打开之前或之后都是有效的。</p>
</section>
<section id="calltips">
<span id="id4"></span><h3>提示<a class="headerlink" href="#calltips" title="永久链接至标题"></a></h3>
<p>当在一个 <em>可用的</em> 函数名称之后输入 <kbd class="kbd docutils literal notranslate">(</kbd> 时将自动显示一个调用提示。 函数名称表达式可以包括点号和方括号索引操作。 调用提示将保持打开直到它被点击、光标移出参数区、或是输入了 <kbd class="kbd docutils literal notranslate">)</kbd>。 当光标位于某个定义的参数区时,可以在菜单中选择 Edit 的 &quot;Show Call Tip&quot; 或是输入其快捷键来显示调用提示。</p>
<p>调用提示是由函数的签名和文档字符串到第一个空行或第五个非空行为止的内容组成的。 (某些内置函数没有可访问的签名。) 签名中的 '/' 或 '*' 指明其前面或后面的参数仅限以位置或名称(关键字)方式传入。 具体细节可能会改变。</p>
<p>在 Shell 中,可访问的函数取决于有哪些模块已被导入用户进程,包括由 IDLE 本身导入的模块,以及哪些定义已被运行,以上均从最近的重启动开始算起。</p>
<p>例如,重启动 Shell 并输入 <code class="docutils literal notranslate"><span class="pre">itertools.count(</span></code>。 将显示调用提示,因为 IDLE 出于自身需要已将 itertools 导入了用户进程。 (此行为可能会改变。) 输入 <code class="docutils literal notranslate"><span class="pre">turtle.write(</span></code> 则不显示任何提示。 因为 IDLE 本身不会导入 turtle。 菜单项和快捷键同样不会有任何反应。 输入 <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">turtle</span></code>。 则在此之后,<code class="docutils literal notranslate"><span class="pre">turtle.write(</span></code> 将显示调用提示。</p>
<p>在编辑器中import 语句在文件运行之前是没有效果的。 在输入 import 语句之后、添加函数定义之后,或是打开一个现有文件之后可以先运行一下文件。</p>
</section>
<section id="code-context">
<span id="id5"></span><h3>代码上下文<a class="headerlink" href="#code-context" title="永久链接至标题"></a></h3>
<p>在一个包含 Python 代码的编辑器窗口内部,可以切换代码上下文以便显示或隐藏窗口顶部的面板。 当显示时,此面板可以冻结代码块的开头行,例如以 <code class="docutils literal notranslate"><span class="pre">class</span></code>, <code class="docutils literal notranslate"><span class="pre">def</span></code><code class="docutils literal notranslate"><span class="pre">if</span></code> 关键字开头的行,这样的行在不显示时面板时可能离开视野。 此面板的大小将根据需要扩展和收缩以显示当前层级的全部上下文,直至达到配置 IDLE 对话框中所定义的最大行数(默认为 15。 如果如果没有当前上下文行而此功能被启用,则将显示一个空行。 点击上下文面板中的某一行将把该行移至编辑器顶部。</p>
<p>上下文面板的文本和背景颜色可在配置 IDLE 对话框的 Highlights 选项卡中进行配置。</p>
</section>
<section id="python-shell-window">
<h3>Python Shell 窗口<a class="headerlink" href="#python-shell-window" title="永久链接至标题"></a></h3>
<p>通过 IDLE 的 Shell 可以输入、编辑和召回整条语句。 大部分控制台和终端在同一时刻只能处理单个物理行。</p>
<p>当向 Shell 粘贴代码时,它并不会被编译和执行,直到按下 <kbd class="kbd docutils literal notranslate">Return</kbd> 键。 在此之前可以先编辑所粘贴的代码。 如果向 Shell 粘贴了多行代码,多条语句会被当作一条语句来编译,结果将引发 <a class="reference internal" href="exceptions.html#SyntaxError" title="SyntaxError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">SyntaxError</span></code></a></p>
<p>之前小节中描述的编辑功能在交互式地输入代码时也可使用。 IDLE 的 Shell 窗口还会响应以下按键。</p>
<ul>
<li><p><kbd class="kbd docutils literal notranslate">C-c</kbd> 中断执行命令</p></li>
<li><p><kbd class="kbd docutils literal notranslate">C-d</kbd> 发送文件结束命令;如果在 <code class="docutils literal notranslate"><span class="pre">&gt;&gt;&gt;</span></code> 提示符后按下会关闭窗口。</p></li>
<li><p><kbd class="kbd docutils literal notranslate">Alt-/</kbd> (扩展单词) 也有助于减少输入量</p>
<p>历史命令</p>
<ul class="simple">
<li><p><kbd class="kbd docutils literal notranslate">Alt-p</kbd> 提取与你所输入键匹配的前一条命令。 在 macOS 上请使用 <kbd class="kbd docutils literal notranslate">C-p</kbd></p></li>
<li><p><kbd class="kbd docutils literal notranslate">Alt-n</kbd> 提取下一条命令。 在 macOS 上请使用 <kbd class="kbd docutils literal notranslate">C-n</kbd></p></li>
<li><p>在任意的前一条命令上按 <kbd class="kbd docutils literal notranslate">Return</kbd> 将提取该命令</p></li>
</ul>
</li>
</ul>
</section>
<section id="text-colors">
<h3>文本颜色<a class="headerlink" href="#text-colors" title="永久链接至标题"></a></h3>
<p>IDLE 文本默认为白底黑字,但有特殊含义的文本将以彩色显示。 对于 Shell 来说包括 Shell 输出Shell 错误,用户输出和用户错误。 对于 Shell 提示符下或编辑器中的 Python 代码来说则包括关键字,内置类和函数名称,<code class="docutils literal notranslate"><span class="pre">class</span></code><code class="docutils literal notranslate"><span class="pre">def</span></code> 之后的名称,字符串和注释等。 对于任意文本窗口来说则包括光标(如果存在)、找到的文本(如果可能)和选定的文本。</p>
<p>广西着色是在背景上完成的,因此有时会看到非着色的文本。 要改变颜色方案,请使用配置 IDLE 对话框的高亮选项卡。 编辑器中的调试器断点行标记和弹出面板和对话框中的文本则是用户不可配置的。</p>
</section>
</section>
<section id="startup-and-code-execution">
<h2>启动和代码执行<a class="headerlink" href="#startup-and-code-execution" title="永久链接至标题"></a></h2>
<p>在附带 <code class="docutils literal notranslate"><span class="pre">-s</span></code> 选项启用的情况下IDLE 将会执行环境变量 <span class="target" id="index-9"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">IDLESTARTUP</span></code><span class="target" id="index-10"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONSTARTUP"><code class="xref std std-envvar docutils literal notranslate"><span class="pre">PYTHONSTARTUP</span></code></a> 所引用的文件。 IDLE 会先检查 <code class="docutils literal notranslate"><span class="pre">IDLESTARTUP</span></code>;如果 <code class="docutils literal notranslate"><span class="pre">IDLESTARTUP</span></code> 存在则会运行被引用的文件。 如果 <code class="docutils literal notranslate"><span class="pre">IDLESTARTUP</span></code> 不存在,则 IDLE 会检查 <code class="docutils literal notranslate"><span class="pre">PYTHONSTARTUP</span></code>。 这些环境变量所引用的文件是存放经常被 IDLE Shell 所使用的函数,或者执行导入常用模块的 import 语句的便捷场所。</p>
<p>此外,<code class="docutils literal notranslate"><span class="pre">Tk</span></code> 也会在存在启动文件时加载它。 请注意 Tk 文件会被无条件地加载。 此附加文件名为 <code class="docutils literal notranslate"><span class="pre">.Idle.py</span></code> 并且会在用户的家目录下查找。 此文件中的语句将在 Tk 的命名空间中执行,所以此文件不适用于导入要在 IDLE 的 Python Shell 中使用的函数。</p>
<section id="command-line-usage">
<h3>命令行语法<a class="headerlink" href="#command-line-usage" title="永久链接至标题"></a></h3>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>idle.py [-c command] [-d] [-e] [-h] [-i] [-r file] [-s] [-t title] [-] [arg] ...
-c command run command in the shell window
-d enable debugger and open shell window
-e open editor window
-h print help message with legal combinations and exit
-i open shell window
-r file run file in shell window
-s run $IDLESTARTUP or $PYTHONSTARTUP first, in shell window
-t title set title of shell window
- run stdin in shell (- must be last option before args)
</pre></div>
</div>
<p>如果有参数:</p>
<ul class="simple">
<li><p>如果使用了 <code class="docutils literal notranslate"><span class="pre">-</span></code>, <code class="docutils literal notranslate"><span class="pre">-c</span></code><code class="docutils literal notranslate"><span class="pre">r</span></code>,则放在 <code class="docutils literal notranslate"><span class="pre">sys.argv[1:...]</span></code><code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code> 中的所有参数都会被设为 <code class="docutils literal notranslate"><span class="pre">''</span></code>, <code class="docutils literal notranslate"><span class="pre">'-c'</span></code><code class="docutils literal notranslate"><span class="pre">'-r'</span></code>。 不会打开任何编辑器窗口,即使是在选项对话框中的默认设置。</p></li>
<li><p>在其他情况下,参数为要打开编辑的文件而 <code class="docutils literal notranslate"><span class="pre">sys.argv</span></code> 反映的是传给 IDLE 本身的参数。</p></li>
</ul>
</section>
<section id="startup-failure">
<h3>启动失败<a class="headerlink" href="#startup-failure" title="永久链接至标题"></a></h3>
<p>IDLE 使用一个套接字在 IDLE GUI 进程和用户代码执行进程之间通信。 当 Shell 启动或重启动时必须建立一个连接。 (重启动会以一个内容为 'RESTART' 的分隔行来标示)。 如果用户进程无法连接到 GUI 进程,它通常会显示一个包含 'cannot connect' 消息的 <code class="docutils literal notranslate"><span class="pre">Tk</span></code> 错误提示框来引导用户。 随后将会退出程序。</p>
<p>有一个 Unix 系统专属的连接失败是由系统网络设置中错误配置的掩码规则导致的。 当从一个终端启动 IDLE 时,用户将看到一条以 <code class="docutils literal notranslate"><span class="pre">**</span> <span class="pre">Invalid</span> <span class="pre">host:</span></code> 开头的消息。 有效的值为 <code class="docutils literal notranslate"><span class="pre">127.0.0.1</span> <span class="pre">(idlelib.rpc.LOCALHOST)</span></code>。 用户可以在一个终端窗口输入 <code class="docutils literal notranslate"><span class="pre">tcpconnect</span> <span class="pre">-irv</span> <span class="pre">127.0.0.1</span> <span class="pre">6543</span></code> 并在另一个终端窗口中输入 <code class="docutils literal notranslate"><span class="pre">tcplisten</span> <span class="pre">&lt;same</span> <span class="pre">args&gt;</span></code> 来进行诊断。</p>
<p>导致连接失败的一个常见原因是用户创建的文件与标准库模块同名,例如 <em>random.py</em><em>tkinter.py</em>。 当这样的文件与要运行的文件位于同一目录中时IDLE 将无法导入标准库模块。 可用的解决办法是重命名用户文件。</p>
<p>虽然现在已不太常见,但杀毒软件或防火墙程序也有可能会阻止连接。 如果无法将此类程序设为允许连接,那么为了运行 IDLE 就必须将其关闭。 允许这样的内部连接是安全的,因为数据在外部端口上不可见。 一个类似的问题是错误的网络配置阻止了连接。</p>
<p>Python 的安装问题有时会使 IDLE 退出:存在多个版本时可能导致程序崩溃,或者单独安装时可能需要管理员权限。 如果想要避免程序崩溃,或是不想以管理员身份运行,最简单的做法是完全卸载 Python 并重新安装。</p>
<p>有时会出现 pythonw.exe 僵尸进程问题。 在 Windows 上,可以使用任务管理员来检查并停止该进程。 有时由程序崩溃或键盘中断control-C所发起的重启动可能会出现连接失败。 关闭错误提示框或使用 Shell 菜单中的 Restart Shell 可能会修复此类临时性错误。</p>
<p>当 IDLE 首次启动时,它会尝试读取 <code class="docutils literal notranslate"><span class="pre">~/.idlerc/</span></code> 中的用户配置文件(~ 是用户的家目录)。 如果配置有问题,则应当显示一条错误消息。 除随机磁盘错误之外,此类错误均可通过不手动编辑这些文件来避免。 请使用 Options 菜单来打开配置对话框。 一旦用户配置文件出现错误,最好的解决办法就是删除它并使用配置对话框重新设置。</p>
<p>如果 IDLE 退出时没有发出任何错误消息,并且它不是通过控制台启动的,请尝试通过控制台或终端 (<code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">idlelib</span></code>) 来启动它以查看是否会出现错误消息。</p>
<p>在基于 Unix 的系统上使用 tcl/tk 低于 <code class="docutils literal notranslate"><span class="pre">8.6.11</span></code> 的版本 (查看 <code class="docutils literal notranslate"><span class="pre">About</span> <span class="pre">IDLE</span></code>) 时特定字体的特定字符可能导致终端提示 tk 错误消息。 这可能发生在启动 IDLE 编辑包此种字符的文件或是在之后输入此种字符的时候。 如果无法升级 tcl/tk可以重新配置 IDLE 来使用其他的字体。</p>
</section>
<section id="running-user-code">
<h3>运行用户代码<a class="headerlink" href="#running-user-code" title="永久链接至标题"></a></h3>
<p>除了少量例外,使用 IDLE 执行 Python 代码的结果应当与使用默认方法,即在文本模式的系统控制台或终端窗口中直接通过 Python 解释器执行同样的代码相同。 但是,不同的界面和操作有时会影响显示的结果。 例如,<code class="docutils literal notranslate"><span class="pre">sys.modules</span></code> 初始时具有更多条目,而 <code class="docutils literal notranslate"><span class="pre">threading.active_count()</span></code> 将返回 2 而不是 1。</p>
<p>在默认情况下IDLE 会在单独的 OS 进程中运行用户代码而不是在运行 Shell 和编辑器的用户界面进程中运行。 在执行进程中,它会将 <code class="docutils literal notranslate"><span class="pre">sys.stdin</span></code>, <code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code><code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code> 替换为从 Shell 窗口获取输入并向其发送输出的对象。 保存在 <code class="docutils literal notranslate"><span class="pre">sys.__stdin__</span></code>, <code class="docutils literal notranslate"><span class="pre">sys.__stdout__</span></code><code class="docutils literal notranslate"><span class="pre">sys.__stderr__</span></code> 中的原始值不会被改变,但可能会为 <code class="docutils literal notranslate"><span class="pre">None</span></code></p>
<p>将打印输出从一个进程发送到另一个进程中的文本部件要比打印到同一个进程中的系统终端慢。 这在打印多个参数时将有更明显的影响,因为每个参数、每个分隔符和换行符对应的字符串都要单独发送。 在开发中,这通常不算是问题,但如果希望能在 IDLE 中更快地打印,可以将想要显示的所有内容先格式化并拼接到一起然后打印单个字符串。 格式字符串和 <a class="reference internal" href="stdtypes.html#str.join" title="str.join"><code class="xref py py-meth docutils literal notranslate"><span class="pre">str.join()</span></code></a> 都可以被用于合并字段和文本行。</p>
<p>IDLE 的标准流替换不会被执行进程中创建的子进程所继承,不论它是由用户代码直接创建还是由 multiprocessing 之类的模块创建的。 如果这样的子进程使用了 <code class="docutils literal notranslate"><span class="pre">input</span></code> 获取 sys.stdin 或者使用了 <code class="docutils literal notranslate"><span class="pre">print</span></code><code class="docutils literal notranslate"><span class="pre">write</span></code> 输出到 sys.stdout 或 sys.stderr则应当在命令行窗口中启动 IDLE。 这样二级子进程将会被附加到该窗口进行输出和输出。</p>
<p>如果 <code class="docutils literal notranslate"><span class="pre">sys</span></code> 被用户代码重置,例如使用了 <code class="docutils literal notranslate"><span class="pre">importlib.reload(sys)</span></code>,则 IDLE 的修改将丢失,来自键盘的输入和向屏幕的输出将无法正确运作。</p>
<p>当 Shell 获得焦点时,它将控制键盘与屏幕。 这通常会保持透明,但一些直接访问键盘和屏幕的函数将会不起作用。 这也包括那些确定是否有键被按下以及是哪个键被按下的系统专属函数。</p>
<p>在执行进程中运行的 IDLE 代码会向调用栈添加在其他情况下不存在的帧。 IDLE 包装了 <code class="docutils literal notranslate"><span class="pre">sys.getrecursionlimit</span></code><code class="docutils literal notranslate"><span class="pre">sys.setrecursionlimit</span></code> 以减少额外栈帧的影响。</p>
<p>当用户代码直接或者通过调用 sys.exit 引发 SystemExit 时IDLE 将返回 Shell 提示符而非完全退出。</p>
</section>
<section id="user-output-in-shell">
<h3>Shell中的用户输出<a class="headerlink" href="#user-output-in-shell" title="永久链接至标题"></a></h3>
<p>当一个程序输出文本时,结果将由相应的输出设备来确定。 当 IDLE 执行用户代码时,<code class="docutils literal notranslate"><span class="pre">sys.stdout</span></code><code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code> 会被连接到 IDLE Shell 的显示区。 它的某些特性是从底层的 Tk Text 部件继承而来。 其他特性则是程序所添加的。 总之Shell 被设计用于开发环境而非生产环境运行。</p>
<p>例如Shell 绝不会丢弃输出。 一个向 Shell 发送无限输出的程序将最终占满内存,导致内存错误。 作为对比,某些系统文本模式窗口只会保留输出的最后 n 行。 例如Windows 控制台可由用户设置保留 1 至 9999 行,默认为 300 行。</p>
<p>A Tk Text widget, and hence IDLE's Shell, displays characters (codepoints) in
the BMP (Basic Multilingual Plane) subset of Unicode. Which characters are
displayed with a proper glyph and which with a replacement box depends on the
operating system and installed fonts. Tab characters cause the following text
to begin after the next tab stop. (They occur every 8 'characters'). Newline
characters cause following text to appear on a new line. Other control
characters are ignored or displayed as a space, box, or something else,
depending on the operating system and font. (Moving the text cursor through
such output with arrow keys may exhibit some surprising spacing behavior.)</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="o">=</span> <span class="s1">&#39;a</span><span class="se">\t</span><span class="s1">b</span><span class="se">\a</span><span class="s1">&lt;</span><span class="se">\x02</span><span class="s1">&gt;&lt;</span><span class="se">\r</span><span class="s1">&gt;</span><span class="se">\b</span><span class="s1">c</span><span class="se">\n</span><span class="s1">d&#39;</span> <span class="c1"># Enter 22 chars.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
<span class="go">14</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span> <span class="c1"># Display repr(s)</span>
<span class="go">&#39;a\tb\x07&lt;\x02&gt;&lt;\r&gt;\x08c\nd&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">s</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">)</span> <span class="c1"># Display s as is.</span>
<span class="go"># Result varies by OS and font. Try it.</span>
</pre></div>
</div>
<p><code class="docutils literal notranslate"><span class="pre">repr</span></code> 函数会被用于表达式值的交互式回显。 它将返回输入字符串的一个修改版本,其中的控制代码、部分 BMP 码位以及所有非 BMP 码位都将被替换为转义代码。 如上面所演示的,它使用户可以辨识字符串中的字符,无论它们会如何显示。</p>
<p>普通的与错误的输出通常会在与代码输入和彼此之间保持区分 (显示于不同的行)。 它们也会分别使用不同的高亮颜色。</p>
<p>对于 SyntaxError 回溯信息,表示检测到错误位置的正常 '^' 标记被替换为带有代表错误的文本颜色高亮。 当从文件运行的代码导致了其他异常时,用户可以右击回溯信息行在 IDLE 编辑器中跳转到相应的行。 如有必要将打开相应的文件。</p>
<p>Shell 具有将输出行折叠为一个 'Squeezed text' 标签的特殊功能。 此功能将自动应用于超过 N 行的输出 (默认 N = 50)。 N 可以在设置对话框中 General 页的 PyShell 区域中修改。 行数更少的输出也可通过在输出上右击来折叠。 此功能适用于过多的输出行数导致滚动操作变慢的情况。</p>
<p>已折叠的输出可通过双击该标签来原地展开。 也可通过右击该标签将其发送到剪贴板或单独的查看窗口。</p>
</section>
<section id="developing-tkinter-applications">
<h3>开发 tkinter 应用程序<a class="headerlink" href="#developing-tkinter-applications" title="永久链接至标题"></a></h3>
<p>IDLE 有意与标准 Python 保持区别以方便 tkinter 程序的开发。 在标准 Python 中输入 <code class="docutils literal notranslate"><span class="pre">import</span> <span class="pre">tkinter</span> <span class="pre">as</span> <span class="pre">tk;</span> <span class="pre">root</span> <span class="pre">=</span> <span class="pre">tk.Tk()</span></code> 不会显示任何东西。 在 IDLE 中输入同样的代码则会显示一个 tk 窗口。 在标准 Python 中,还必须输入 <code class="docutils literal notranslate"><span class="pre">root.update()</span></code> 才会将窗口显示出来。 IDLE 会在幕后执行同样的方法,每秒大约 20 次,即每隔大约 50 毫秒。 下面输入 <code class="docutils literal notranslate"><span class="pre">b</span> <span class="pre">=</span> <span class="pre">tk.Button(root,</span> <span class="pre">text='button');</span> <span class="pre">b.pack()</span></code>。 在标准 Python 中仍然不会有任何可见的变化,直到输入 <code class="docutils literal notranslate"><span class="pre">root.update()</span></code></p>
<p>大多数 tkinter 程序都会运行 <code class="docutils literal notranslate"><span class="pre">root.mainloop()</span></code>,它通常直到 tk 应用被销毁时才会返回。 如果程序是通过 <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-i</span></code> 或 IDLE 编辑器运行的,则 <code class="docutils literal notranslate"><span class="pre">&gt;&gt;&gt;</span></code> Shell 提示符将直到 <code class="docutils literal notranslate"><span class="pre">mainloop()</span></code> 返回时才会出现,这时将结束程序的交互。</p>
<p>当通过 IDLE 编辑器运行 tkinter 程序时,可以注释掉 mainloop 调用。 这样将立即回到 Shell 提示符并可与正在运行的应用程序交互。 请记得当在标准 Python 中运行时重新启用 mainloop 调用。</p>
</section>
<section id="running-without-a-subprocess">
<h3>在没有子进程的情况下运行<a class="headerlink" href="#running-without-a-subprocess" title="永久链接至标题"></a></h3>
<p>By default, IDLE executes user code in a separate subprocess via a socket,
which uses the internal loopback interface. This connection is not
externally visible and no data is sent to or received from the Internet.
If firewall software complains anyway, you can ignore it.</p>
<p>如果创建套接字连接的尝试失败IDLE 将会通知你。 这样的失败可能是暂时性的,但是如果持续存在,问题可能是防火墙阻止了连接或某个系统配置错误。 在问题得到解决之前,可以使用 -n 命令行开关来运行 IDLE。</p>
<p>如果 IDLE 启动时使用了 -n 命令行开关则它将在单个进程中运行并且将不再创建运行 RPC Python 执行服务器的子进程。 这适用于 Python 无法在你的系统平台上创建子进程或 RPC 套接字接口的情况。 但是,在这种模式下用户代码没有与 IDLE 本身相隔离。 而且,当选择 Run/Run Module (F5) 时运行环境也不会重启。 如果你的代码已被修改,你必须为受影响的模块执行 reload() 并重新导入特定的条目 (例如 from foo import baz) 才能让修改生效。 出于这些原因,在可能的情况下最好还是使用默认的子进程来运行 IDLE。</p>
<div class="deprecated">
<p><span class="versionmodified deprecated">3.4 版后已移除.</span></p>
</div>
</section>
</section>
<section id="help-and-preferences">
<h2>帮助和偏好<a class="headerlink" href="#help-and-preferences" title="永久链接至标题"></a></h2>
<section id="help-sources">
<span id="id6"></span><h3>帮助源<a class="headerlink" href="#help-sources" title="永久链接至标题"></a></h3>
<p>Help 菜单项 &quot;IDLE Help&quot; 会显示标准库参考中 IDLE 一章的带格式 HTML 版本。 这些内容放在只读的 tkinter 文本窗口中,与在浏览器中看到的内容类似。 可使用鼠标滚轮、滚动条或上下方向键来浏览文本。 或是点击 TOC (Table of Contents) 按钮并在打开的选项框中选择一个节标题。</p>
<p>Help 菜单项 &quot;Python Docs&quot; 会打开更丰富的帮助源,包括教程,通过 <code class="docutils literal notranslate"><span class="pre">docs.python.org/x.y</span></code> 来访问,其中 'x.y' 是当前运行的 Python 版本。 如果你的系统有此文档的离线副本 (这可能是一个安装选项),则将打开这个副本。</p>
<p>选定的 URL 可以使用配置 IDLE 对话框的 General 选项卡随时在帮助菜单中添加或移除。</p>
</section>
<section id="setting-preferences">
<span id="preferences"></span><h3>首选项设置<a class="headerlink" href="#setting-preferences" title="永久链接至标题"></a></h3>
<p>字体首选项、高亮、按键和通用首选项可通过 Option 菜单的配置 IDLE 项来修改。 非默认的用户设置将保存在用户家目录下的 <code class="docutils literal notranslate"><span class="pre">.idlerc</span></code> 目录中。 用户配置文件错误导致的问题可通过编辑或删除 <code class="docutils literal notranslate"><span class="pre">.idlerc</span></code> 中的一个或多个文件来解决。</p>
<p>在 Font 选项卡中,可以查看使用多种语言的多个字符的示例文本来了解字体或字号效果。 可以编辑示例文本来添加想要的其他字符。 请使用示例文本选择等宽字体。 如果某些字符在 Shell 或编辑器中的显示有问题,可以将它们添加到示例文本的开头并尝试改变字号和字体。</p>
<p>在 Highlights 和 Keys 选项卡中,可以选择内置或自定义的颜色主题和按键集合。 要将更新的内置颜色主题或按键集合与旧版 IDLE 一起使用,可以将其保存为新的自定义主题或按键集合就将可在旧版 IDLE 中使用。</p>
</section>
<section id="idle-on-macos">
<h3>macOS 上的IDLE<a class="headerlink" href="#idle-on-macos" title="永久链接至标题"></a></h3>
<p>在 System Preferences: Dock 中,可以将 &quot;Prefer tabs when opening documents&quot; 设为 &quot;Always&quot;。 但是该设置不能兼容 IDLE 所使用的 tk/tkinter GUI 框架,并会使得部分 IDLE 特性失效。</p>
</section>
<section id="extensions">
<h3>扩展<a class="headerlink" href="#extensions" title="永久链接至标题"></a></h3>
<p>IDLE 可以包含扩展插件。 扩展插件的首选项可通过首选项对话框的 Extensions 选项卡来修改。 请查看 idlelib 目录下 config-extensions.def 的开头来了解详情。 目前唯一的扩展插件是 zzdummy它也是一个测试用的示例。</p>
</section>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../contents.html">目录</a></h3>
<ul>
<li><a class="reference internal" href="#">IDLE</a><ul>
<li><a class="reference internal" href="#menus">目录</a><ul>
<li><a class="reference internal" href="#file-menu-shell-and-editor">文件菜单 (命令行和编辑器)</a></li>
<li><a class="reference internal" href="#edit-menu-shell-and-editor">编辑菜单(命令行和编辑器)</a></li>
<li><a class="reference internal" href="#format-menu-editor-window-only">格式菜单(仅 window 编辑器)</a></li>
<li><a class="reference internal" href="#run-menu-editor-window-only">运行菜单(仅 window 编辑器)</a></li>
<li><a class="reference internal" href="#shell-menu-shell-window-only">Shell 菜单(仅 window 编辑器)</a></li>
<li><a class="reference internal" href="#debug-menu-shell-window-only">调试菜单(仅 window 编辑器)</a></li>
<li><a class="reference internal" href="#options-menu-shell-and-editor">选项菜单(命令行和编辑器)</a></li>
<li><a class="reference internal" href="#window-menu-shell-and-editor">Window 菜单(命令行和编辑器)</a></li>
<li><a class="reference internal" href="#help-menu-shell-and-editor">帮助菜单(命令行和编辑器)</a></li>
<li><a class="reference internal" href="#context-menus">上下文菜单</a></li>
</ul>
</li>
<li><a class="reference internal" href="#editing-and-navigation">编辑和导航</a><ul>
<li><a class="reference internal" href="#editor-windows">编辑窗口</a></li>
<li><a class="reference internal" href="#key-bindings">按键绑定</a></li>
<li><a class="reference internal" href="#automatic-indentation">自动缩进</a></li>
<li><a class="reference internal" href="#completions">完成</a></li>
<li><a class="reference internal" href="#calltips">提示</a></li>
<li><a class="reference internal" href="#code-context">代码上下文</a></li>
<li><a class="reference internal" href="#python-shell-window">Python Shell 窗口</a></li>
<li><a class="reference internal" href="#text-colors">文本颜色</a></li>
</ul>
</li>
<li><a class="reference internal" href="#startup-and-code-execution">启动和代码执行</a><ul>
<li><a class="reference internal" href="#command-line-usage">命令行语法</a></li>
<li><a class="reference internal" href="#startup-failure">启动失败</a></li>
<li><a class="reference internal" href="#running-user-code">运行用户代码</a></li>
<li><a class="reference internal" href="#user-output-in-shell">Shell中的用户输出</a></li>
<li><a class="reference internal" href="#developing-tkinter-applications">开发 tkinter 应用程序</a></li>
<li><a class="reference internal" href="#running-without-a-subprocess">在没有子进程的情况下运行</a></li>
</ul>
</li>
<li><a class="reference internal" href="#help-and-preferences">帮助和偏好</a><ul>
<li><a class="reference internal" href="#help-sources">帮助源</a></li>
<li><a class="reference internal" href="#setting-preferences">首选项设置</a></li>
<li><a class="reference internal" href="#idle-on-macos">macOS 上的IDLE</a></li>
<li><a class="reference internal" href="#extensions">扩展</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="tkinter.scrolledtext.html"
title="上一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.scrolledtext</span></code> --- 滚动文字控件</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="othergui.html"
title="下一章">其他图形用户界面GUI</a></p>
<div role="note" aria-label="source link">
<h3>本页</h3>
<ul class="this-page-menu">
<li><a href="../bugs.html">报告 Bug</a></li>
<li>
<a href="https://github.com/python/cpython/blob/3.8/Doc/library/idle.rst"
rel="nofollow">显示源代码
</a>
</li>
</ul>
</div>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="related" role="navigation" aria-label="related navigation">
<h3>导航</h3>
<ul>
<li class="right" style="margin-right: 10px">
<a href="../genindex.html" title="总目录"
>索引</a></li>
<li class="right" >
<a href="../py-modindex.html" title="Python 模块索引"
>模块</a> |</li>
<li class="right" >
<a href="othergui.html" title="其他图形用户界面GUI包"
>下一页</a> |</li>
<li class="right" >
<a href="tkinter.scrolledtext.html" title="tkinter.scrolledtext --- 滚动文字控件"
>上一页</a> |</li>
<li><img src="../_static/py.svg" alt="python logo" style="vertical-align: middle; margin-top: -1px"/></li>
<li><a href="https://www.python.org/">Python</a> &#187;</li>
<li class="switchers">
<div class="language_switcher_placeholder"></div>
<div class="version_switcher_placeholder"></div>
</li>
<li>
</li>
<li id="cpython-language-and-version">
<a href="../index.html">3.8.20 Documentation</a> &#187;
</li>
<li class="nav-item nav-item-1"><a href="index.html" >Python 标准库</a> &#187;</li>
<li class="nav-item nav-item-2"><a href="tk.html" >Tk图形用户界面(GUI)</a> &#187;</li>
<li class="right">
<div class="inline-search" role="search">
<form class="inline-search" action="../search.html" method="get">
<input placeholder="快速搜索" aria-label="快速搜索" type="text" name="q" />
<input type="submit" value="转向" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
|
</li>
</ul>
</div>
<div class="footer">
&copy; <a href="../copyright.html">版权所有</a> 2001-2024, Python Software Foundation.
<br />
This page is licensed under the Python Software Foundation License Version 2.
<br />
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
<br />
<br />
The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
<br />
最后更新于 12月 09, 2024.
<a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
<br />
Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.4.4.
</div>
</body>
</html>