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

888 lines
84 KiB
HTML
Raw 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>tkinter —— Tcl/Tk 的 Python 接口 &#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="tkinter.ttk --- Tk 风格的控件" href="tkinter.ttk.html" />
<link rel="prev" title="Tk图形用户界面(GUI)" href="tk.html" />
<link rel="canonical" href="https://docs.python.org/3/library/tkinter.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="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> —— Tcl/Tk 的 Python 接口</a><ul>
<li><a class="reference internal" href="#tkinter-modules">Tkinter 模块</a></li>
<li><a class="reference internal" href="#tkinter-life-preserver">Tkinter 拾遗</a><ul>
<li><a class="reference internal" href="#how-to-use-this-section">本节内容的用法</a></li>
<li><a class="reference internal" href="#a-simple-hello-world-program">简单的 Hello World 程序</a></li>
</ul>
</li>
<li><a class="reference internal" href="#a-very-quick-look-at-tcl-tk">Tcl/Tk 速览</a></li>
<li><a class="reference internal" href="#mapping-basic-tk-into-tkinter">将简单的 Tk 映射到 Tkinter</a></li>
<li><a class="reference internal" href="#how-tk-and-tkinter-are-related">Tk 和 Tkinter 如何关联</a></li>
<li><a class="reference internal" href="#handy-reference">快速参考</a><ul>
<li><a class="reference internal" href="#setting-options">可选配置项</a></li>
<li><a class="reference internal" href="#the-packer">包装器</a></li>
<li><a class="reference internal" href="#packer-options">包装器的参数</a></li>
<li><a class="reference internal" href="#coupling-widget-variables">部件与变量的关联</a></li>
<li><a class="reference internal" href="#the-window-manager">窗口管理器</a></li>
<li><a class="reference internal" href="#tk-option-data-types">Tk 参数的数据类型</a></li>
<li><a class="reference internal" href="#bindings-and-events">绑定和事件</a></li>
<li><a class="reference internal" href="#the-index-parameter">index 参数</a></li>
<li><a class="reference internal" href="#images">图片</a></li>
</ul>
</li>
<li><a class="reference internal" href="#file-handlers">文件处理程序</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="tk.html"
title="上一章">Tk图形用户界面(GUI)</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="tkinter.ttk.html"
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.ttk</span></code> --- Tk 风格的控件</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/tkinter.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/tkinter.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="tkinter.ttk.html" title="tkinter.ttk --- Tk 风格的控件"
accesskey="N">下一页</a> |</li>
<li class="right" >
<a href="tk.html" title="Tk图形用户界面(GUI)"
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="module-tkinter">
<span id="tkinter-python-interface-to-tcl-tk"></span><h1><a class="reference internal" href="#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> —— Tcl/Tk 的 Python 接口<a class="headerlink" href="#module-tkinter" title="永久链接至标题"></a></h1>
<p><strong>源代码:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.8/Lib/tkinter/__init__.py">Lib/tkinter/__init__.py</a></p>
<hr class="docutils" />
<p>The <a class="reference internal" href="#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> package (&quot;Tk interface&quot;) is the standard Python interface to
the Tk GUI toolkit. Both Tk and <a class="reference internal" href="#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> are available on most Unix
platforms, as well as on Windows systems. (Tk itself is not part of Python; it
is maintained at ActiveState.)</p>
<p>若在命令行执行 <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">tkinter</span></code>,应会弹出一个简单的 Tk 界面窗口, 表明 <a class="reference internal" href="#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> 包已安装完成,还会显示当前安装的 Tcl/Tk 版本,以便阅读对应版本的 Tcl/Tk 文档。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<p>Tkinter文档</p>
<dl class="simple">
<dt><a class="reference external" href="https://wiki.python.org/moin/TkInter">Python Tkinter 资源</a></dt><dd><p>The Python Tkinter Topic Guide 提供了在 Python 中使用 Tk 的很多信息, 同时包含了Tk其他信息的链接。</p>
</dd>
<dt><a class="reference external" href="http://www.tkdocs.com/">TKDocs</a></dt><dd><p>大量的教程,部分可视化组件的介绍说明。</p>
</dd>
<dt><a class="reference external" href="https://www.tkdocs.com/shipman/">Tkinter 8.5 参考手册:一种 Python GUI</a></dt><dd><p>在线参考资料。</p>
</dd>
<dt><a class="reference external" href="http://effbot.org/tkinterbook/">Tkinter docs from effbot</a></dt><dd><p>effbot.org 提供的 tkinter 在线参考资料。</p>
</dd>
<dt><a class="reference external" href="http://learning-python.com/about-pp4e.html">使用 Python 编程</a></dt><dd><p>由 Mark Lutz 所著的书籍,对 Tkinter 进行了完美的介绍。</p>
</dd>
<dt><a class="reference external" href="https://www.amazon.com/Modern-Tkinter-Python-Developers-ebook/dp/B0071QDNLO/">为繁忙的 Python 开发者所准备的现代 Tkinter</a></dt><dd><p>由 Mark Rozerman 所著的关于如何使用 Python 和 Tkinter 来搭建有吸引力的和现代化的图形用户界面的书籍</p>
</dd>
<dt><a class="reference external" href="https://www.manning.com/books/python-and-tkinter-programming">Python 和 Tkinter 编程</a></dt><dd><p>作者: John Grayson (ISBN 1-884777-81-3).</p>
</dd>
</dl>
<p>Tcl/Tk 文档:</p>
<dl class="simple">
<dt><a class="reference external" href="https://www.tcl.tk/man/tcl8.6/TkCmd/contents.htm">Tk 命令</a></dt><dd><p>多数命令以 <a class="reference internal" href="#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> 或者 <a class="reference internal" href="tkinter.ttk.html#module-tkinter.ttk" title="tkinter.ttk: Tk themed widget set"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.ttk</span></code></a> 类的形式存在。改变 '8.6' 以匹配所安装的 Tcl/Tk 版本。</p>
</dd>
<dt><a class="reference external" href="https://www.tcl.tk/doc/">Tcl/Tk 最新手册页面</a></dt><dd><p>www.tcl.tk 上面最新的 Tcl/Tk 手册。</p>
</dd>
<dt><a class="reference external" href="https://tcl.tk">ActiveState Tcl 主页</a></dt><dd><p>Tk/Tcl 的多数开发工作发生在 ActiveState 。</p>
</dd>
<dt><a class="reference external" href="https://www.amazon.com/exec/obidos/ASIN/020163337X">Tcl 及 Tk 工具集</a></dt><dd><p>由 Tcl 发明者 John Ousterhout 所著的书籍。</p>
</dd>
<dt>` Tcl 和 Tk 编程实战 &lt;<a class="reference external" href="http://www.beedub.com/book/">http://www.beedub.com/book/</a>&gt;`_</dt><dd><p>Brent Welch 所著的百科全局式书籍。</p>
</dd>
</dl>
</div>
<section id="tkinter-modules">
<h2>Tkinter 模块<a class="headerlink" href="#tkinter-modules" title="永久链接至标题"></a></h2>
<p>在大多数时候你只需要 <a class="reference internal" href="#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> 就足够了但也有一些额外的模块可供使用。Tk 接口位于一个名字 <code class="xref py py-mod docutils literal notranslate"><span class="pre">_tkinter</span></code> 的二进制模块当中。此模块包含了低层级的 Tk 接口,它不应该被应用程序员所直接使用。它通常是一个共享库(或 DLL但在某些情况下也可能被静态链接到 Python 解释器。</p>
<p>除了Tk接口 <a class="reference internal" href="#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> 也包含了若干 Python 模块,<code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.constants</span></code> 是其中最重要的。导入 <a class="reference internal" href="#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> 会自动导入 <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.constants</span></code> ,所以,要使用 Tkinter 通常你只需要一条简单的 import 语句:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tkinter</span>
</pre></div>
</div>
<p>或者更常用的:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">tkinter</span> <span class="kn">import</span> <span class="o">*</span>
</pre></div>
</div>
<dl class="class">
<dt id="tkinter.Tk">
<em class="property">class </em><code class="sig-prename descclassname">tkinter.</code><code class="sig-name descname">Tk</code><span class="sig-paren">(</span><em class="sig-param">screenName=None</em>, <em class="sig-param">baseName=None</em>, <em class="sig-param">className='Tk'</em>, <em class="sig-param">useTk=1</em><span class="sig-paren">)</span><a class="headerlink" href="#tkinter.Tk" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#tkinter.Tk" title="tkinter.Tk"><code class="xref py py-class docutils literal notranslate"><span class="pre">Tk</span></code></a> 类被初始化时无参数。此时会创建一个 Tk 顶级控件,通常是应用程序的主窗口。每个实例都有自己关联的 Tcl 解释器。</p>
</dd></dl>
<dl class="function">
<dt id="tkinter.Tcl">
<code class="sig-prename descclassname">tkinter.</code><code class="sig-name descname">Tcl</code><span class="sig-paren">(</span><em class="sig-param">screenName=None</em>, <em class="sig-param">baseName=None</em>, <em class="sig-param">className='Tk'</em>, <em class="sig-param">useTk=0</em><span class="sig-paren">)</span><a class="headerlink" href="#tkinter.Tcl" title="永久链接至目标"></a></dt>
<dd><p><a class="reference internal" href="#tkinter.Tcl" title="tkinter.Tcl"><code class="xref py py-func docutils literal notranslate"><span class="pre">Tcl()</span></code></a> 函数是一个工厂函数,它创建的对象类似于 <a class="reference internal" href="#tkinter.Tk" title="tkinter.Tk"><code class="xref py py-class docutils literal notranslate"><span class="pre">Tk</span></code></a> 类创建的,只是不会初始化 Tk 子系统。这在调动 Tcl 解释器时最为有用,这时不想创建多余的顶层窗口,或者无法创建(比如不带 X 服务的 Unix/Linux 系统)。由 <a class="reference internal" href="#tkinter.Tcl" title="tkinter.Tcl"><code class="xref py py-func docutils literal notranslate"><span class="pre">Tcl()</span></code></a> 创建的对象可调用 <code class="xref py py-meth docutils literal notranslate"><span class="pre">loadtk()</span></code> 方法创建一个顶层窗口(且会初始化 Tk 子系统)。</p>
</dd></dl>
<p>提供Tk支持的其他模块包括</p>
<dl class="simple">
<dt><a class="reference internal" href="tkinter.scrolledtext.html#module-tkinter.scrolledtext" title="tkinter.scrolledtext: Text widget with a vertical scroll bar. (Tk)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.scrolledtext</span></code></a></dt><dd><p>内置纵向滚动条的文本组件。</p>
</dd>
<dt><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.colorchooser</span></code></dt><dd><p>让用户选择颜色的对话框。</p>
</dd>
<dt><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.commondialog</span></code></dt><dd><p>本文其他模块定义的对话框的基类。</p>
</dd>
<dt><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.filedialog</span></code></dt><dd><p>允许用户指定文件的通用对话框,用于打开或保存文件。</p>
</dd>
<dt><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.font</span></code></dt><dd><p>帮助操作字体的工具。</p>
</dd>
<dt><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.messagebox</span></code></dt><dd><p>访问标准的 Tk 对话框。</p>
</dd>
<dt><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.simpledialog</span></code></dt><dd><p>基础对话框和一些便捷功能。</p>
</dd>
<dt><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.dnd</span></code></dt><dd><p><a class="reference internal" href="#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> 提供拖放支持。这是实验性功能,当被 Tk DND 取代时应被废弃。</p>
</dd>
<dt><a class="reference internal" href="turtle.html#module-turtle" title="turtle: An educational framework for simple graphics applications"><code class="xref py py-mod docutils literal notranslate"><span class="pre">turtle</span></code></a></dt><dd><p>Tk 窗口中的海龟绘图库。</p>
</dd>
</dl>
</section>
<section id="tkinter-life-preserver">
<h2>Tkinter 拾遗<a class="headerlink" href="#tkinter-life-preserver" title="永久链接至标题"></a></h2>
<p>本节不应作为 Tk 或 Tkinter 的详尽教程。而只是一个补充,提供一些系统指南。</p>
<p>致谢:</p>
<ul class="simple">
<li><p>Tk 是 John Ousterhout 在伯克利大学时写的。</p></li>
<li><p>Tkinter 是由 Steen Lumholt 和 Guido van Rossum 编写的。</p></li>
<li><p>本拾遗由弗吉尼亚大学的 Matt Conway 撰写。</p></li>
<li><p>HTML 渲染和一些自由编辑功能,是由 Ken Manheimer 根据 FrameMaker 创建的。</p></li>
<li><p>Fredrik Lundh 认真研究并修改了类的接口描述,使其与 Tk 4.2 保持一致。</p></li>
<li><p>Mike Clarkson 将文档转换为 LaTeX 格式,并编写了参考手册的用户界面章节。</p></li>
</ul>
<section id="how-to-use-this-section">
<h3>本节内容的用法<a class="headerlink" href="#how-to-use-this-section" title="永久链接至标题"></a></h3>
<p>本节分为两部分:前半部分(大致)涵盖了背景材料,后半部分可以作为手头的参考手册。</p>
<p>当试图回答“如何才能怎么怎么”这种问题时,通常最好是弄清楚如何直接在 Tk 中实现,然后转换回相应的 <a class="reference internal" href="#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> 调用。Python 程序员通常可查看 Tk 文档来猜测正确的 Python 命令。这意味着要用好 Tkinter必须对 Tk 有一定的了解。本文无法完成这个任务,所以最好的做法就是给出目前最好的文档。下面给出一些小提示:</p>
<ul class="simple">
<li><p>作者强烈建议用户先拿到一份 Tk 手册。具体来说,<code class="docutils literal notranslate"><span class="pre">manN</span></code> 目录下的 man 文档是最有用的。<code class="docutils literal notranslate"><span class="pre">man3</span></code> 的 man 文档描述了 Tk 库的 C 接口,因此对脚本编写人员没有什么特别的帮助。</p></li>
<li><p>Addison-Wesley 出版了一本名为《Tcl 和 Tk 工具包》的书,作者是 John OusterhoutISBN 0-201-63337-X对于新手来说这是一本很好的 Tcl 和Tk 介绍书籍。该书不算详尽,很多细节还是要看 man 手册。</p></li>
<li><p>对大多数人而言,<code class="file docutils literal notranslate"><span class="pre">tkinter/__init__.py</span></code> 是最后一招,但其他手段都无效时也不失为一个好去处。</p></li>
</ul>
</section>
<section id="a-simple-hello-world-program">
<h3>简单的 Hello World 程序<a class="headerlink" href="#a-simple-hello-world-program" title="永久链接至标题"></a></h3>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tkinter</span> <span class="k">as</span> <span class="nn">tk</span>
<span class="k">class</span> <span class="nc">Application</span><span class="p">(</span><span class="n">tk</span><span class="o">.</span><span class="n">Frame</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">master</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">master</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">master</span> <span class="o">=</span> <span class="n">master</span>
<span class="bp">self</span><span class="o">.</span><span class="n">pack</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">create_widgets</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">create_widgets</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="bp">self</span><span class="o">.</span><span class="n">hi_there</span> <span class="o">=</span> <span class="n">tk</span><span class="o">.</span><span class="n">Button</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">hi_there</span><span class="p">[</span><span class="s2">&quot;text&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;Hello World</span><span class="se">\n</span><span class="s2">(click me)&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">hi_there</span><span class="p">[</span><span class="s2">&quot;command&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">say_hi</span>
<span class="bp">self</span><span class="o">.</span><span class="n">hi_there</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="n">side</span><span class="o">=</span><span class="s2">&quot;top&quot;</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">quit</span> <span class="o">=</span> <span class="n">tk</span><span class="o">.</span><span class="n">Button</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">text</span><span class="o">=</span><span class="s2">&quot;QUIT&quot;</span><span class="p">,</span> <span class="n">fg</span><span class="o">=</span><span class="s2">&quot;red&quot;</span><span class="p">,</span>
<span class="n">command</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">master</span><span class="o">.</span><span class="n">destroy</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">quit</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="n">side</span><span class="o">=</span><span class="s2">&quot;bottom&quot;</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">say_hi</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;hi there, everyone!&quot;</span><span class="p">)</span>
<span class="n">root</span> <span class="o">=</span> <span class="n">tk</span><span class="o">.</span><span class="n">Tk</span><span class="p">()</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">Application</span><span class="p">(</span><span class="n">master</span><span class="o">=</span><span class="n">root</span><span class="p">)</span>
<span class="n">app</span><span class="o">.</span><span class="n">mainloop</span><span class="p">()</span>
</pre></div>
</div>
</section>
</section>
<section id="a-very-quick-look-at-tcl-tk">
<h2>Tcl/Tk 速览<a class="headerlink" href="#a-very-quick-look-at-tcl-tk" title="永久链接至标题"></a></h2>
<p>类的层次结构看起来很复杂,但在实际操作中,应用程序编写人员几乎总是查看最底层的类。</p>
<p>注释:</p>
<ul class="simple">
<li><p>提供这些类,是为了能在同一个命名空间下将某些功能组织在一起。并不意味着可以独立对其进行实例化。</p></li>
<li><p><a class="reference internal" href="#tkinter.Tk" title="tkinter.Tk"><code class="xref py py-class docutils literal notranslate"><span class="pre">Tk</span></code></a> 类在同一个应用程序中仅需作一次实例化。应用程序编程人员不需要显式进行实例化,只要有其他任何类被实例化,系统就会创建一个。</p></li>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">Widget</span></code> 类不是用来实例化的,它仅用于继承以便生成“真正”的部件( C++ 中称为“抽象类”)。</p></li>
</ul>
<p>若要充分利用这些参考资料,有时需要知道如何阅读 Tk 的短文,以及如何识别 Tk 命令的各个部分。(参见 <a class="reference internal" href="#tkinter-basic-mapping"><span class="std std-ref">将简单的 Tk 映射到 Tkinter</span></a> 一节,了解以下内容在 <a class="reference internal" href="#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>
<p>Tk 脚本就是 Tcl 程序。像所有其他的 Tcl 程序一样Tk 脚本只是由空格分隔的单词列表。一个Tk 部件只是它的 <em>class</em><em>options</em> 用于进行配置,<em>actions</em> 让它执行有用的动作。</p>
<p>要在 Tk 中制作一个部件,总是采用如下格式的命令:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">classCommand</span> <span class="n">newPathname</span> <span class="n">options</span>
</pre></div>
</div>
<dl class="simple">
<dt><em>classCommand</em></dt><dd><p>表示要制作何种部件(按钮、标签、菜单......)。</p>
</dd>
</dl>
<dl class="simple" id="index-0">
<dt><em>newPathname</em></dt><dd><p>该组件的新名字。Tk 中的所有名字都必须唯一。为了帮助实现这一点Tk 中的部件都用 <em>路径</em> 命名,就像文件系统中的文件一样。顶层的部件,即 <em></em>,名为 <code class="docutils literal notranslate"><span class="pre">.``(句点),而子部件则由更多的句点分隔。比如部件名可能会是</span> <span class="pre">``.myApp.controlPanel.okButton</span></code></p>
</dd>
<dt><em>options</em></dt><dd><p>配置部件的外观,有时也能配置行为。这些选项以标志和值的列表形式出现。标志前带一个“-”,就像 Unix shell 命令的标志一样,如果值超过一个单词,就用引号括起来。</p>
</dd>
</dl>
<p>例如:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">button</span> <span class="o">.</span><span class="n">fred</span> <span class="o">-</span><span class="n">fg</span> <span class="n">red</span> <span class="o">-</span><span class="n">text</span> <span class="s2">&quot;hi there&quot;</span>
<span class="o">^</span> <span class="o">^</span> \<span class="n">______________________</span><span class="o">/</span>
<span class="o">|</span> <span class="o">|</span> <span class="o">|</span>
<span class="k">class</span> <span class="nc">new</span> <span class="n">options</span>
<span class="n">command</span> <span class="n">widget</span> <span class="p">(</span><span class="o">-</span><span class="n">opt</span> <span class="n">val</span> <span class="o">-</span><span class="n">opt</span> <span class="n">val</span> <span class="o">...</span><span class="p">)</span>
</pre></div>
</div>
<p>一旦创建成功,部件的路径名就成了一条新的命令。这个新的 <em>部件命令</em> 是程序员让新部件执行某些 <em>action</em> 的句柄。在 C 语言中可表示为someAction(fred, someOptions),在 C++ 中可表示为fred.someAction(someOptions),而在 Tk 中写作:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">.</span><span class="n">fred</span> <span class="n">someAction</span> <span class="n">someOptions</span>
</pre></div>
</div>
<p>请注意,对象名 <code class="docutils literal notranslate"><span class="pre">.fred</span></code> 是以句点开头的。</p>
<p>如您所料,<em>someAction</em> 的可用值取决于部件的类别。如果 fred 为按钮,则 <cite>.fred disable</cite> 有效fred 会变灰),而当 fred 为标签时则无效Tk 不支持标签的禁用)。</p>
<p><em>someOptions</em> 的合法值取决于动作。有些动作不需要参数,比如 <code class="docutils literal notranslate"><span class="pre">disable</span></code>,其他动作如文本输入框的 <code class="docutils literal notranslate"><span class="pre">delete</span></code> 命令则需用参数指定要删除的文本范围。</p>
</section>
<section id="mapping-basic-tk-into-tkinter">
<span id="tkinter-basic-mapping"></span><h2>将简单的 Tk 映射到 Tkinter<a class="headerlink" href="#mapping-basic-tk-into-tkinter" title="永久链接至标题"></a></h2>
<p>Tk 中的类命令对应于 Tkinter 中的类构造函数:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">button</span> <span class="o">.</span><span class="n">fred</span> <span class="o">=====&gt;</span> <span class="n">fred</span> <span class="o">=</span> <span class="n">Button</span><span class="p">()</span>
</pre></div>
</div>
<p>父对象是隐含在创建时给定的新名字中的。在 Tkinter 中,父对象是显式指定的:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">button</span> <span class="o">.</span><span class="n">panel</span><span class="o">.</span><span class="n">fred</span> <span class="o">=====&gt;</span> <span class="n">fred</span> <span class="o">=</span> <span class="n">Button</span><span class="p">(</span><span class="n">panel</span><span class="p">)</span>
</pre></div>
</div>
<p>Tk 中的配置项是以连字符标签列表的形式给出的,后跟着参数值。在 Tkinter 中,选项指定为实例构造函数中的关键字参数,在配置调用时指定为关键字 args或在已有实例中指定为实例索引以字典的形式。参见 <a class="reference internal" href="#tkinter-setting-options"><span class="std std-ref">可选配置项</span></a> 一节的选项设置部分。</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">button</span> <span class="o">.</span><span class="n">fred</span> <span class="o">-</span><span class="n">fg</span> <span class="n">red</span> <span class="o">=====&gt;</span> <span class="n">fred</span> <span class="o">=</span> <span class="n">Button</span><span class="p">(</span><span class="n">panel</span><span class="p">,</span> <span class="n">fg</span><span class="o">=</span><span class="s2">&quot;red&quot;</span><span class="p">)</span>
<span class="o">.</span><span class="n">fred</span> <span class="n">configure</span> <span class="o">-</span><span class="n">fg</span> <span class="n">red</span> <span class="o">=====&gt;</span> <span class="n">fred</span><span class="p">[</span><span class="s2">&quot;fg&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">red</span>
<span class="n">OR</span> <span class="o">==&gt;</span> <span class="n">fred</span><span class="o">.</span><span class="n">config</span><span class="p">(</span><span class="n">fg</span><span class="o">=</span><span class="s2">&quot;red&quot;</span><span class="p">)</span>
</pre></div>
</div>
<p>在 Tk 中, 要在某个部件上执行动作, 要用部件名作为命令, 并后面附上动作名称, 可能还会带有参数option。在 Tkinter 中,则调用类实例的方法来执行部件的动作。部件能够执行的动作(方法)列在 <code class="file docutils literal notranslate"><span class="pre">tkinter/__init__.py</span></code> 中。</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">.</span><span class="n">fred</span> <span class="n">invoke</span> <span class="o">=====&gt;</span> <span class="n">fred</span><span class="o">.</span><span class="n">invoke</span><span class="p">()</span>
</pre></div>
</div>
<p>若要将部件交给打包器geometry manager需带上可选参数去调用 pack。在 Tkinter 中Pack 类拥有全部这些功能pack 命令的各种形式都以方法的形式实现。 <a class="reference internal" href="#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> 中的所有部件都是从 Packer 继承而来的因此继承了所有打包方法。关于Form geometry manager 的更多信息,请参见 <a class="reference internal" href="tkinter.tix.html#module-tkinter.tix" title="tkinter.tix: Tk Extension Widgets for Tkinter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.tix</span></code></a> 模块的文档。</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">pack</span> <span class="o">.</span><span class="n">fred</span> <span class="o">-</span><span class="n">side</span> <span class="n">left</span> <span class="o">=====&gt;</span> <span class="n">fred</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="n">side</span><span class="o">=</span><span class="s2">&quot;left&quot;</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="how-tk-and-tkinter-are-related">
<h2>Tk 和 Tkinter 如何关联<a class="headerlink" href="#how-tk-and-tkinter-are-related" title="永久链接至标题"></a></h2>
<p>自上而下:</p>
<dl class="simple">
<dt>Python应用程序位于这里</dt><dd><p>Python 应用程序进行了 <a class="reference internal" href="#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>
</dd>
<dt>tkinter Python 包)</dt><dd><p>此调用(比如创建一个按钮部件)在 <a class="reference internal" href="#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> 包中实现,它是用 Python 编写的。该 Python 函数将解析命令和参数,并转换为貌似 Tk 脚本的格式,而不像是 Python 脚本。</p>
</dd>
<dt>_tkinterC 语言)</dt><dd><p>这些命令及参数将被传给 <code class="xref py py-mod docutils literal notranslate"><span class="pre">_tkinter</span></code> 扩展模块中的 C 函数——注意下划线。</p>
</dd>
<dt>Tk 部件C 和 Tcl</dt><dd><p>该 C 函数能调用其他的 C 模块,包括构成 Tk 库的 C 函数。Tk 是用 C 和一些 Tcl 实现的。Tk 部件的 Tcl 部分用于将某些默认行为绑定到部件上,并在导入 Python <a class="reference internal" href="#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>
</dd>
<dt>TkC 语言)</dt><dd><p>Tk 部件的 Tk 语言部分实现了最终的映射</p>
</dd>
<dt>XlibC 语言)</dt><dd><p>Xlib 库在屏幕上绘制图形。</p>
</dd>
</dl>
</section>
<section id="handy-reference">
<h2>快速参考<a class="headerlink" href="#handy-reference" title="永久链接至标题"></a></h2>
<section id="setting-options">
<span id="tkinter-setting-options"></span><h3>可选配置项<a class="headerlink" href="#setting-options" title="永久链接至标题"></a></h3>
<p>配置参数可以控制组件颜色和边框宽度等。可通过三种方式进行设置:</p>
<dl>
<dt>在对象创建时,使用关键字参数</dt><dd><div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">fred</span> <span class="o">=</span> <span class="n">Button</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">fg</span><span class="o">=</span><span class="s2">&quot;red&quot;</span><span class="p">,</span> <span class="n">bg</span><span class="o">=</span><span class="s2">&quot;blue&quot;</span><span class="p">)</span>
</pre></div>
</div>
</dd>
<dt>在对象创建后,将参数名用作字典索引</dt><dd><div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">fred</span><span class="p">[</span><span class="s2">&quot;fg&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;red&quot;</span>
<span class="n">fred</span><span class="p">[</span><span class="s2">&quot;bg&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;blue&quot;</span>
</pre></div>
</div>
</dd>
<dt>利用 config() 方法修改对象的多个属性</dt><dd><div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">fred</span><span class="o">.</span><span class="n">config</span><span class="p">(</span><span class="n">fg</span><span class="o">=</span><span class="s2">&quot;red&quot;</span><span class="p">,</span> <span class="n">bg</span><span class="o">=</span><span class="s2">&quot;blue&quot;</span><span class="p">)</span>
</pre></div>
</div>
</dd>
</dl>
<p>关于这些参数及其表现的完整解释,请参阅 Tk 手册中有关组件的 man 帮助页。</p>
<p>请注意man 手册页列出了每个部件的“标准选项”和“组件特有选项”。前者是很多组件通用的选项列表,后者是该组件特有的选项。标准选项在 <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/options(3)">options(3)</a></em> man 手册中有文档。</p>
<p>本文没有区分标准选项和部件特有选项。有些选项不适用于某类组件。组件是否对某选项做出响应,取决于组件的类别;按钮组件有一个 <code class="docutils literal notranslate"><span class="pre">command</span></code> 选项,而标签组件就没有。</p>
<p>组件支持的选项在其手册中有列出,也可在运行时调用 <code class="xref py py-meth docutils literal notranslate"><span class="pre">config()</span></code> 方法(不带参数)查看,或者通过调用组件的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">keys()</span></code> 方法进行查询。这些调用的返回值为字典,字典的键是字符串格式的选项名(比如 <code class="docutils literal notranslate"><span class="pre">'relief'</span></code>),字典的值为五元组。</p>
<p>有些选项,比如 <code class="docutils literal notranslate"><span class="pre">bg</span></code> 是全名通用选项的同义词(<code class="docutils literal notranslate"><span class="pre">bg</span></code> 是 “background”的简写。向 <code class="docutils literal notranslate"><span class="pre">config()</span></code> 方法传入选项的简称将返回一个二元组,而不是五元组。传回的二元组将包含同义词的全名和“真正的”选项(比如 <code class="docutils literal notranslate"><span class="pre">('bg',</span> <span class="pre">'background')</span></code>)。</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 13%" />
<col style="width: 61%" />
<col style="width: 26%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>索引</p></th>
<th class="head"><p>含意</p></th>
<th class="head"><p>示例</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>0</p></td>
<td><p>选项名称</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'relief'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p>1</p></td>
<td><p>数据库查找的选项名称</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'relief'</span></code></p></td>
</tr>
<tr class="row-even"><td><p>2</p></td>
<td><p>数据库查找的选项类</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'Relief'</span></code></p></td>
</tr>
<tr class="row-odd"><td><p>3</p></td>
<td><p>默认值</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'raised'</span></code></p></td>
</tr>
<tr class="row-even"><td><p>4</p></td>
<td><p>当前值</p></td>
<td><p><code class="docutils literal notranslate"><span class="pre">'groove'</span></code></p></td>
</tr>
</tbody>
</table>
<p>示例:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">fred</span><span class="o">.</span><span class="n">config</span><span class="p">())</span>
<span class="go">{&#39;relief&#39;: (&#39;relief&#39;, &#39;relief&#39;, &#39;Relief&#39;, &#39;raised&#39;, &#39;groove&#39;)}</span>
</pre></div>
</div>
<p>当然,输出的字典将包含所有可用选项及其值。这里只是举个例子。</p>
</section>
<section id="the-packer">
<h3>包装器<a class="headerlink" href="#the-packer" title="永久链接至标题"></a></h3>
<p id="index-1">包装器是 Tk 的形状管理机制之一。 形状geometry )管理器用于指定多个部件在容器(共同的 <em></em> 组件)内的相对位置。与更为麻烦的 <em>定位器</em> 相比(不太常用,这里不做介绍),包装器可接受定性的相对关系—— <em>上面</em><em>左边</em><em>填充</em> 等,并确定精确的位置坐标。</p>
<p><em></em> 部件的大小都由其内部的 “从属部件” 的大小决定。包装器用于控制从属部件在主部件中出现的位置。可以把部件包入框架,再把框架包入其他框架中,搭建出所需的布局。此外,只要完成了包装,组件的布局就会进行动态调整,以适应布局参数的变化。</p>
<p>请注意,只有用形状管理器指定几何形状后,部件才会显示出来。忘记设置形状参数是新手常犯的错误,惊讶于创建完部件却啥都没出现。部件只有在应用了类似于打包器的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">pack()</span></code> 方法之后才会显示在屏幕上。</p>
<p>调用 pack() 方法时可以给出由关键字/参数值组成的键值对,以便控制组件在其容器中出现的位置,以及主程序窗口大小变动时的行为。下面是一些例子:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">fred</span><span class="o">.</span><span class="n">pack</span><span class="p">()</span> <span class="c1"># defaults to side = &quot;top&quot;</span>
<span class="n">fred</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="n">side</span><span class="o">=</span><span class="s2">&quot;left&quot;</span><span class="p">)</span>
<span class="n">fred</span><span class="o">.</span><span class="n">pack</span><span class="p">(</span><span class="n">expand</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="packer-options">
<h3>包装器的参数<a class="headerlink" href="#packer-options" title="永久链接至标题"></a></h3>
<p>关于包装器及其可接受的参数,更多信息请参阅 man 手册和 John Ousterhout 书中的第 183 页。</p>
<dl class="simple">
<dt>anchor</dt><dd><p>anchor 类型。 表示包装器要放置的每个从属组件的位置。</p>
</dd>
<dt>expand</dt><dd><p>布尔型,<code class="docutils literal notranslate"><span class="pre">0</span></code><code class="docutils literal notranslate"><span class="pre">1</span></code></p>
</dd>
<dt>fill</dt><dd><p>合法值为:<code class="docutils literal notranslate"><span class="pre">'x'</span></code><code class="docutils literal notranslate"><span class="pre">'y'</span></code><code class="docutils literal notranslate"><span class="pre">'both'</span></code><code class="docutils literal notranslate"><span class="pre">'none'</span></code></p>
</dd>
<dt>ipadx 和 ipady</dt><dd><p>距离值,指定从属部件的内边距。</p>
</dd>
<dt>padx 和 pady</dt><dd><p>距离值,指定从属部件的外边距。</p>
</dd>
<dt>side</dt><dd><p>合法值为:<code class="docutils literal notranslate"><span class="pre">'left'</span></code><code class="docutils literal notranslate"><span class="pre">'right'</span></code><code class="docutils literal notranslate"><span class="pre">'top'</span></code><code class="docutils literal notranslate"><span class="pre">'bottom'</span></code></p>
</dd>
</dl>
</section>
<section id="coupling-widget-variables">
<h3>部件与变量的关联<a class="headerlink" href="#coupling-widget-variables" title="永久链接至标题"></a></h3>
<p>通过一些特定参数,某些组件(如文本输入组件)的当前设置可直接与应用程序的变量关联。这些参数包括 <code class="docutils literal notranslate"><span class="pre">variable</span></code><code class="docutils literal notranslate"><span class="pre">textvariable</span></code><code class="docutils literal notranslate"><span class="pre">onvalue</span></code><code class="docutils literal notranslate"><span class="pre">offvalue</span></code><code class="docutils literal notranslate"><span class="pre">value</span></code>。这种关联是双向的:只要这些变量因任何原因发生变化,其关联的部件就会更新以反映新的参数值。</p>
<p>不幸的是,在目前 <a class="reference internal" href="#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> 的实现代码中,不可能通过 <code class="docutils literal notranslate"><span class="pre">variable</span></code><code class="docutils literal notranslate"><span class="pre">textvariable</span></code> 参数将任意 Python 变量移交给组件。变量只有是 <a class="reference internal" href="#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> 中定义的 Variable 类的子类,才能生效。</p>
<p>已经定义了很多有用的 Variable 子类: <code class="xref py py-class docutils literal notranslate"><span class="pre">StringVar</span></code><code class="xref py py-class docutils literal notranslate"><span class="pre">IntVar</span></code><code class="xref py py-class docutils literal notranslate"><span class="pre">DoubleVar</span></code><code class="xref py py-class docutils literal notranslate"><span class="pre">BooleanVar</span></code>。调用 <code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code> 方法可以读取这些变量的当前值;调用 <code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code> 方法则可改变变量值。只要遵循这种用法,组件就会保持跟踪变量的值,而不需要更多的干预。</p>
<p>例如:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tkinter</span> <span class="k">as</span> <span class="nn">tk</span>
<span class="k">class</span> <span class="nc">App</span><span class="p">(</span><span class="n">tk</span><span class="o">.</span><span class="n">Frame</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">master</span><span class="p">):</span>
<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">master</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">pack</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">entrythingy</span> <span class="o">=</span> <span class="n">tk</span><span class="o">.</span><span class="n">Entry</span><span class="p">()</span>
<span class="bp">self</span><span class="o">.</span><span class="n">entrythingy</span><span class="o">.</span><span class="n">pack</span><span class="p">()</span>
<span class="c1"># Create the application variable.</span>
<span class="bp">self</span><span class="o">.</span><span class="n">contents</span> <span class="o">=</span> <span class="n">tk</span><span class="o">.</span><span class="n">StringVar</span><span class="p">()</span>
<span class="c1"># Set it to some value.</span>
<span class="bp">self</span><span class="o">.</span><span class="n">contents</span><span class="o">.</span><span class="n">set</span><span class="p">(</span><span class="s2">&quot;this is a variable&quot;</span><span class="p">)</span>
<span class="c1"># Tell the entry widget to watch this variable.</span>
<span class="bp">self</span><span class="o">.</span><span class="n">entrythingy</span><span class="p">[</span><span class="s2">&quot;textvariable&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">contents</span>
<span class="c1"># Define a callback for when the user hits return.</span>
<span class="c1"># It prints the current value of the variable.</span>
<span class="bp">self</span><span class="o">.</span><span class="n">entrythingy</span><span class="o">.</span><span class="n">bind</span><span class="p">(</span><span class="s1">&#39;&lt;Key-Return&gt;&#39;</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">print_contents</span><span class="p">)</span>
<span class="k">def</span> <span class="nf">print_contents</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">event</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;Hi. The current entry content is:&quot;</span><span class="p">,</span>
<span class="bp">self</span><span class="o">.</span><span class="n">contents</span><span class="o">.</span><span class="n">get</span><span class="p">())</span>
<span class="n">root</span> <span class="o">=</span> <span class="n">tk</span><span class="o">.</span><span class="n">Tk</span><span class="p">()</span>
<span class="n">myapp</span> <span class="o">=</span> <span class="n">App</span><span class="p">(</span><span class="n">root</span><span class="p">)</span>
<span class="n">myapp</span><span class="o">.</span><span class="n">mainloop</span><span class="p">()</span>
</pre></div>
</div>
</section>
<section id="the-window-manager">
<h3>窗口管理器<a class="headerlink" href="#the-window-manager" title="永久链接至标题"></a></h3>
<p id="index-2">Tk 有个实用命令 <code class="docutils literal notranslate"><span class="pre">wm</span></code>,用于与窗口管理器进行交互。<code class="docutils literal notranslate"><span class="pre">wm</span></code> 命令的参数可用于控制标题、位置、图标之类的东西。在 <a class="reference internal" href="#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> 中,这些命令已被实现为 <code class="xref py py-class docutils literal notranslate"><span class="pre">Wm</span></code> 类的方法。顶层部件是 <code class="xref py py-class docutils literal notranslate"><span class="pre">Wm</span></code> 类的子类,所以可以直接调用 <code class="xref py py-class docutils literal notranslate"><span class="pre">Wm</span></code> 的这些方法。</p>
<p>要获得指定部件所在的顶层窗口,通常只要引用该部件的主窗口即可。当然,如果该部件是包装在框架内的,那么主窗口不代表就是顶层窗口。为了获得任意组件所在的顶层窗口,可以调用 <code class="xref py py-meth docutils literal notranslate"><span class="pre">_root()</span></code> 方法。该方法以下划线开头,表明其为 Python 实现的代码,而非 Tk 提供的某个接口。</p>
<p>以下是一些典型用法:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tkinter</span> <span class="k">as</span> <span class="nn">tk</span>
<span class="k">class</span> <span class="nc">App</span><span class="p">(</span><span class="n">tk</span><span class="o">.</span><span class="n">Frame</span><span class="p">):</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">master</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
<span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">master</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">pack</span><span class="p">()</span>
<span class="c1"># create the application</span>
<span class="n">myapp</span> <span class="o">=</span> <span class="n">App</span><span class="p">()</span>
<span class="c1">#</span>
<span class="c1"># here are method calls to the window manager class</span>
<span class="c1">#</span>
<span class="n">myapp</span><span class="o">.</span><span class="n">master</span><span class="o">.</span><span class="n">title</span><span class="p">(</span><span class="s2">&quot;My Do-Nothing Application&quot;</span><span class="p">)</span>
<span class="n">myapp</span><span class="o">.</span><span class="n">master</span><span class="o">.</span><span class="n">maxsize</span><span class="p">(</span><span class="mi">1000</span><span class="p">,</span> <span class="mi">400</span><span class="p">)</span>
<span class="c1"># start the program</span>
<span class="n">myapp</span><span class="o">.</span><span class="n">mainloop</span><span class="p">()</span>
</pre></div>
</div>
</section>
<section id="tk-option-data-types">
<h3>Tk 参数的数据类型<a class="headerlink" href="#tk-option-data-types" title="永久链接至标题"></a></h3>
<dl id="index-3">
<dt>anchor</dt><dd><p>合法值是罗盘的方位点:<code class="docutils literal notranslate"><span class="pre">&quot;n&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;ne&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;e&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;se&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;s&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;sw&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;w&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;nw&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;center&quot;</span></code></p>
</dd>
<dt>bitmap</dt><dd><p>内置已命名的位图有八个:<code class="docutils literal notranslate"><span class="pre">'error'</span></code><code class="docutils literal notranslate"><span class="pre">'gray25'</span></code><code class="docutils literal notranslate"><span class="pre">'gray50'</span></code><code class="docutils literal notranslate"><span class="pre">'hourglass'</span></code><code class="docutils literal notranslate"><span class="pre">'info'</span></code><code class="docutils literal notranslate"><span class="pre">'questhead'</span></code><code class="docutils literal notranslate"><span class="pre">'question'</span></code><code class="docutils literal notranslate"><span class="pre">'warning'</span></code> 。若要指定位图的文件名,请给出完整路径,前面加一个 <code class="docutils literal notranslate"><span class="pre">&#64;</span></code>,比如 <code class="docutils literal notranslate"><span class="pre">&quot;&#64;/usr/contrib/bitmap/gumby.bit&quot;</span></code></p>
</dd>
<dt>boolean</dt><dd><p>可以传入整数 0 或 1或是字符串 <code class="docutils literal notranslate"><span class="pre">&quot;yes&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;no&quot;</span></code></p>
</dd>
<dt>callback -- 回调</dt><dd><p>指任何无需调用参数的 Python 函数。 例如:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">print_it</span><span class="p">():</span>
<span class="nb">print</span><span class="p">(</span><span class="s2">&quot;hi there&quot;</span><span class="p">)</span>
<span class="n">fred</span><span class="p">[</span><span class="s2">&quot;command&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="n">print_it</span>
</pre></div>
</div>
</dd>
<dt>color</dt><dd><p>可在 rgb.txt 文件中以颜色名的形式给出,或是 RGB 字符串的形式4 位 <code class="docutils literal notranslate"><span class="pre">&quot;#RGB&quot;</span></code> 8 位 <code class="docutils literal notranslate"><span class="pre">&quot;#RRGGBB&quot;</span></code>12 位:<code class="docutils literal notranslate"><span class="pre">&quot;#RRRGGGBBB&quot;</span></code>16 位:<code class="docutils literal notranslate"><span class="pre">&quot;#RRRRGGGGBBBB&quot;</span></code>其中R、G、B 为合法的十六进制数值。 详见 Ousterhout 书中的第 160 页。</p>
</dd>
<dt>cursor</dt><dd><p>可采用 <code class="file docutils literal notranslate"><span class="pre">cursorfont.h</span></code> 中的标准光标名称,去掉 <code class="docutils literal notranslate"><span class="pre">XC_</span></code> 前缀。 比如要获取一个手形光标(<code class="xref py py-const docutils literal notranslate"><span class="pre">XC_hand2</span></code>),可以用字符串 <code class="docutils literal notranslate"><span class="pre">&quot;hand2&quot;</span></code>。也可以指定自己的位图和掩码文件作为光标。参见 Ousterhout 书中的第 179 页。</p>
</dd>
<dt>distance</dt><dd><p>屏幕距离可以用像素或绝对距离来指定。像素是数字,绝对距离是字符串,后面的字符表示单位:<code class="docutils literal notranslate"><span class="pre">c</span></code> 是厘米,<code class="docutils literal notranslate"><span class="pre">i</span></code> 是英寸,<code class="docutils literal notranslate"><span class="pre">m</span></code> 是毫米,<code class="docutils literal notranslate"><span class="pre">p</span></code> 则表示打印机的点数。例如3.5 英寸可表示为 <code class="docutils literal notranslate"><span class="pre">&quot;3.5i&quot;</span></code></p>
</dd>
<dt>font</dt><dd><p>Tk 采用一串名称的格式表示字体,例如 <code class="docutils literal notranslate"><span class="pre">{courier</span> <span class="pre">10</span> <span class="pre">bold}</span></code>。正数的字体大小以点为单位,负数的大小以像素为单位。</p>
</dd>
<dt>geometry</dt><dd><p>这是一个 <code class="docutils literal notranslate"><span class="pre">widthxheight</span></code> 形式的字符串其中宽度和高度对于大多数部件来说是以像素为单位的对于显示文本的部件来说是以字符为单位的。例如fred[&quot;geometry&quot;] = &quot;200x100&quot;</p>
</dd>
<dt>justify</dt><dd><p>合法的值为字符串: <code class="docutils literal notranslate"><span class="pre">&quot;left&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;center&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;right&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;fill&quot;</span></code></p>
</dd>
<dt>区域</dt><dd><p>这是包含四个元素的字符串,以空格分隔,每个元素是表示一个合法的距离值(见上文)。例如:<code class="docutils literal notranslate"><span class="pre">&quot;2</span> <span class="pre">3</span> <span class="pre">4</span> <span class="pre">5&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;3i</span> <span class="pre">2i</span> <span class="pre">4.5i</span> <span class="pre">2i&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;3c</span> <span class="pre">2c</span> <span class="pre">4c</span> <span class="pre">10.43c&quot;</span></code> 都是合法的区域值。</p>
</dd>
<dt>relief</dt><dd><p>决定了组件的边框样式。 合法值包括:<code class="docutils literal notranslate"><span class="pre">&quot;raised&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;sunken&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;flat&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;groove&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;ridge&quot;</span></code></p>
</dd>
<dt>scrollcommand</dt><dd><p>这几乎就是带滚动条部件的 <code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code> 方法,但也可是任一只有一个参数的部件方法。</p>
</dd>
<dt>wrap:</dt><dd><p>只能是以下值之一:<code class="docutils literal notranslate"><span class="pre">&quot;none&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;char&quot;</span></code><code class="docutils literal notranslate"><span class="pre">&quot;word&quot;</span></code></p>
</dd>
</dl>
</section>
<section id="bindings-and-events">
<h3>绑定和事件<a class="headerlink" href="#bindings-and-events" title="永久链接至标题"></a></h3>
<p id="index-4">部件命令中的 bind 方法可觉察某些事件并在事件发生时触发一个回调函数。bind 方法的形式是:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">bind</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">sequence</span><span class="p">,</span> <span class="n">func</span><span class="p">,</span> <span class="n">add</span><span class="o">=</span><span class="s1">&#39;&#39;</span><span class="p">):</span>
</pre></div>
</div>
<p>其中:</p>
<dl class="simple">
<dt>sequence</dt><dd><p>是一个表示事件的目标种类的字符串。(详情请看 bind 的手册页和 John Outsterhout 的书的第 201 页。)</p>
</dd>
<dt>func</dt><dd><p>是带有一个参数的 Python 函数,发生事件时将会调用。传入的参数为一个 Event 实例。(以这种方式部署的函数通常称为 <em>回调函数</em>。)</p>
</dd>
<dt>add</dt><dd><p>可选项, <code class="docutils literal notranslate"><span class="pre">''</span></code><code class="docutils literal notranslate"><span class="pre">'+'</span></code> 。传入空字符串表示本次绑定将替换与此事件关联的其他所有绑定。传递 <code class="docutils literal notranslate"><span class="pre">'+'</span></code> 则意味着加入此事件类型已绑定函数的列表中。</p>
</dd>
</dl>
<p>例如:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">turn_red</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">event</span><span class="p">):</span>
<span class="n">event</span><span class="o">.</span><span class="n">widget</span><span class="p">[</span><span class="s2">&quot;activeforeground&quot;</span><span class="p">]</span> <span class="o">=</span> <span class="s2">&quot;red&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">button</span><span class="o">.</span><span class="n">bind</span><span class="p">(</span><span class="s2">&quot;&lt;Enter&gt;&quot;</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">turn_red</span><span class="p">)</span>
</pre></div>
</div>
<p>请注意,在 <code class="docutils literal notranslate"><span class="pre">turn_red()</span></code> 回调函数中如何访问事件的 widget 字段。该字段包含了捕获 X 事件的控件。下表列出了事件可供访问的其他字段,及其在 Tk 中的表示方式,这在查看 Tk 手册时很有用处。</p>
<table class="docutils align-default">
<colgroup>
<col style="width: 8%" />
<col style="width: 42%" />
<col style="width: 8%" />
<col style="width: 42%" />
</colgroup>
<thead>
<tr class="row-odd"><th class="head"><p>Tk</p></th>
<th class="head"><p>Tkinter 事件字段</p></th>
<th class="head"><p>Tk</p></th>
<th class="head"><p>Tkinter 事件字段</p></th>
</tr>
</thead>
<tbody>
<tr class="row-even"><td><p>%f</p></td>
<td><p>focus</p></td>
<td><p>%A</p></td>
<td><p>char</p></td>
</tr>
<tr class="row-odd"><td><p>%h</p></td>
<td><p>height</p></td>
<td><p>%E</p></td>
<td><p>send_event</p></td>
</tr>
<tr class="row-even"><td><p>%k</p></td>
<td><p>keycode</p></td>
<td><p>%K</p></td>
<td><p>keysym</p></td>
</tr>
<tr class="row-odd"><td><p>%s</p></td>
<td><p>state</p></td>
<td><p>%N</p></td>
<td><p>keysym_num</p></td>
</tr>
<tr class="row-even"><td><p>%t</p></td>
<td><p>time</p></td>
<td><p>%T</p></td>
<td><p>type</p></td>
</tr>
<tr class="row-odd"><td><p>%w</p></td>
<td><p>width</p></td>
<td><p>%W</p></td>
<td><p>widget</p></td>
</tr>
<tr class="row-even"><td><p>%x</p></td>
<td><p>x</p></td>
<td><p>%X</p></td>
<td><p>x_root</p></td>
</tr>
<tr class="row-odd"><td><p>%y</p></td>
<td><p>y</p></td>
<td><p>%Y</p></td>
<td><p>y_root</p></td>
</tr>
</tbody>
</table>
</section>
<section id="the-index-parameter">
<h3>index 参数<a class="headerlink" href="#the-index-parameter" title="永久链接至标题"></a></h3>
<p>很多控件都需要传入 index 参数。该参数用于指明 Text 控件中的位置,或指明 Entry 控件中的字符,或指明 Menu 控件中的菜单项。</p>
<dl class="simple">
<dt>Entry 控件的索引index、view index 等)</dt><dd><p>Entry 控件带有索引属性,指向显示文本中的字符位置。这些 <a class="reference internal" href="#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>
</dd>
<dt>Text 控件的索引</dt><dd><p>Text 控件的索引语法非常复杂,最好还是在 Tk 手册中查看。</p>
</dd>
<dt>Menu 索引menu.invoke()、menu.entryconfig() 等)</dt><dd><p>菜单的某些属性和方法可以操纵特定的菜单项。只要属性或参数需要用到菜单索引,就可用以下方式传入:</p>
<ul class="simple">
<li><p>一个整数,指的是菜单项的数字位置,从顶部开始计数,从 0 开始;</p></li>
<li><p>字符串 <code class="docutils literal notranslate"><span class="pre">&quot;active&quot;</span></code>,指的是当前光标所在的菜单;</p></li>
<li><p>字符串 <code class="docutils literal notranslate"><span class="pre">&quot;last&quot;</span></code>,指的是上一个菜单项;</p></li>
<li><p>带有 <code class="docutils literal notranslate"><span class="pre">&#64;</span></code> 前缀的整数,比如 <code class="docutils literal notranslate"><span class="pre">&#64;6</span></code>,这里的整数解释为菜单坐标系中的 y 像素坐标;</p></li>
<li><p>表示没有任何菜单条目的字符串 <code class="docutils literal notranslate"><span class="pre">&quot;none&quot;</span></code> 经常与 menu.activate() 一同被用来停用所有条目,以及 ——</p></li>
<li><p>与菜单项的文本标签进行模式匹配的文本串,从菜单顶部扫描到底部。请注意,此索引类型是在其他所有索引类型之后才会考虑的,这意味着文本标签为 <code class="docutils literal notranslate"><span class="pre">last</span></code><code class="docutils literal notranslate"><span class="pre">active</span></code><code class="docutils literal notranslate"><span class="pre">none</span></code> 的菜单项匹配成功后,可能会视为这些单词文字本身。</p></li>
</ul>
</dd>
</dl>
</section>
<section id="images">
<h3>图片<a class="headerlink" href="#images" title="永久链接至标题"></a></h3>
<p>通过 <code class="xref py py-class docutils literal notranslate"><span class="pre">tkinter.Image</span></code> 的各种子类可以创建相应格式的图片:</p>
<ul class="simple">
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">BitmapImage</span></code> 对应 XBM 格式的图片。</p></li>
<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">PhotoImage</span></code> 对应 PGM、PPM、GIF 和 PNG 格式的图片。后者自 Tk 8.6 开始支持。</p></li>
</ul>
<p>这两种图片可通过 <code class="docutils literal notranslate"><span class="pre">file</span></code><code class="docutils literal notranslate"><span class="pre">data</span></code> 属性创建的(也可能由其他属性创建)。</p>
<p>然后可在某些支持 <code class="docutils literal notranslate"><span class="pre">image</span></code> 属性的控件中如标签、按钮、菜单使用图片对象。这时Tk 不会保留对图片对象的引用。当图片对象的最后一个 Python 引用被删除时,图片数据也会删除,并且 Tk 会在用到图片对象的地方显示一个空白框。</p>
<div class="admonition seealso">
<p class="admonition-title">参见</p>
<p><a class="reference external" href="http://python-pillow.org/">Pillow</a> 包增加了对 BMP、JPEG、TIFF 和 WebP 等格式的支持。</p>
</div>
</section>
</section>
<section id="file-handlers">
<span id="tkinter-file-handlers"></span><h2>文件处理程序<a class="headerlink" href="#file-handlers" title="永久链接至标题"></a></h2>
<p>Tk 允许为文件操作注册和注销一个回调函数,当对文件描述符进行 I/O 时Tk 的主循环会调用该回调函数。每个文件描述符只能注册一个处理程序。示例代码如下:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tkinter</span>
<span class="n">widget</span> <span class="o">=</span> <span class="n">tkinter</span><span class="o">.</span><span class="n">Tk</span><span class="p">()</span>
<span class="n">mask</span> <span class="o">=</span> <span class="n">tkinter</span><span class="o">.</span><span class="n">READABLE</span> <span class="o">|</span> <span class="n">tkinter</span><span class="o">.</span><span class="n">WRITABLE</span>
<span class="n">widget</span><span class="o">.</span><span class="n">tk</span><span class="o">.</span><span class="n">createfilehandler</span><span class="p">(</span><span class="n">file</span><span class="p">,</span> <span class="n">mask</span><span class="p">,</span> <span class="n">callback</span><span class="p">)</span>
<span class="o">...</span>
<span class="n">widget</span><span class="o">.</span><span class="n">tk</span><span class="o">.</span><span class="n">deletefilehandler</span><span class="p">(</span><span class="n">file</span><span class="p">)</span>
</pre></div>
</div>
<p>在 Windows 系统中不可用。</p>
<p>由于不知道可读取多少字节,你可能不希望使用 <a class="reference internal" href="io.html#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">BufferedIOBase</span></code></a><a class="reference internal" href="io.html#io.TextIOBase" title="io.TextIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">TextIOBase</span></code></a><a class="reference internal" href="io.html#io.BufferedIOBase.read" title="io.BufferedIOBase.read"><code class="xref py py-meth docutils literal notranslate"><span class="pre">read()</span></code></a><a class="reference internal" href="io.html#io.IOBase.readline" title="io.IOBase.readline"><code class="xref py py-meth docutils literal notranslate"><span class="pre">readline()</span></code></a> 方法,因为这些方法必须读取预定数量的字节。 对于套接字,可使用 <a class="reference internal" href="socket.html#socket.socket.recv" title="socket.socket.recv"><code class="xref py py-meth docutils literal notranslate"><span class="pre">recv()</span></code></a><a class="reference internal" href="socket.html#socket.socket.recvfrom" title="socket.socket.recvfrom"><code class="xref py py-meth docutils literal notranslate"><span class="pre">recvfrom()</span></code></a> 方法;对于其他文件,可使用原始读取方法或 <code class="docutils literal notranslate"><span class="pre">os.read(file.fileno(),</span> <span class="pre">maxbytecount)</span></code></p>
<dl class="method">
<dt id="tkinter.Widget.tk.createfilehandler">
<code class="sig-prename descclassname">Widget.tk.</code><code class="sig-name descname">createfilehandler</code><span class="sig-paren">(</span><em class="sig-param">file</em>, <em class="sig-param">mask</em>, <em class="sig-param">func</em><span class="sig-paren">)</span><a class="headerlink" href="#tkinter.Widget.tk.createfilehandler" title="永久链接至目标"></a></dt>
<dd><p>注册文件处理程序的回调函数 <em>func</em><em>file</em> 参数可以是具备 <a class="reference internal" href="io.html#io.IOBase.fileno" title="io.IOBase.fileno"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fileno()</span></code></a> 方法的对象(例如文件或套接字对象),也可以是整数文件描述符。 <em>mask</em> 参数是下述三个常量的逻辑“或”组合。回调函数将用以下格式调用:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">callback</span><span class="p">(</span><span class="n">file</span><span class="p">,</span> <span class="n">mask</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>
<dl class="method">
<dt id="tkinter.Widget.tk.deletefilehandler">
<code class="sig-prename descclassname">Widget.tk.</code><code class="sig-name descname">deletefilehandler</code><span class="sig-paren">(</span><em class="sig-param">file</em><span class="sig-paren">)</span><a class="headerlink" href="#tkinter.Widget.tk.deletefilehandler" title="永久链接至目标"></a></dt>
<dd><p>注销文件处理函数。</p>
</dd></dl>
<dl class="data">
<dt id="tkinter.READABLE">
<code class="sig-prename descclassname">tkinter.</code><code class="sig-name descname">READABLE</code><a class="headerlink" href="#tkinter.READABLE" title="永久链接至目标"></a></dt>
<dt id="tkinter.WRITABLE">
<code class="sig-prename descclassname">tkinter.</code><code class="sig-name descname">WRITABLE</code><a class="headerlink" href="#tkinter.WRITABLE" title="永久链接至目标"></a></dt>
<dt id="tkinter.EXCEPTION">
<code class="sig-prename descclassname">tkinter.</code><code class="sig-name descname">EXCEPTION</code><a class="headerlink" href="#tkinter.EXCEPTION" title="永久链接至目标"></a></dt>
<dd><p>Constants used in the <em>mask</em> arguments.</p>
</dd></dl>
</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="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> —— Tcl/Tk 的 Python 接口</a><ul>
<li><a class="reference internal" href="#tkinter-modules">Tkinter 模块</a></li>
<li><a class="reference internal" href="#tkinter-life-preserver">Tkinter 拾遗</a><ul>
<li><a class="reference internal" href="#how-to-use-this-section">本节内容的用法</a></li>
<li><a class="reference internal" href="#a-simple-hello-world-program">简单的 Hello World 程序</a></li>
</ul>
</li>
<li><a class="reference internal" href="#a-very-quick-look-at-tcl-tk">Tcl/Tk 速览</a></li>
<li><a class="reference internal" href="#mapping-basic-tk-into-tkinter">将简单的 Tk 映射到 Tkinter</a></li>
<li><a class="reference internal" href="#how-tk-and-tkinter-are-related">Tk 和 Tkinter 如何关联</a></li>
<li><a class="reference internal" href="#handy-reference">快速参考</a><ul>
<li><a class="reference internal" href="#setting-options">可选配置项</a></li>
<li><a class="reference internal" href="#the-packer">包装器</a></li>
<li><a class="reference internal" href="#packer-options">包装器的参数</a></li>
<li><a class="reference internal" href="#coupling-widget-variables">部件与变量的关联</a></li>
<li><a class="reference internal" href="#the-window-manager">窗口管理器</a></li>
<li><a class="reference internal" href="#tk-option-data-types">Tk 参数的数据类型</a></li>
<li><a class="reference internal" href="#bindings-and-events">绑定和事件</a></li>
<li><a class="reference internal" href="#the-index-parameter">index 参数</a></li>
<li><a class="reference internal" href="#images">图片</a></li>
</ul>
</li>
<li><a class="reference internal" href="#file-handlers">文件处理程序</a></li>
</ul>
</li>
</ul>
<h4>上一个主题</h4>
<p class="topless"><a href="tk.html"
title="上一章">Tk图形用户界面(GUI)</a></p>
<h4>下一个主题</h4>
<p class="topless"><a href="tkinter.ttk.html"
title="下一章"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.ttk</span></code> --- Tk 风格的控件</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/tkinter.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="tkinter.ttk.html" title="tkinter.ttk --- Tk 风格的控件"
>下一页</a> |</li>
<li class="right" >
<a href="tk.html" title="Tk图形用户界面(GUI)"
>上一页</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>