gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1337 lines
109KB

  1. <!DOCTYPE html>
  2. <html lang="en" data-content_root="../">
  3. <head>
  4. <meta charset="utf-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
  6. <meta property="og:title" content="tkinter — Python interface to Tcl/Tk" />
  7. <meta property="og:type" content="website" />
  8. <meta property="og:url" content="https://docs.python.org/3/library/tkinter.html" />
  9. <meta property="og:site_name" content="Python documentation" />
  10. <meta property="og:description" content="Source code: Lib/tkinter/__init__.py The tkinter package (“Tk interface”) is the standard Python interface to the Tcl/Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, inclu..." />
  11. <meta property="og:image" content="https://docs.python.org/3/_static/og-image.png" />
  12. <meta property="og:image:alt" content="Python documentation" />
  13. <meta name="description" content="Source code: Lib/tkinter/__init__.py The tkinter package (“Tk interface”) is the standard Python interface to the Tcl/Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, inclu..." />
  14. <meta property="og:image:width" content="200" />
  15. <meta property="og:image:height" content="200" />
  16. <meta name="theme-color" content="#3776ab" />
  17. <title>tkinter — Python interface to Tcl/Tk &#8212; Python 3.12.3 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">
  18. <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=80d5e7a1" />
  19. <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=bb723527" />
  20. <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=b20cc3f5" />
  21. <script src="../_static/documentation_options.js?v=2c828074"></script>
  22. <script src="../_static/doctools.js?v=888ff710"></script>
  23. <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
  24. <script src="../_static/sidebar.js"></script>
  25. <link rel="search" type="application/opensearchdescription+xml"
  26. title="Search within Python 3.12.3 documentation"
  27. href="../_static/opensearch.xml"/>
  28. <link rel="author" title="About these documents" href="../about.html" />
  29. <link rel="index" title="Index" href="../genindex.html" />
  30. <link rel="search" title="Search" href="../search.html" />
  31. <link rel="copyright" title="Copyright" href="../copyright.html" />
  32. <link rel="next" title="tkinter.colorchooser — Color choosing dialog" href="tkinter.colorchooser.html" />
  33. <link rel="prev" title="Graphical User Interfaces with Tk" href="tk.html" />
  34. <link rel="canonical" href="https://docs.python.org/3/library/tkinter.html" />
  35. <style>
  36. @media only screen {
  37. table.full-width-table {
  38. width: 100%;
  39. }
  40. }
  41. </style>
  42. <link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">
  43. <link rel="shortcut icon" type="image/png" href="../_static/py.svg" />
  44. <script type="text/javascript" src="../_static/copybutton.js"></script>
  45. <script type="text/javascript" src="../_static/menu.js"></script>
  46. <script type="text/javascript" src="../_static/search-focus.js"></script>
  47. <script type="text/javascript" src="../_static/themetoggle.js"></script>
  48. </head>
  49. <body>
  50. <div class="mobile-nav">
  51. <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"
  52. aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu" />
  53. <nav class="nav-content" role="navigation">
  54. <label for="menuToggler" class="toggler__label">
  55. <span></span>
  56. </label>
  57. <span class="nav-items-wrapper">
  58. <a href="https://www.python.org/" class="nav-logo">
  59. <img src="../_static/py.svg" alt="Python logo"/>
  60. </a>
  61. <span class="version_switcher_placeholder"></span>
  62. <form role="search" class="search" action="../search.html" method="get">
  63. <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">
  64. <path fill-rule="nonzero" fill="currentColor" 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"></path>
  65. </svg>
  66. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" />
  67. <input type="submit" value="Go"/>
  68. </form>
  69. </span>
  70. </nav>
  71. <div class="menu-wrapper">
  72. <nav class="menu" role="navigation" aria-label="main navigation">
  73. <div class="language_switcher_placeholder"></div>
  74. <label class="theme-selector-label">
  75. Theme
  76. <select class="theme-selector" oninput="activateTheme(this.value)">
  77. <option value="auto" selected>Auto</option>
  78. <option value="light">Light</option>
  79. <option value="dark">Dark</option>
  80. </select>
  81. </label>
  82. <div>
  83. <h3><a href="../contents.html">Table of Contents</a></h3>
  84. <ul>
  85. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> — Python interface to Tcl/Tk</a><ul>
  86. <li><a class="reference internal" href="#architecture">Architecture</a></li>
  87. <li><a class="reference internal" href="#tkinter-modules">Tkinter Modules</a></li>
  88. <li><a class="reference internal" href="#tkinter-life-preserver">Tkinter Life Preserver</a><ul>
  89. <li><a class="reference internal" href="#a-hello-world-program">A Hello World Program</a></li>
  90. <li><a class="reference internal" href="#important-tk-concepts">Important Tk Concepts</a></li>
  91. <li><a class="reference internal" href="#understanding-how-tkinter-wraps-tcl-tk">Understanding How Tkinter Wraps Tcl/Tk</a></li>
  92. <li><a class="reference internal" href="#how-do-i-what-option-does">How do I…? What option does…?</a></li>
  93. <li><a class="reference internal" href="#navigating-the-tcl-tk-reference-manual">Navigating the Tcl/Tk Reference Manual</a></li>
  94. </ul>
  95. </li>
  96. <li><a class="reference internal" href="#threading-model">Threading model</a></li>
  97. <li><a class="reference internal" href="#handy-reference">Handy Reference</a><ul>
  98. <li><a class="reference internal" href="#setting-options">Setting Options</a></li>
  99. <li><a class="reference internal" href="#the-packer">The Packer</a></li>
  100. <li><a class="reference internal" href="#packer-options">Packer Options</a></li>
  101. <li><a class="reference internal" href="#coupling-widget-variables">Coupling Widget Variables</a></li>
  102. <li><a class="reference internal" href="#the-window-manager">The Window Manager</a></li>
  103. <li><a class="reference internal" href="#tk-option-data-types">Tk Option Data Types</a></li>
  104. <li><a class="reference internal" href="#bindings-and-events">Bindings and Events</a></li>
  105. <li><a class="reference internal" href="#the-index-parameter">The index Parameter</a></li>
  106. <li><a class="reference internal" href="#images">Images</a></li>
  107. </ul>
  108. </li>
  109. <li><a class="reference internal" href="#file-handlers">File Handlers</a></li>
  110. </ul>
  111. </li>
  112. </ul>
  113. </div>
  114. <div>
  115. <h4>Previous topic</h4>
  116. <p class="topless"><a href="tk.html"
  117. title="previous chapter">Graphical User Interfaces with Tk</a></p>
  118. </div>
  119. <div>
  120. <h4>Next topic</h4>
  121. <p class="topless"><a href="tkinter.colorchooser.html"
  122. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.colorchooser</span></code> — Color choosing dialog</a></p>
  123. </div>
  124. <div role="note" aria-label="source link">
  125. <h3>This Page</h3>
  126. <ul class="this-page-menu">
  127. <li><a href="../bugs.html">Report a Bug</a></li>
  128. <li>
  129. <a href="https://github.com/python/cpython/blob/main/Doc/library/tkinter.rst"
  130. rel="nofollow">Show Source
  131. </a>
  132. </li>
  133. </ul>
  134. </div>
  135. </nav>
  136. </div>
  137. </div>
  138. <div class="related" role="navigation" aria-label="related navigation">
  139. <h3>Navigation</h3>
  140. <ul>
  141. <li class="right" style="margin-right: 10px">
  142. <a href="../genindex.html" title="General Index"
  143. accesskey="I">index</a></li>
  144. <li class="right" >
  145. <a href="../py-modindex.html" title="Python Module Index"
  146. >modules</a> |</li>
  147. <li class="right" >
  148. <a href="tkinter.colorchooser.html" title="tkinter.colorchooser — Color choosing dialog"
  149. accesskey="N">next</a> |</li>
  150. <li class="right" >
  151. <a href="tk.html" title="Graphical User Interfaces with Tk"
  152. accesskey="P">previous</a> |</li>
  153. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  154. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  155. <li class="switchers">
  156. <div class="language_switcher_placeholder"></div>
  157. <div class="version_switcher_placeholder"></div>
  158. </li>
  159. <li>
  160. </li>
  161. <li id="cpython-language-and-version">
  162. <a href="../index.html">3.12.3 Documentation</a> &#187;
  163. </li>
  164. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  165. <li class="nav-item nav-item-2"><a href="tk.html" accesskey="U">Graphical User Interfaces with Tk</a> &#187;</li>
  166. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> — Python interface to Tcl/Tk</a></li>
  167. <li class="right">
  168. <div class="inline-search" role="search">
  169. <form class="inline-search" action="../search.html" method="get">
  170. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  171. <input type="submit" value="Go" />
  172. </form>
  173. </div>
  174. |
  175. </li>
  176. <li class="right">
  177. <label class="theme-selector-label">
  178. Theme
  179. <select class="theme-selector" oninput="activateTheme(this.value)">
  180. <option value="auto" selected>Auto</option>
  181. <option value="light">Light</option>
  182. <option value="dark">Dark</option>
  183. </select>
  184. </label> |</li>
  185. </ul>
  186. </div>
  187. <div class="document">
  188. <div class="documentwrapper">
  189. <div class="bodywrapper">
  190. <div class="body" role="main">
  191. <section id="module-tkinter">
  192. <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> — Python interface to Tcl/Tk<a class="headerlink" href="#module-tkinter" title="Link to this heading">¶</a></h1>
  193. <p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.12/Lib/tkinter/__init__.py">Lib/tkinter/__init__.py</a></p>
  194. <hr class="docutils" />
  195. <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 (“Tk interface”) is the standard Python interface to
  196. the Tcl/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
  197. platforms, including macOS, as well as on Windows systems.</p>
  198. <p>Running <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">tkinter</span></code> from the command line should open a window
  199. demonstrating a simple Tk interface, letting you know that <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> is
  200. properly installed on your system, and also showing what version of Tcl/Tk is
  201. installed, so you can read the Tcl/Tk documentation specific to that version.</p>
  202. <p>Tkinter supports a range of Tcl/Tk versions, built either with or
  203. without thread support. The official Python binary release bundles Tcl/Tk 8.6
  204. threaded. See the source code for the <a class="reference internal" href="#module-_tkinter" title="_tkinter: A binary module that contains the low-level interface to Tcl/Tk."><code class="xref py py-mod docutils literal notranslate"><span class="pre">_tkinter</span></code></a> module
  205. for more information about supported versions.</p>
  206. <p>Tkinter is not a thin wrapper, but adds a fair amount of its own logic to
  207. make the experience more pythonic. This documentation will concentrate on these
  208. additions and changes, and refer to the official Tcl/Tk documentation for
  209. details that are unchanged.</p>
  210. <div class="admonition note">
  211. <p class="admonition-title">Note</p>
  212. <p>Tcl/Tk 8.5 (2007) introduced a modern set of themed user interface components
  213. along with a new API to use them. Both old and new APIs are still available.
  214. Most documentation you will find online still uses the old API and
  215. can be woefully outdated.</p>
  216. </div>
  217. <div class="admonition seealso">
  218. <p class="admonition-title">See also</p>
  219. <ul class="simple">
  220. <li><dl class="simple">
  221. <dt><a class="reference external" href="https://tkdocs.com/">TkDocs</a></dt><dd><p>Extensive tutorial on creating user interfaces with Tkinter. Explains key concepts,
  222. and illustrates recommended approaches using the modern API.</p>
  223. </dd>
  224. </dl>
  225. </li>
  226. <li><dl class="simple">
  227. <dt><a class="reference external" href="https://www.tkdocs.com/shipman/">Tkinter 8.5 reference: a GUI for Python</a></dt><dd><p>Reference documentation for Tkinter 8.5 detailing available classes, methods, and options.</p>
  228. </dd>
  229. </dl>
  230. </li>
  231. </ul>
  232. <p>Tcl/Tk Resources:</p>
  233. <ul class="simple">
  234. <li><dl class="simple">
  235. <dt><a class="reference external" href="https://www.tcl.tk/man/tcl8.6/TkCmd/contents.htm">Tk commands</a></dt><dd><p>Comprehensive reference to each of the underlying Tcl/Tk commands used by Tkinter.</p>
  236. </dd>
  237. </dl>
  238. </li>
  239. <li><dl class="simple">
  240. <dt><a class="reference external" href="https://www.tcl.tk">Tcl/Tk Home Page</a></dt><dd><p>Additional documentation, and links to Tcl/Tk core development.</p>
  241. </dd>
  242. </dl>
  243. </li>
  244. </ul>
  245. <p>Books:</p>
  246. <ul class="simple">
  247. <li><dl class="simple">
  248. <dt><a class="reference external" href="https://tkdocs.com/book.html">Modern Tkinter for Busy Python Developers</a></dt><dd><p>By Mark Roseman. (ISBN 978-1999149567)</p>
  249. </dd>
  250. </dl>
  251. </li>
  252. <li><dl class="simple">
  253. <dt><a class="reference external" href="https://www.packtpub.com/product/python-gui-programming-with-tkinter/9781788835886">Python GUI programming with Tkinter</a></dt><dd><p>By Alan D. Moore. (ISBN 978-1788835886)</p>
  254. </dd>
  255. </dl>
  256. </li>
  257. <li><dl class="simple">
  258. <dt><a class="reference external" href="https://learning-python.com/about-pp4e.html">Programming Python</a></dt><dd><p>By Mark Lutz; has excellent coverage of Tkinter. (ISBN 978-0596158101)</p>
  259. </dd>
  260. </dl>
  261. </li>
  262. <li><dl class="simple">
  263. <dt><a class="reference external" href="https://www.amazon.com/exec/obidos/ASIN/032133633X">Tcl and the Tk Toolkit (2nd edition)</a></dt><dd><p>By John Ousterhout, inventor of Tcl/Tk, and Ken Jones; does not cover Tkinter. (ISBN 978-0321336330)</p>
  264. </dd>
  265. </dl>
  266. </li>
  267. </ul>
  268. </div>
  269. <section id="architecture">
  270. <h2>Architecture<a class="headerlink" href="#architecture" title="Link to this heading">¶</a></h2>
  271. <p>Tcl/Tk is not a single library but rather consists of a few distinct
  272. modules, each with separate functionality and its own official
  273. documentation. Python’s binary releases also ship an add-on module
  274. together with it.</p>
  275. <dl class="simple">
  276. <dt>Tcl</dt><dd><p>Tcl is a dynamic interpreted programming language, just like Python. Though
  277. it can be used on its own as a general-purpose programming language, it is
  278. most commonly embedded into C applications as a scripting engine or an
  279. interface to the Tk toolkit. The Tcl library has a C interface to
  280. create and manage one or more instances of a Tcl interpreter, run Tcl
  281. commands and scripts in those instances, and add custom commands
  282. implemented in either Tcl or C. Each interpreter has an event queue,
  283. and there are facilities to send events to it and process them.
  284. Unlike Python, Tcl’s execution model is designed around cooperative
  285. multitasking, and Tkinter bridges this difference
  286. (see <a class="reference internal" href="#threading-model">Threading model</a> for details).</p>
  287. </dd>
  288. <dt>Tk</dt><dd><p>Tk is a <a class="reference external" href="https://wiki.tcl-lang.org/37432">Tcl package</a> implemented in C
  289. that adds custom commands to create and manipulate GUI widgets. Each
  290. <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> object embeds its own Tcl interpreter instance with Tk loaded into
  291. it. Tk’s widgets are very customizable, though at the cost of a dated appearance.
  292. Tk uses Tcl’s event queue to generate and process GUI events.</p>
  293. </dd>
  294. <dt>Ttk</dt><dd><p>Themed Tk (Ttk) is a newer family of Tk widgets that provide a much better
  295. appearance on different platforms than many of the classic Tk widgets.
  296. Ttk is distributed as part of Tk, starting with Tk version 8.5. Python
  297. bindings are provided in a separate module, <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>.</p>
  298. </dd>
  299. </dl>
  300. <p>Internally, Tk and Ttk use facilities of the underlying operating system,
  301. i.e., Xlib on Unix/X11, Cocoa on macOS, GDI on Windows.</p>
  302. <p>When your Python application uses a class in Tkinter, e.g., to create a widget,
  303. 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> module first assembles a Tcl/Tk command string. It passes that
  304. Tcl command string to an internal <a class="reference internal" href="#module-_tkinter" title="_tkinter: A binary module that contains the low-level interface to Tcl/Tk."><code class="xref py py-mod docutils literal notranslate"><span class="pre">_tkinter</span></code></a> binary module, which then
  305. calls the Tcl interpreter to evaluate it. The Tcl interpreter will then call into the
  306. Tk and/or Ttk packages, which will in turn make calls to Xlib, Cocoa, or GDI.</p>
  307. </section>
  308. <section id="tkinter-modules">
  309. <h2>Tkinter Modules<a class="headerlink" href="#tkinter-modules" title="Link to this heading">¶</a></h2>
  310. <p>Support for Tkinter is spread across several modules. Most applications will need the
  311. main <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> module, as well as the <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> module, which provides
  312. the modern themed widget set and API:</p>
  313. <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>
  314. <span class="kn">from</span> <span class="nn">tkinter</span> <span class="kn">import</span> <span class="n">ttk</span>
  315. </pre></div>
  316. </div>
  317. <dl class="py class">
  318. <dt class="sig sig-object py" id="tkinter.Tk">
  319. <em class="property"><span class="pre">class</span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">tkinter.</span></span><span class="sig-name descname"><span class="pre">Tk</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">screenName</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">baseName</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">className</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'Tk'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">useTk</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">sync</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tkinter.Tk" title="Link to this definition">¶</a></dt>
  320. <dd><p>Construct a toplevel Tk widget, which is usually the main window of an
  321. application, and initialize a Tcl interpreter for this widget. Each
  322. instance has its own associated Tcl interpreter.</p>
  323. <p>The <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> class is typically instantiated using all default values.
  324. However, the following keyword arguments are currently recognized:</p>
  325. <dl>
  326. <dt><em>screenName</em></dt><dd><p>When given (as a string), sets the <span class="target" id="index-0"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">DISPLAY</span></code> environment
  327. variable. (X11 only)</p>
  328. </dd>
  329. <dt><em>baseName</em></dt><dd><p>Name of the profile file. By default, <em>baseName</em> is derived from the
  330. program name (<code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code>).</p>
  331. </dd>
  332. <dt><em>className</em></dt><dd><p>Name of the widget class. Used as a profile file and also as the name
  333. with which Tcl is invoked (<em>argv0</em> in <em>interp</em>).</p>
  334. </dd>
  335. <dt><em>useTk</em></dt><dd><p>If <code class="docutils literal notranslate"><span class="pre">True</span></code>, initialize the Tk subsystem. The <a class="reference internal" href="#tkinter.Tcl" title="tkinter.Tcl"><code class="xref py py-func docutils literal notranslate"><span class="pre">tkinter.Tcl()</span></code></a>
  336. function sets this to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>
  337. </dd>
  338. <dt><em>sync</em></dt><dd><p>If <code class="docutils literal notranslate"><span class="pre">True</span></code>, execute all X server commands synchronously, so that errors
  339. are reported immediately. Can be used for debugging. (X11 only)</p>
  340. </dd>
  341. <dt><em>use</em></dt><dd><p>Specifies the <em>id</em> of the window in which to embed the application,
  342. instead of it being created as an independent toplevel window. <em>id</em> must
  343. be specified in the same way as the value for the -use option for
  344. toplevel widgets (that is, it has a form like that returned by
  345. <code class="xref py py-meth docutils literal notranslate"><span class="pre">winfo_id()</span></code>).</p>
  346. <p>Note that on some platforms this will only work correctly if <em>id</em> refers
  347. to a Tk frame or toplevel that has its -container option enabled.</p>
  348. </dd>
  349. </dl>
  350. <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> reads and interprets profile files, named
  351. <code class="file docutils literal notranslate"><span class="pre">.</span><em><span class="pre">className</span></em><span class="pre">.tcl</span></code> and <code class="file docutils literal notranslate"><span class="pre">.</span><em><span class="pre">baseName</span></em><span class="pre">.tcl</span></code>, into the Tcl
  352. interpreter and calls <a class="reference internal" href="functions.html#exec" title="exec"><code class="xref py py-func docutils literal notranslate"><span class="pre">exec()</span></code></a> on the contents of
  353. <code class="file docutils literal notranslate"><span class="pre">.</span><em><span class="pre">className</span></em><span class="pre">.py</span></code> and <code class="file docutils literal notranslate"><span class="pre">.</span><em><span class="pre">baseName</span></em><span class="pre">.py</span></code>. The path for the
  354. profile files is the <span class="target" id="index-1"></span><code class="xref std std-envvar docutils literal notranslate"><span class="pre">HOME</span></code> environment variable or, if that
  355. isn’t defined, then <a class="reference internal" href="os.html#os.curdir" title="os.curdir"><code class="xref py py-data docutils literal notranslate"><span class="pre">os.curdir</span></code></a>.</p>
  356. <dl class="py attribute">
  357. <dt class="sig sig-object py" id="tkinter.Tk.tk">
  358. <span class="sig-name descname"><span class="pre">tk</span></span><a class="headerlink" href="#tkinter.Tk.tk" title="Link to this definition">¶</a></dt>
  359. <dd><p>The Tk application object created by instantiating <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>. This
  360. provides access to the Tcl interpreter. Each widget that is attached
  361. the same instance of <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> has the same value for its <a class="reference internal" href="#tkinter.Tk.tk" title="tkinter.Tk.tk"><code class="xref py py-attr docutils literal notranslate"><span class="pre">tk</span></code></a>
  362. attribute.</p>
  363. </dd></dl>
  364. <dl class="py attribute">
  365. <dt class="sig sig-object py" id="tkinter.Tk.master">
  366. <span class="sig-name descname"><span class="pre">master</span></span><a class="headerlink" href="#tkinter.Tk.master" title="Link to this definition">¶</a></dt>
  367. <dd><p>The widget object that contains this widget. For <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>, the
  368. <em>master</em> is <a class="reference internal" href="constants.html#None" title="None"><code class="xref py py-const docutils literal notranslate"><span class="pre">None</span></code></a> because it is the main window. The terms
  369. <em>master</em> and <em>parent</em> are similar and sometimes used interchangeably
  370. as argument names; however, calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">winfo_parent()</span></code> returns a
  371. string of the widget name whereas <a class="reference internal" href="#tkinter.Tk.master" title="tkinter.Tk.master"><code class="xref py py-attr docutils literal notranslate"><span class="pre">master</span></code></a> returns the object.
  372. <em>parent</em>/<em>child</em> reflects the tree-like relationship while
  373. <em>master</em>/<em>slave</em> reflects the container structure.</p>
  374. </dd></dl>
  375. <dl class="py attribute">
  376. <dt class="sig sig-object py" id="tkinter.Tk.children">
  377. <span class="sig-name descname"><span class="pre">children</span></span><a class="headerlink" href="#tkinter.Tk.children" title="Link to this definition">¶</a></dt>
  378. <dd><p>The immediate descendants of this widget as a <a class="reference internal" href="stdtypes.html#dict" title="dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> with the
  379. child widget names as the keys and the child instance objects as the
  380. values.</p>
  381. </dd></dl>
  382. </dd></dl>
  383. <dl class="py function">
  384. <dt class="sig sig-object py" id="tkinter.Tcl">
  385. <span class="sig-prename descclassname"><span class="pre">tkinter.</span></span><span class="sig-name descname"><span class="pre">Tcl</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">screenName</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">baseName</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">className</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'Tk'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">useTk</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">False</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tkinter.Tcl" title="Link to this definition">¶</a></dt>
  386. <dd><p>The <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> function is a factory function which creates an object much like
  387. that created by the <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> class, except that it does not initialize the Tk
  388. subsystem. This is most often useful when driving the Tcl interpreter in an
  389. environment where one doesn’t want to create extraneous toplevel windows, or
  390. where one cannot (such as Unix/Linux systems without an X server). An object
  391. created by the <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> object can have a Toplevel window created (and the Tk
  392. subsystem initialized) by calling its <code class="xref py py-meth docutils literal notranslate"><span class="pre">loadtk()</span></code> method.</p>
  393. </dd></dl>
  394. <p>The modules that provide Tk support include:</p>
  395. <dl class="simple">
  396. <dt><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></dt><dd><p>Main Tkinter module.</p>
  397. </dd>
  398. <dt><a class="reference internal" href="tkinter.colorchooser.html#module-tkinter.colorchooser" title="tkinter.colorchooser: Color choosing dialog (Tk)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.colorchooser</span></code></a></dt><dd><p>Dialog to let the user choose a color.</p>
  399. </dd>
  400. <dt><a class="reference internal" href="dialog.html#module-tkinter.commondialog" title="tkinter.commondialog: Tkinter base class for dialogs (Tk)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.commondialog</span></code></a></dt><dd><p>Base class for the dialogs defined in the other modules listed here.</p>
  401. </dd>
  402. <dt><a class="reference internal" href="dialog.html#module-tkinter.filedialog" title="tkinter.filedialog: Dialog classes for file selection (Tk)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.filedialog</span></code></a></dt><dd><p>Common dialogs to allow the user to specify a file to open or save.</p>
  403. </dd>
  404. <dt><a class="reference internal" href="tkinter.font.html#module-tkinter.font" title="tkinter.font: Tkinter font-wrapping class (Tk)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.font</span></code></a></dt><dd><p>Utilities to help work with fonts.</p>
  405. </dd>
  406. <dt><a class="reference internal" href="tkinter.messagebox.html#module-tkinter.messagebox" title="tkinter.messagebox: Various types of alert dialogs (Tk)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.messagebox</span></code></a></dt><dd><p>Access to standard Tk dialog boxes.</p>
  407. </dd>
  408. <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>Text widget with a vertical scroll bar built in.</p>
  409. </dd>
  410. <dt><a class="reference internal" href="dialog.html#module-tkinter.simpledialog" title="tkinter.simpledialog: Simple dialog windows (Tk)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.simpledialog</span></code></a></dt><dd><p>Basic dialogs and convenience functions.</p>
  411. </dd>
  412. <dt><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></dt><dd><p>Themed widget set introduced in Tk 8.5, providing modern alternatives
  413. for many of the classic widgets in the main <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> module.</p>
  414. </dd>
  415. </dl>
  416. <p>Additional modules:</p>
  417. <dl class="simple" id="module-_tkinter">
  418. <dt><a class="reference internal" href="#module-_tkinter" title="_tkinter: A binary module that contains the low-level interface to Tcl/Tk."><code class="xref py py-mod docutils literal notranslate"><span class="pre">_tkinter</span></code></a></dt><dd><p>A binary module that contains the low-level interface to Tcl/Tk.
  419. It is automatically imported by the main <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> module,
  420. and should never be used directly by application programmers.
  421. It is usually a shared library (or DLL), but might in some cases be
  422. statically linked with the Python interpreter.</p>
  423. </dd>
  424. <dt><a class="reference internal" href="idle.html#module-idlelib" title="idlelib: Implementation package for the IDLE shell/editor."><code class="xref py py-mod docutils literal notranslate"><span class="pre">idlelib</span></code></a></dt><dd><p>Python’s Integrated Development and Learning Environment (IDLE). Based
  425. on <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>
  426. </dd>
  427. <dt><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.constants</span></code></dt><dd><p>Symbolic constants that can be used in place of strings when passing
  428. various parameters to Tkinter calls. Automatically imported by the
  429. main <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> module.</p>
  430. </dd>
  431. <dt><a class="reference internal" href="tkinter.dnd.html#module-tkinter.dnd" title="tkinter.dnd: Tkinter drag-and-drop interface (Tk)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.dnd</span></code></a></dt><dd><p>(experimental) Drag-and-drop support for <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>. This will
  432. become deprecated when it is replaced with the Tk DND.</p>
  433. </dd>
  434. <dt><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></dt><dd><p>(deprecated) An older third-party Tcl/Tk package that adds several new
  435. widgets. Better alternatives for most can be found in <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>.</p>
  436. </dd>
  437. <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>Turtle graphics in a Tk window.</p>
  438. </dd>
  439. </dl>
  440. </section>
  441. <section id="tkinter-life-preserver">
  442. <h2>Tkinter Life Preserver<a class="headerlink" href="#tkinter-life-preserver" title="Link to this heading">¶</a></h2>
  443. <p>This section is not designed to be an exhaustive tutorial on either Tk or
  444. Tkinter. For that, refer to one of the external resources noted earlier.
  445. Instead, this section provides a very quick orientation to what a Tkinter
  446. application looks like, identifies foundational Tk concepts, and
  447. explains how the Tkinter wrapper is structured.</p>
  448. <p>The remainder of this section will help you to identify the classes,
  449. methods, and options you’ll need in your Tkinter application, and where to
  450. find more detailed documentation on them, including in the official Tcl/Tk
  451. reference manual.</p>
  452. <section id="a-hello-world-program">
  453. <h3>A Hello World Program<a class="headerlink" href="#a-hello-world-program" title="Link to this heading">¶</a></h3>
  454. <p>We’ll start by walking through a “Hello World” application in Tkinter. This
  455. isn’t the smallest one we could write, but has enough to illustrate some
  456. key concepts you’ll need to know.</p>
  457. <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>
  458. <span class="kn">from</span> <span class="nn">tkinter</span> <span class="kn">import</span> <span class="n">ttk</span>
  459. <span class="n">root</span> <span class="o">=</span> <span class="n">Tk</span><span class="p">()</span>
  460. <span class="n">frm</span> <span class="o">=</span> <span class="n">ttk</span><span class="o">.</span><span class="n">Frame</span><span class="p">(</span><span class="n">root</span><span class="p">,</span> <span class="n">padding</span><span class="o">=</span><span class="mi">10</span><span class="p">)</span>
  461. <span class="n">frm</span><span class="o">.</span><span class="n">grid</span><span class="p">()</span>
  462. <span class="n">ttk</span><span class="o">.</span><span class="n">Label</span><span class="p">(</span><span class="n">frm</span><span class="p">,</span> <span class="n">text</span><span class="o">=</span><span class="s2">&quot;Hello World!&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="n">column</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">row</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
  463. <span class="n">ttk</span><span class="o">.</span><span class="n">Button</span><span class="p">(</span><span class="n">frm</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">command</span><span class="o">=</span><span class="n">root</span><span class="o">.</span><span class="n">destroy</span><span class="p">)</span><span class="o">.</span><span class="n">grid</span><span class="p">(</span><span class="n">column</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">row</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
  464. <span class="n">root</span><span class="o">.</span><span class="n">mainloop</span><span class="p">()</span>
  465. </pre></div>
  466. </div>
  467. <p>After the imports, the next line creates an instance of the <code class="xref py py-class docutils literal notranslate"><span class="pre">Tk</span></code> class,
  468. which initializes Tk and creates its associated Tcl interpreter. It also
  469. creates a toplevel window, known as the root window, which serves as the main
  470. window of the application.</p>
  471. <p>The following line creates a frame widget, which in this case will contain
  472. a label and a button we’ll create next. The frame is fit inside the root
  473. window.</p>
  474. <p>The next line creates a label widget holding a static text string. The
  475. <code class="xref py py-meth docutils literal notranslate"><span class="pre">grid()</span></code> method is used to specify the relative layout (position) of the
  476. label within its containing frame widget, similar to how tables in HTML work.</p>
  477. <p>A button widget is then created, and placed to the right of the label. When
  478. pressed, it will call the <code class="xref py py-meth docutils literal notranslate"><span class="pre">destroy()</span></code> method of the root window.</p>
  479. <p>Finally, the <code class="xref py py-meth docutils literal notranslate"><span class="pre">mainloop()</span></code> method puts everything on the display, and
  480. responds to user input until the program terminates.</p>
  481. </section>
  482. <section id="important-tk-concepts">
  483. <h3>Important Tk Concepts<a class="headerlink" href="#important-tk-concepts" title="Link to this heading">¶</a></h3>
  484. <p>Even this simple program illustrates the following key Tk concepts:</p>
  485. <dl class="simple">
  486. <dt>widgets</dt><dd><p>A Tkinter user interface is made up of individual <em>widgets</em>. Each widget is
  487. represented as a Python object, instantiated from classes like
  488. <code class="xref py py-class docutils literal notranslate"><span class="pre">ttk.Frame</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">ttk.Label</span></code>, and <code class="xref py py-class docutils literal notranslate"><span class="pre">ttk.Button</span></code>.</p>
  489. </dd>
  490. <dt>widget hierarchy</dt><dd><p>Widgets are arranged in a <em>hierarchy</em>. The label and button were contained
  491. within a frame, which in turn was contained within the root window. When
  492. creating each <em>child</em> widget, its <em>parent</em> widget is passed as the first
  493. argument to the widget constructor.</p>
  494. </dd>
  495. <dt>configuration options</dt><dd><p>Widgets have <em>configuration options</em>, which modify their appearance and
  496. behavior, such as the text to display in a label or button. Different
  497. classes of widgets will have different sets of options.</p>
  498. </dd>
  499. <dt>geometry management</dt><dd><p>Widgets aren’t automatically added to the user interface when they are
  500. created. A <em>geometry manager</em> like <code class="docutils literal notranslate"><span class="pre">grid</span></code> controls where in the
  501. user interface they are placed.</p>
  502. </dd>
  503. <dt>event loop</dt><dd><p>Tkinter reacts to user input, changes from your program, and even refreshes
  504. the display only when actively running an <em>event loop</em>. If your program
  505. isn’t running the event loop, your user interface won’t update.</p>
  506. </dd>
  507. </dl>
  508. </section>
  509. <section id="understanding-how-tkinter-wraps-tcl-tk">
  510. <h3>Understanding How Tkinter Wraps Tcl/Tk<a class="headerlink" href="#understanding-how-tkinter-wraps-tcl-tk" title="Link to this heading">¶</a></h3>
  511. <p>When your application uses Tkinter’s classes and methods, internally Tkinter
  512. is assembling strings representing Tcl/Tk commands, and executing those
  513. commands in the Tcl interpreter attached to your application’s <code class="xref py py-class docutils literal notranslate"><span class="pre">Tk</span></code>
  514. instance.</p>
  515. <p>Whether it’s trying to navigate reference documentation, trying to find
  516. the right method or option, adapting some existing code, or debugging your
  517. Tkinter application, there are times that it will be useful to understand
  518. what those underlying Tcl/Tk commands look like.</p>
  519. <p>To illustrate, here is the Tcl/Tk equivalent of the main part of the Tkinter
  520. script above.</p>
  521. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">ttk</span><span class="p">::</span><span class="n">frame</span> <span class="o">.</span><span class="n">frm</span> <span class="o">-</span><span class="n">padding</span> <span class="mi">10</span>
  522. <span class="n">grid</span> <span class="o">.</span><span class="n">frm</span>
  523. <span class="n">grid</span> <span class="p">[</span><span class="n">ttk</span><span class="p">::</span><span class="n">label</span> <span class="o">.</span><span class="n">frm</span><span class="o">.</span><span class="n">lbl</span> <span class="o">-</span><span class="n">text</span> <span class="s2">&quot;Hello World!&quot;</span><span class="p">]</span> <span class="o">-</span><span class="n">column</span> <span class="mi">0</span> <span class="o">-</span><span class="n">row</span> <span class="mi">0</span>
  524. <span class="n">grid</span> <span class="p">[</span><span class="n">ttk</span><span class="p">::</span><span class="n">button</span> <span class="o">.</span><span class="n">frm</span><span class="o">.</span><span class="n">btn</span> <span class="o">-</span><span class="n">text</span> <span class="s2">&quot;Quit&quot;</span> <span class="o">-</span><span class="n">command</span> <span class="s2">&quot;destroy .&quot;</span><span class="p">]</span> <span class="o">-</span><span class="n">column</span> <span class="mi">1</span> <span class="o">-</span><span class="n">row</span> <span class="mi">0</span>
  525. </pre></div>
  526. </div>
  527. <p>Tcl’s syntax is similar to many shell languages, where the first word is the
  528. command to be executed, with arguments to that command following it, separated
  529. by spaces. Without getting into too many details, notice the following:</p>
  530. <ul class="simple">
  531. <li><p>The commands used to create widgets (like <code class="docutils literal notranslate"><span class="pre">ttk::frame</span></code>) correspond to
  532. widget classes in Tkinter.</p></li>
  533. <li><p>Tcl widget options (like <code class="docutils literal notranslate"><span class="pre">-text</span></code>) correspond to keyword arguments in
  534. Tkinter.</p></li>
  535. <li><p>Widgets are referred to by a <em>pathname</em> in Tcl (like <code class="docutils literal notranslate"><span class="pre">.frm.btn</span></code>),
  536. whereas Tkinter doesn’t use names but object references.</p></li>
  537. <li><p>A widget’s place in the widget hierarchy is encoded in its (hierarchical)
  538. pathname, which uses a <code class="docutils literal notranslate"><span class="pre">.</span></code> (dot) as a path separator. The pathname for
  539. the root window is just <code class="docutils literal notranslate"><span class="pre">.</span></code> (dot). In Tkinter, the hierarchy is defined
  540. not by pathname but by specifying the parent widget when creating each
  541. child widget.</p></li>
  542. <li><p>Operations which are implemented as separate <em>commands</em> in Tcl (like
  543. <code class="docutils literal notranslate"><span class="pre">grid</span></code> or <code class="docutils literal notranslate"><span class="pre">destroy</span></code>) are represented as <em>methods</em> on Tkinter widget
  544. objects. As you’ll see shortly, at other times Tcl uses what appear to be
  545. method calls on widget objects, which more closely mirror what would is
  546. used in Tkinter.</p></li>
  547. </ul>
  548. </section>
  549. <section id="how-do-i-what-option-does">
  550. <h3>How do I…? What option does…?<a class="headerlink" href="#how-do-i-what-option-does" title="Link to this heading">¶</a></h3>
  551. <p>If you’re not sure how to do something in Tkinter, and you can’t immediately
  552. find it in the tutorial or reference documentation you’re using, there are a
  553. few strategies that can be helpful.</p>
  554. <p>First, remember that the details of how individual widgets work may vary
  555. across different versions of both Tkinter and Tcl/Tk. If you’re searching
  556. documentation, make sure it corresponds to the Python and Tcl/Tk versions
  557. installed on your system.</p>
  558. <p>When searching for how to use an API, it helps to know the exact name of the
  559. class, option, or method that you’re using. Introspection, either in an
  560. interactive Python shell or with <a class="reference internal" href="functions.html#print" title="print"><code class="xref py py-func docutils literal notranslate"><span class="pre">print()</span></code></a>, can help you identify what
  561. you need.</p>
  562. <p>To find out what configuration options are available on any widget, call its
  563. <code class="xref py py-meth docutils literal notranslate"><span class="pre">configure()</span></code> method, which returns a dictionary containing a variety of
  564. information about each object, including its default and current values. Use
  565. <code class="xref py py-meth docutils literal notranslate"><span class="pre">keys()</span></code> to get just the names of each option.</p>
  566. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">btn</span> <span class="o">=</span> <span class="n">ttk</span><span class="o">.</span><span class="n">Button</span><span class="p">(</span><span class="n">frm</span><span class="p">,</span> <span class="o">...</span><span class="p">)</span>
  567. <span class="nb">print</span><span class="p">(</span><span class="n">btn</span><span class="o">.</span><span class="n">configure</span><span class="p">()</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>
  568. </pre></div>
  569. </div>
  570. <p>As most widgets have many configuration options in common, it can be useful
  571. to find out which are specific to a particular widget class. Comparing the
  572. list of options to that of a simpler widget, like a frame, is one way to
  573. do that.</p>
  574. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="n">btn</span><span class="o">.</span><span class="n">configure</span><span class="p">()</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="n">frm</span><span class="o">.</span><span class="n">configure</span><span class="p">()</span><span class="o">.</span><span class="n">keys</span><span class="p">()))</span>
  575. </pre></div>
  576. </div>
  577. <p>Similarly, you can find the available methods for a widget object using the
  578. standard <a class="reference internal" href="functions.html#dir" title="dir"><code class="xref py py-func docutils literal notranslate"><span class="pre">dir()</span></code></a> function. If you try it, you’ll see there are over 200
  579. common widget methods, so again identifying those specific to a widget class
  580. is helpful.</p>
  581. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="nb">dir</span><span class="p">(</span><span class="n">btn</span><span class="p">))</span>
  582. <span class="nb">print</span><span class="p">(</span><span class="nb">set</span><span class="p">(</span><span class="nb">dir</span><span class="p">(</span><span class="n">btn</span><span class="p">))</span> <span class="o">-</span> <span class="nb">set</span><span class="p">(</span><span class="nb">dir</span><span class="p">(</span><span class="n">frm</span><span class="p">)))</span>
  583. </pre></div>
  584. </div>
  585. </section>
  586. <section id="navigating-the-tcl-tk-reference-manual">
  587. <h3>Navigating the Tcl/Tk Reference Manual<a class="headerlink" href="#navigating-the-tcl-tk-reference-manual" title="Link to this heading">¶</a></h3>
  588. <p>As noted, the official <a class="reference external" href="https://www.tcl.tk/man/tcl8.6/TkCmd/contents.htm">Tk commands</a>
  589. reference manual (man pages) is often the most accurate description of what
  590. specific operations on widgets do. Even when you know the name of the option
  591. or method that you need, you may still have a few places to look.</p>
  592. <p>While all operations in Tkinter are implemented as method calls on widget
  593. objects, you’ve seen that many Tcl/Tk operations appear as commands that
  594. take a widget pathname as its first parameter, followed by optional
  595. parameters, e.g.</p>
  596. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">destroy</span> <span class="o">.</span>
  597. <span class="n">grid</span> <span class="o">.</span><span class="n">frm</span><span class="o">.</span><span class="n">btn</span> <span class="o">-</span><span class="n">column</span> <span class="mi">0</span> <span class="o">-</span><span class="n">row</span> <span class="mi">0</span>
  598. </pre></div>
  599. </div>
  600. <p>Others, however, look more like methods called on a widget object (in fact,
  601. when you create a widget in Tcl/Tk, it creates a Tcl command with the name
  602. of the widget pathname, with the first parameter to that command being the
  603. name of a method to call).</p>
  604. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="o">.</span><span class="n">frm</span><span class="o">.</span><span class="n">btn</span> <span class="n">invoke</span>
  605. <span class="o">.</span><span class="n">frm</span><span class="o">.</span><span class="n">lbl</span> <span class="n">configure</span> <span class="o">-</span><span class="n">text</span> <span class="s2">&quot;Goodbye&quot;</span>
  606. </pre></div>
  607. </div>
  608. <p>In the official Tcl/Tk reference documentation, you’ll find most operations
  609. that look like method calls on the man page for a specific widget (e.g.,
  610. you’ll find the <code class="xref py py-meth docutils literal notranslate"><span class="pre">invoke()</span></code> method on the
  611. <a class="reference external" href="https://www.tcl.tk/man/tcl8.6/TkCmd/ttk_button.htm">ttk::button</a>
  612. man page), while functions that take a widget as a parameter often have
  613. their own man page (e.g.,
  614. <a class="reference external" href="https://www.tcl.tk/man/tcl8.6/TkCmd/grid.htm">grid</a>).</p>
  615. <p>You’ll find many common options and methods in the
  616. <a class="reference external" href="https://www.tcl.tk/man/tcl8.6/TkCmd/options.htm">options</a> or
  617. <a class="reference external" href="https://www.tcl.tk/man/tcl8.6/TkCmd/ttk_widget.htm">ttk::widget</a> man
  618. pages, while others are found in the man page for a specific widget class.</p>
  619. <p>You’ll also find that many Tkinter methods have compound names, e.g.,
  620. <code class="xref py py-func docutils literal notranslate"><span class="pre">winfo_x()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">winfo_height()</span></code>, <code class="xref py py-func docutils literal notranslate"><span class="pre">winfo_viewable()</span></code>. You’d find
  621. documentation for all of these in the
  622. <a class="reference external" href="https://www.tcl.tk/man/tcl8.6/TkCmd/winfo.htm">winfo</a> man page.</p>
  623. <div class="admonition note">
  624. <p class="admonition-title">Note</p>
  625. <p>Somewhat confusingly, there are also methods on all Tkinter widgets
  626. that don’t actually operate on the widget, but operate at a global
  627. scope, independent of any widget. Examples are methods for accessing
  628. the clipboard or the system bell. (They happen to be implemented as
  629. methods in the base <code class="xref py py-class docutils literal notranslate"><span class="pre">Widget</span></code> class that all Tkinter widgets
  630. inherit from).</p>
  631. </div>
  632. </section>
  633. </section>
  634. <section id="threading-model">
  635. <h2>Threading model<a class="headerlink" href="#threading-model" title="Link to this heading">¶</a></h2>
  636. <p>Python and Tcl/Tk have very different threading models, which <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>
  637. tries to bridge. If you use threads, you may need to be aware of this.</p>
  638. <p>A Python interpreter may have many threads associated with it. In Tcl, multiple
  639. threads can be created, but each thread has a separate Tcl interpreter instance
  640. associated with it. Threads can also create more than one interpreter instance,
  641. though each interpreter instance can be used only by the one thread that created it.</p>
  642. <p>Each <code class="xref py py-class docutils literal notranslate"><span class="pre">Tk</span></code> object created by <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> contains a Tcl interpreter.
  643. It also keeps track of which thread created that interpreter. Calls to
  644. <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> can be made from any Python thread. Internally, if a call comes
  645. from a thread other than the one that created the <code class="xref py py-class docutils literal notranslate"><span class="pre">Tk</span></code> object, an event
  646. is posted to the interpreter’s event queue, and when executed, the result is
  647. returned to the calling Python thread.</p>
  648. <p>Tcl/Tk applications are normally event-driven, meaning that after initialization,
  649. the interpreter runs an event loop (i.e. <code class="xref py py-func docutils literal notranslate"><span class="pre">Tk.mainloop()</span></code>) and responds to events.
  650. Because it is single-threaded, event handlers must respond quickly, otherwise they
  651. will block other events from being processed. To avoid this, any long-running
  652. computations should not run in an event handler, but are either broken into smaller
  653. pieces using timers, or run in another thread. This is different from many GUI
  654. toolkits where the GUI runs in a completely separate thread from all application
  655. code including event handlers.</p>
  656. <p>If the Tcl interpreter is not running the event loop and processing events, any
  657. <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> calls made from threads other than the one running the Tcl
  658. interpreter will fail.</p>
  659. <p>A number of special cases exist:</p>
  660. <ul class="simple">
  661. <li><p>Tcl/Tk libraries can be built so they are not thread-aware. In this case,
  662. <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> calls the library from the originating Python thread, even
  663. if this is different than the thread that created the Tcl interpreter. A global
  664. lock ensures only one call occurs at a time.</p></li>
  665. <li><p>While <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> allows you to create more than one instance of a <code class="xref py py-class docutils literal notranslate"><span class="pre">Tk</span></code>
  666. object (with its own interpreter), all interpreters that are part of the same
  667. thread share a common event queue, which gets ugly fast. In practice, don’t create
  668. more than one instance of <code class="xref py py-class docutils literal notranslate"><span class="pre">Tk</span></code> at a time. Otherwise, it’s best to create
  669. them in separate threads and ensure you’re running a thread-aware Tcl/Tk build.</p></li>
  670. <li><p>Blocking event handlers are not the only way to prevent the Tcl interpreter from
  671. reentering the event loop. It is even possible to run multiple nested event loops
  672. or abandon the event loop entirely. If you’re doing anything tricky when it comes
  673. to events or threads, be aware of these possibilities.</p></li>
  674. <li><p>There are a few select <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> functions that presently work only when
  675. called from the thread that created the Tcl interpreter.</p></li>
  676. </ul>
  677. </section>
  678. <section id="handy-reference">
  679. <h2>Handy Reference<a class="headerlink" href="#handy-reference" title="Link to this heading">¶</a></h2>
  680. <section id="setting-options">
  681. <span id="tkinter-setting-options"></span><h3>Setting Options<a class="headerlink" href="#setting-options" title="Link to this heading">¶</a></h3>
  682. <p>Options control things like the color and border width of a widget. Options can
  683. be set in three ways:</p>
  684. <dl>
  685. <dt>At object creation time, using keyword arguments</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>
  686. </pre></div>
  687. </div>
  688. </dd>
  689. <dt>After object creation, treating the option name like a dictionary index</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>
  690. <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>
  691. </pre></div>
  692. </div>
  693. </dd>
  694. <dt>Use the config() method to update multiple attrs subsequent to object creation</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>
  695. </pre></div>
  696. </div>
  697. </dd>
  698. </dl>
  699. <p>For a complete explanation of a given option and its behavior, see the Tk man
  700. pages for the widget in question.</p>
  701. <p>Note that the man pages list “STANDARD OPTIONS” and “WIDGET SPECIFIC OPTIONS”
  702. for each widget. The former is a list of options that are common to many
  703. widgets, the latter are the options that are idiosyncratic to that particular
  704. widget. The Standard Options are documented on the <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/options(3)">options(3)</a></em> man
  705. page.</p>
  706. <p>No distinction between standard and widget-specific options is made in this
  707. document. Some options don’t apply to some kinds of widgets. Whether a given
  708. widget responds to a particular option depends on the class of the widget;
  709. buttons have a <code class="docutils literal notranslate"><span class="pre">command</span></code> option, labels do not.</p>
  710. <p>The options supported by a given widget are listed in that widget’s man page, or
  711. can be queried at runtime by calling the <code class="xref py py-meth docutils literal notranslate"><span class="pre">config()</span></code> method without
  712. arguments, or by calling the <code class="xref py py-meth docutils literal notranslate"><span class="pre">keys()</span></code> method on that widget. The return
  713. value of these calls is a dictionary whose key is the name of the option as a
  714. string (for example, <code class="docutils literal notranslate"><span class="pre">'relief'</span></code>) and whose values are 5-tuples.</p>
  715. <p>Some options, like <code class="docutils literal notranslate"><span class="pre">bg</span></code> are synonyms for common options with long names
  716. (<code class="docutils literal notranslate"><span class="pre">bg</span></code> is shorthand for “background”). Passing the <code class="docutils literal notranslate"><span class="pre">config()</span></code> method the name
  717. of a shorthand option will return a 2-tuple, not 5-tuple. The 2-tuple passed
  718. back will contain the name of the synonym and the “real” option (such as
  719. <code class="docutils literal notranslate"><span class="pre">('bg',</span> <span class="pre">'background')</span></code>).</p>
  720. <table class="docutils align-default">
  721. <thead>
  722. <tr class="row-odd"><th class="head"><p>Index</p></th>
  723. <th class="head"><p>Meaning</p></th>
  724. <th class="head"><p>Example</p></th>
  725. </tr>
  726. </thead>
  727. <tbody>
  728. <tr class="row-even"><td><p>0</p></td>
  729. <td><p>option name</p></td>
  730. <td><p><code class="docutils literal notranslate"><span class="pre">'relief'</span></code></p></td>
  731. </tr>
  732. <tr class="row-odd"><td><p>1</p></td>
  733. <td><p>option name for database lookup</p></td>
  734. <td><p><code class="docutils literal notranslate"><span class="pre">'relief'</span></code></p></td>
  735. </tr>
  736. <tr class="row-even"><td><p>2</p></td>
  737. <td><p>option class for database
  738. lookup</p></td>
  739. <td><p><code class="docutils literal notranslate"><span class="pre">'Relief'</span></code></p></td>
  740. </tr>
  741. <tr class="row-odd"><td><p>3</p></td>
  742. <td><p>default value</p></td>
  743. <td><p><code class="docutils literal notranslate"><span class="pre">'raised'</span></code></p></td>
  744. </tr>
  745. <tr class="row-even"><td><p>4</p></td>
  746. <td><p>current value</p></td>
  747. <td><p><code class="docutils literal notranslate"><span class="pre">'groove'</span></code></p></td>
  748. </tr>
  749. </tbody>
  750. </table>
  751. <p>Example:</p>
  752. <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>
  753. <span class="go">{&#39;relief&#39;: (&#39;relief&#39;, &#39;relief&#39;, &#39;Relief&#39;, &#39;raised&#39;, &#39;groove&#39;)}</span>
  754. </pre></div>
  755. </div>
  756. <p>Of course, the dictionary printed will include all the options available and
  757. their values. This is meant only as an example.</p>
  758. </section>
  759. <section id="the-packer">
  760. <h3>The Packer<a class="headerlink" href="#the-packer" title="Link to this heading">¶</a></h3>
  761. <p id="index-2">The packer is one of Tk’s geometry-management mechanisms. Geometry managers
  762. are used to specify the relative positioning of widgets within their container -
  763. their mutual <em>master</em>. In contrast to the more cumbersome <em>placer</em> (which is
  764. used less commonly, and we do not cover here), the packer takes qualitative
  765. relationship specification - <em>above</em>, <em>to the left of</em>, <em>filling</em>, etc - and
  766. works everything out to determine the exact placement coordinates for you.</p>
  767. <p>The size of any <em>master</em> widget is determined by the size of the “slave widgets”
  768. inside. The packer is used to control where slave widgets appear inside the
  769. master into which they are packed. You can pack widgets into frames, and frames
  770. into other frames, in order to achieve the kind of layout you desire.
  771. Additionally, the arrangement is dynamically adjusted to accommodate incremental
  772. changes to the configuration, once it is packed.</p>
  773. <p>Note that widgets do not appear until they have had their geometry specified
  774. with a geometry manager. It’s a common early mistake to leave out the geometry
  775. specification, and then be surprised when the widget is created but nothing
  776. appears. A widget will appear only after it has had, for example, the packer’s
  777. <code class="xref py py-meth docutils literal notranslate"><span class="pre">pack()</span></code> method applied to it.</p>
  778. <p>The pack() method can be called with keyword-option/value pairs that control
  779. where the widget is to appear within its container, and how it is to behave when
  780. the main application window is resized. Here are some examples:</p>
  781. <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>
  782. <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>
  783. <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>
  784. </pre></div>
  785. </div>
  786. </section>
  787. <section id="packer-options">
  788. <h3>Packer Options<a class="headerlink" href="#packer-options" title="Link to this heading">¶</a></h3>
  789. <p>For more extensive information on the packer and the options that it can take,
  790. see the man pages and page 183 of John Ousterhout’s book.</p>
  791. <dl class="simple">
  792. <dt>anchor</dt><dd><p>Anchor type. Denotes where the packer is to place each slave in its parcel.</p>
  793. </dd>
  794. <dt>expand</dt><dd><p>Boolean, <code class="docutils literal notranslate"><span class="pre">0</span></code> or <code class="docutils literal notranslate"><span class="pre">1</span></code>.</p>
  795. </dd>
  796. <dt>fill</dt><dd><p>Legal values: <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>
  797. </dd>
  798. <dt>ipadx and ipady</dt><dd><p>A distance - designating internal padding on each side of the slave widget.</p>
  799. </dd>
  800. <dt>padx and pady</dt><dd><p>A distance - designating external padding on each side of the slave widget.</p>
  801. </dd>
  802. <dt>side</dt><dd><p>Legal values are: <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>
  803. </dd>
  804. </dl>
  805. </section>
  806. <section id="coupling-widget-variables">
  807. <h3>Coupling Widget Variables<a class="headerlink" href="#coupling-widget-variables" title="Link to this heading">¶</a></h3>
  808. <p>The current-value setting of some widgets (like text entry widgets) can be
  809. connected directly to application variables by using special options. These
  810. options are <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>, and
  811. <code class="docutils literal notranslate"><span class="pre">value</span></code>. This connection works both ways: if the variable changes for any
  812. reason, the widget it’s connected to will be updated to reflect the new value.</p>
  813. <p>Unfortunately, in the current implementation of <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> it is not
  814. possible to hand over an arbitrary Python variable to a widget through a
  815. <code class="docutils literal notranslate"><span class="pre">variable</span></code> or <code class="docutils literal notranslate"><span class="pre">textvariable</span></code> option. The only kinds of variables for which
  816. this works are variables that are subclassed from a class called Variable,
  817. defined in <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>
  818. <p>There are many useful subclasses of Variable already defined:
  819. <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>, and
  820. <code class="xref py py-class docutils literal notranslate"><span class="pre">BooleanVar</span></code>. To read the current value of such a variable, call the
  821. <code class="xref py py-meth docutils literal notranslate"><span class="pre">get()</span></code> method on it, and to change its value you call the <code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code>
  822. method. If you follow this protocol, the widget will always track the value of
  823. the variable, with no further intervention on your part.</p>
  824. <p>For example:</p>
  825. <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>
  826. <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>
  827. <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>
  828. <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>
  829. <span class="bp">self</span><span class="o">.</span><span class="n">pack</span><span class="p">()</span>
  830. <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>
  831. <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>
  832. <span class="c1"># Create the application variable.</span>
  833. <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>
  834. <span class="c1"># Set it to some value.</span>
  835. <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>
  836. <span class="c1"># Tell the entry widget to watch this variable.</span>
  837. <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>
  838. <span class="c1"># Define a callback for when the user hits return.</span>
  839. <span class="c1"># It prints the current value of the variable.</span>
  840. <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>
  841. <span class="bp">self</span><span class="o">.</span><span class="n">print_contents</span><span class="p">)</span>
  842. <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>
  843. <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>
  844. <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>
  845. <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>
  846. <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>
  847. <span class="n">myapp</span><span class="o">.</span><span class="n">mainloop</span><span class="p">()</span>
  848. </pre></div>
  849. </div>
  850. </section>
  851. <section id="the-window-manager">
  852. <h3>The Window Manager<a class="headerlink" href="#the-window-manager" title="Link to this heading">¶</a></h3>
  853. <p id="index-3">In Tk, there is a utility command, <code class="docutils literal notranslate"><span class="pre">wm</span></code>, for interacting with the window
  854. manager. Options to the <code class="docutils literal notranslate"><span class="pre">wm</span></code> command allow you to control things like titles,
  855. placement, icon bitmaps, and the like. In <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>, these commands have
  856. been implemented as methods on the <code class="xref py py-class docutils literal notranslate"><span class="pre">Wm</span></code> class. Toplevel widgets are
  857. subclassed from the <code class="xref py py-class docutils literal notranslate"><span class="pre">Wm</span></code> class, and so can call the <code class="xref py py-class docutils literal notranslate"><span class="pre">Wm</span></code> methods
  858. directly.</p>
  859. <p>To get at the toplevel window that contains a given widget, you can often just
  860. refer to the widget’s master. Of course if the widget has been packed inside of
  861. a frame, the master won’t represent a toplevel window. To get at the toplevel
  862. window that contains an arbitrary widget, you can call the <code class="xref py py-meth docutils literal notranslate"><span class="pre">_root()</span></code> method.
  863. This method begins with an underscore to denote the fact that this function is
  864. part of the implementation, and not an interface to Tk functionality.</p>
  865. <p>Here are some examples of typical usage:</p>
  866. <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>
  867. <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>
  868. <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>
  869. <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>
  870. <span class="bp">self</span><span class="o">.</span><span class="n">pack</span><span class="p">()</span>
  871. <span class="c1"># create the application</span>
  872. <span class="n">myapp</span> <span class="o">=</span> <span class="n">App</span><span class="p">()</span>
  873. <span class="c1">#</span>
  874. <span class="c1"># here are method calls to the window manager class</span>
  875. <span class="c1">#</span>
  876. <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>
  877. <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>
  878. <span class="c1"># start the program</span>
  879. <span class="n">myapp</span><span class="o">.</span><span class="n">mainloop</span><span class="p">()</span>
  880. </pre></div>
  881. </div>
  882. </section>
  883. <section id="tk-option-data-types">
  884. <h3>Tk Option Data Types<a class="headerlink" href="#tk-option-data-types" title="Link to this heading">¶</a></h3>
  885. <dl id="index-4">
  886. <dt>anchor</dt><dd><p>Legal values are points of the compass: <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>,
  887. <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>, and also <code class="docutils literal notranslate"><span class="pre">&quot;center&quot;</span></code>.</p>
  888. </dd>
  889. <dt>bitmap</dt><dd><p>There are eight built-in, named bitmaps: <code class="docutils literal notranslate"><span class="pre">'error'</span></code>, <code class="docutils literal notranslate"><span class="pre">'gray25'</span></code>,
  890. <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>,
  891. <code class="docutils literal notranslate"><span class="pre">'warning'</span></code>. To specify an X bitmap filename, give the full path to the file,
  892. preceded with an <code class="docutils literal notranslate"><span class="pre">&#64;</span></code>, as in <code class="docutils literal notranslate"><span class="pre">&quot;&#64;/usr/contrib/bitmap/gumby.bit&quot;</span></code>.</p>
  893. </dd>
  894. <dt>boolean</dt><dd><p>You can pass integers 0 or 1 or the strings <code class="docutils literal notranslate"><span class="pre">&quot;yes&quot;</span></code> or <code class="docutils literal notranslate"><span class="pre">&quot;no&quot;</span></code>.</p>
  895. </dd>
  896. <dt>callback</dt><dd><p>This is any Python function that takes no arguments. For example:</p>
  897. <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>
  898. <span class="nb">print</span><span class="p">(</span><span class="s2">&quot;hi there&quot;</span><span class="p">)</span>
  899. <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>
  900. </pre></div>
  901. </div>
  902. </dd>
  903. <dt>color</dt><dd><p>Colors can be given as the names of X colors in the rgb.txt file, or as strings
  904. representing RGB values in 4 bit: <code class="docutils literal notranslate"><span class="pre">&quot;#RGB&quot;</span></code>, 8 bit: <code class="docutils literal notranslate"><span class="pre">&quot;#RRGGBB&quot;</span></code>, 12 bit:
  905. <code class="docutils literal notranslate"><span class="pre">&quot;#RRRGGGBBB&quot;</span></code>, or 16 bit: <code class="docutils literal notranslate"><span class="pre">&quot;#RRRRGGGGBBBB&quot;</span></code> ranges, where R,G,B here
  906. represent any legal hex digit. See page 160 of Ousterhout’s book for details.</p>
  907. </dd>
  908. <dt>cursor</dt><dd><p>The standard X cursor names from <code class="file docutils literal notranslate"><span class="pre">cursorfont.h</span></code> can be used, without the
  909. <code class="docutils literal notranslate"><span class="pre">XC_</span></code> prefix. For example to get a hand cursor (<code class="xref py py-const docutils literal notranslate"><span class="pre">XC_hand2</span></code>), use the
  910. string <code class="docutils literal notranslate"><span class="pre">&quot;hand2&quot;</span></code>. You can also specify a bitmap and mask file of your own.
  911. See page 179 of Ousterhout’s book.</p>
  912. </dd>
  913. <dt>distance</dt><dd><p>Screen distances can be specified in either pixels or absolute distances.
  914. Pixels are given as numbers and absolute distances as strings, with the trailing
  915. character denoting units: <code class="docutils literal notranslate"><span class="pre">c</span></code> for centimetres, <code class="docutils literal notranslate"><span class="pre">i</span></code> for inches, <code class="docutils literal notranslate"><span class="pre">m</span></code> for
  916. millimetres, <code class="docutils literal notranslate"><span class="pre">p</span></code> for printer’s points. For example, 3.5 inches is expressed
  917. as <code class="docutils literal notranslate"><span class="pre">&quot;3.5i&quot;</span></code>.</p>
  918. </dd>
  919. <dt>font</dt><dd><p>Tk uses a list font name format, such as <code class="docutils literal notranslate"><span class="pre">{courier</span> <span class="pre">10</span> <span class="pre">bold}</span></code>. Font sizes with
  920. positive numbers are measured in points; sizes with negative numbers are
  921. measured in pixels.</p>
  922. </dd>
  923. <dt>geometry</dt><dd><p>This is a string of the form <code class="docutils literal notranslate"><span class="pre">widthxheight</span></code>, where width and height are
  924. measured in pixels for most widgets (in characters for widgets displaying text).
  925. For example: <code class="docutils literal notranslate"><span class="pre">fred[&quot;geometry&quot;]</span> <span class="pre">=</span> <span class="pre">&quot;200x100&quot;</span></code>.</p>
  926. </dd>
  927. <dt>justify</dt><dd><p>Legal values are the strings: <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>, and
  928. <code class="docutils literal notranslate"><span class="pre">&quot;fill&quot;</span></code>.</p>
  929. </dd>
  930. <dt>region</dt><dd><p>This is a string with four space-delimited elements, each of which is a legal
  931. distance (see above). For example: <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> and <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> and
  932. <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> are all legal regions.</p>
  933. </dd>
  934. <dt>relief</dt><dd><p>Determines what the border style of a widget will be. Legal values are:
  935. <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>, and <code class="docutils literal notranslate"><span class="pre">&quot;ridge&quot;</span></code>.</p>
  936. </dd>
  937. <dt>scrollcommand</dt><dd><p>This is almost always the <code class="xref py py-meth docutils literal notranslate"><span class="pre">set()</span></code> method of some scrollbar widget, but can
  938. be any widget method that takes a single argument.</p>
  939. </dd>
  940. <dt>wrap</dt><dd><p>Must be one of: <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>, or <code class="docutils literal notranslate"><span class="pre">&quot;word&quot;</span></code>.</p>
  941. </dd>
  942. </dl>
  943. </section>
  944. <section id="bindings-and-events">
  945. <span id="id2"></span><h3>Bindings and Events<a class="headerlink" href="#bindings-and-events" title="Link to this heading">¶</a></h3>
  946. <p id="index-5">The bind method from the widget command allows you to watch for certain events
  947. and to have a callback function trigger when that event type occurs. The form
  948. of the bind method is:</p>
  949. <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>
  950. </pre></div>
  951. </div>
  952. <p>where:</p>
  953. <dl class="simple">
  954. <dt>sequence</dt><dd><p>is a string that denotes the target kind of event. (See the
  955. <em class="manpage"><a class="manpage reference external" href="https://manpages.debian.org/bind(3tk)">bind(3tk)</a></em> man page, and page 201 of John Ousterhout’s book,
  956. <cite>Tcl and the Tk Toolkit (2nd edition)</cite>, for details).</p>
  957. </dd>
  958. <dt>func</dt><dd><p>is a Python function, taking one argument, to be invoked when the event occurs.
  959. An Event instance will be passed as the argument. (Functions deployed this way
  960. are commonly known as <em>callbacks</em>.)</p>
  961. </dd>
  962. <dt>add</dt><dd><p>is optional, either <code class="docutils literal notranslate"><span class="pre">''</span></code> or <code class="docutils literal notranslate"><span class="pre">'+'</span></code>. Passing an empty string denotes that
  963. this binding is to replace any other bindings that this event is associated
  964. with. Passing a <code class="docutils literal notranslate"><span class="pre">'+'</span></code> means that this function is to be added to the list
  965. of functions bound to this event type.</p>
  966. </dd>
  967. </dl>
  968. <p>For example:</p>
  969. <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>
  970. <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>
  971. <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>
  972. </pre></div>
  973. </div>
  974. <p>Notice how the widget field of the event is being accessed in the
  975. <code class="docutils literal notranslate"><span class="pre">turn_red()</span></code> callback. This field contains the widget that caught the X
  976. event. The following table lists the other event fields you can access, and how
  977. they are denoted in Tk, which can be useful when referring to the Tk man pages.</p>
  978. <table class="docutils align-default">
  979. <thead>
  980. <tr class="row-odd"><th class="head"><p>Tk</p></th>
  981. <th class="head"><p>Tkinter Event Field</p></th>
  982. <th class="head"><p>Tk</p></th>
  983. <th class="head"><p>Tkinter Event Field</p></th>
  984. </tr>
  985. </thead>
  986. <tbody>
  987. <tr class="row-even"><td><p>%f</p></td>
  988. <td><p>focus</p></td>
  989. <td><p>%A</p></td>
  990. <td><p>char</p></td>
  991. </tr>
  992. <tr class="row-odd"><td><p>%h</p></td>
  993. <td><p>height</p></td>
  994. <td><p>%E</p></td>
  995. <td><p>send_event</p></td>
  996. </tr>
  997. <tr class="row-even"><td><p>%k</p></td>
  998. <td><p>keycode</p></td>
  999. <td><p>%K</p></td>
  1000. <td><p>keysym</p></td>
  1001. </tr>
  1002. <tr class="row-odd"><td><p>%s</p></td>
  1003. <td><p>state</p></td>
  1004. <td><p>%N</p></td>
  1005. <td><p>keysym_num</p></td>
  1006. </tr>
  1007. <tr class="row-even"><td><p>%t</p></td>
  1008. <td><p>time</p></td>
  1009. <td><p>%T</p></td>
  1010. <td><p>type</p></td>
  1011. </tr>
  1012. <tr class="row-odd"><td><p>%w</p></td>
  1013. <td><p>width</p></td>
  1014. <td><p>%W</p></td>
  1015. <td><p>widget</p></td>
  1016. </tr>
  1017. <tr class="row-even"><td><p>%x</p></td>
  1018. <td><p>x</p></td>
  1019. <td><p>%X</p></td>
  1020. <td><p>x_root</p></td>
  1021. </tr>
  1022. <tr class="row-odd"><td><p>%y</p></td>
  1023. <td><p>y</p></td>
  1024. <td><p>%Y</p></td>
  1025. <td><p>y_root</p></td>
  1026. </tr>
  1027. </tbody>
  1028. </table>
  1029. </section>
  1030. <section id="the-index-parameter">
  1031. <h3>The index Parameter<a class="headerlink" href="#the-index-parameter" title="Link to this heading">¶</a></h3>
  1032. <p>A number of widgets require “index” parameters to be passed. These are used to
  1033. point at a specific place in a Text widget, or to particular characters in an
  1034. Entry widget, or to particular menu items in a Menu widget.</p>
  1035. <dl class="simple">
  1036. <dt>Entry widget indexes (index, view index, etc.)</dt><dd><p>Entry widgets have options that refer to character positions in the text being
  1037. displayed. You can use these <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> functions to access these special
  1038. points in text widgets:</p>
  1039. </dd>
  1040. <dt>Text widget indexes</dt><dd><p>The index notation for Text widgets is very rich and is best described in the Tk
  1041. man pages.</p>
  1042. </dd>
  1043. <dt>Menu indexes (menu.invoke(), menu.entryconfig(), etc.)</dt><dd><p>Some options and methods for menus manipulate specific menu entries. Anytime a
  1044. menu index is needed for an option or a parameter, you may pass in:</p>
  1045. <ul class="simple">
  1046. <li><p>an integer which refers to the numeric position of the entry in the widget,
  1047. counted from the top, starting with 0;</p></li>
  1048. <li><p>the string <code class="docutils literal notranslate"><span class="pre">&quot;active&quot;</span></code>, which refers to the menu position that is currently
  1049. under the cursor;</p></li>
  1050. <li><p>the string <code class="docutils literal notranslate"><span class="pre">&quot;last&quot;</span></code> which refers to the last menu item;</p></li>
  1051. <li><p>An integer preceded by <code class="docutils literal notranslate"><span class="pre">&#64;</span></code>, as in <code class="docutils literal notranslate"><span class="pre">&#64;6</span></code>, where the integer is interpreted
  1052. as a y pixel coordinate in the menu’s coordinate system;</p></li>
  1053. <li><p>the string <code class="docutils literal notranslate"><span class="pre">&quot;none&quot;</span></code>, which indicates no menu entry at all, most often used
  1054. with menu.activate() to deactivate all entries, and finally,</p></li>
  1055. <li><p>a text string that is pattern matched against the label of the menu entry, as
  1056. scanned from the top of the menu to the bottom. Note that this index type is
  1057. considered after all the others, which means that matches for menu items
  1058. labelled <code class="docutils literal notranslate"><span class="pre">last</span></code>, <code class="docutils literal notranslate"><span class="pre">active</span></code>, or <code class="docutils literal notranslate"><span class="pre">none</span></code> may be interpreted as the above
  1059. literals, instead.</p></li>
  1060. </ul>
  1061. </dd>
  1062. </dl>
  1063. </section>
  1064. <section id="images">
  1065. <h3>Images<a class="headerlink" href="#images" title="Link to this heading">¶</a></h3>
  1066. <p>Images of different formats can be created through the corresponding subclass
  1067. of <code class="xref py py-class docutils literal notranslate"><span class="pre">tkinter.Image</span></code>:</p>
  1068. <ul class="simple">
  1069. <li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">BitmapImage</span></code> for images in XBM format.</p></li>
  1070. <li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">PhotoImage</span></code> for images in PGM, PPM, GIF and PNG formats. The latter
  1071. is supported starting with Tk 8.6.</p></li>
  1072. </ul>
  1073. <p>Either type of image is created through either the <code class="docutils literal notranslate"><span class="pre">file</span></code> or the <code class="docutils literal notranslate"><span class="pre">data</span></code>
  1074. option (other options are available as well).</p>
  1075. <p>The image object can then be used wherever an <code class="docutils literal notranslate"><span class="pre">image</span></code> option is supported by
  1076. some widget (e.g. labels, buttons, menus). In these cases, Tk will not keep a
  1077. reference to the image. When the last Python reference to the image object is
  1078. deleted, the image data is deleted as well, and Tk will display an empty box
  1079. wherever the image was used.</p>
  1080. <div class="admonition seealso">
  1081. <p class="admonition-title">See also</p>
  1082. <p>The <a class="reference external" href="https://python-pillow.org/">Pillow</a> package adds support for
  1083. formats such as BMP, JPEG, TIFF, and WebP, among others.</p>
  1084. </div>
  1085. </section>
  1086. </section>
  1087. <section id="file-handlers">
  1088. <span id="tkinter-file-handlers"></span><h2>File Handlers<a class="headerlink" href="#file-handlers" title="Link to this heading">¶</a></h2>
  1089. <p>Tk allows you to register and unregister a callback function which will be
  1090. called from the Tk mainloop when I/O is possible on a file descriptor.
  1091. Only one handler may be registered per file descriptor. Example code:</p>
  1092. <div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">tkinter</span>
  1093. <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>
  1094. <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>
  1095. <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>
  1096. <span class="o">...</span>
  1097. <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>
  1098. </pre></div>
  1099. </div>
  1100. <p>This feature is not available on Windows.</p>
  1101. <p>Since you don’t know how many bytes are available for reading, you may not
  1102. want to use the <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> or <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>
  1103. <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> or <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> methods,
  1104. since these will insist on reading a predefined number of bytes.
  1105. For sockets, the <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> or
  1106. <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> methods will work fine; for other files,
  1107. use raw reads or <code class="docutils literal notranslate"><span class="pre">os.read(file.fileno(),</span> <span class="pre">maxbytecount)</span></code>.</p>
  1108. <dl class="py method">
  1109. <dt class="sig sig-object py" id="tkinter.Widget.tk.createfilehandler">
  1110. <span class="sig-prename descclassname"><span class="pre">Widget.tk.</span></span><span class="sig-name descname"><span class="pre">createfilehandler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">file</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">mask</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">func</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tkinter.Widget.tk.createfilehandler" title="Link to this definition">¶</a></dt>
  1111. <dd><p>Registers the file handler callback function <em>func</em>. The <em>file</em> argument
  1112. may either be an object with a <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> method (such as
  1113. a file or socket object), or an integer file descriptor. The <em>mask</em>
  1114. argument is an ORed combination of any of the three constants below.
  1115. The callback is called as follows:</p>
  1116. <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>
  1117. </pre></div>
  1118. </div>
  1119. </dd></dl>
  1120. <dl class="py method">
  1121. <dt class="sig sig-object py" id="tkinter.Widget.tk.deletefilehandler">
  1122. <span class="sig-prename descclassname"><span class="pre">Widget.tk.</span></span><span class="sig-name descname"><span class="pre">deletefilehandler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">file</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#tkinter.Widget.tk.deletefilehandler" title="Link to this definition">¶</a></dt>
  1123. <dd><p>Unregisters a file handler.</p>
  1124. </dd></dl>
  1125. <dl class="py data">
  1126. <dt class="sig sig-object py" id="tkinter.READABLE">
  1127. <span class="sig-prename descclassname"><span class="pre">_tkinter.</span></span><span class="sig-name descname"><span class="pre">READABLE</span></span><a class="headerlink" href="#tkinter.READABLE" title="Link to this definition">¶</a></dt>
  1128. <dt class="sig sig-object py" id="tkinter.WRITABLE">
  1129. <span class="sig-prename descclassname"><span class="pre">_tkinter.</span></span><span class="sig-name descname"><span class="pre">WRITABLE</span></span><a class="headerlink" href="#tkinter.WRITABLE" title="Link to this definition">¶</a></dt>
  1130. <dt class="sig sig-object py" id="tkinter.EXCEPTION">
  1131. <span class="sig-prename descclassname"><span class="pre">_tkinter.</span></span><span class="sig-name descname"><span class="pre">EXCEPTION</span></span><a class="headerlink" href="#tkinter.EXCEPTION" title="Link to this definition">¶</a></dt>
  1132. <dd><p>Constants used in the <em>mask</em> arguments.</p>
  1133. </dd></dl>
  1134. </section>
  1135. </section>
  1136. <div class="clearer"></div>
  1137. </div>
  1138. </div>
  1139. </div>
  1140. <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
  1141. <div class="sphinxsidebarwrapper">
  1142. <div>
  1143. <h3><a href="../contents.html">Table of Contents</a></h3>
  1144. <ul>
  1145. <li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> — Python interface to Tcl/Tk</a><ul>
  1146. <li><a class="reference internal" href="#architecture">Architecture</a></li>
  1147. <li><a class="reference internal" href="#tkinter-modules">Tkinter Modules</a></li>
  1148. <li><a class="reference internal" href="#tkinter-life-preserver">Tkinter Life Preserver</a><ul>
  1149. <li><a class="reference internal" href="#a-hello-world-program">A Hello World Program</a></li>
  1150. <li><a class="reference internal" href="#important-tk-concepts">Important Tk Concepts</a></li>
  1151. <li><a class="reference internal" href="#understanding-how-tkinter-wraps-tcl-tk">Understanding How Tkinter Wraps Tcl/Tk</a></li>
  1152. <li><a class="reference internal" href="#how-do-i-what-option-does">How do I…? What option does…?</a></li>
  1153. <li><a class="reference internal" href="#navigating-the-tcl-tk-reference-manual">Navigating the Tcl/Tk Reference Manual</a></li>
  1154. </ul>
  1155. </li>
  1156. <li><a class="reference internal" href="#threading-model">Threading model</a></li>
  1157. <li><a class="reference internal" href="#handy-reference">Handy Reference</a><ul>
  1158. <li><a class="reference internal" href="#setting-options">Setting Options</a></li>
  1159. <li><a class="reference internal" href="#the-packer">The Packer</a></li>
  1160. <li><a class="reference internal" href="#packer-options">Packer Options</a></li>
  1161. <li><a class="reference internal" href="#coupling-widget-variables">Coupling Widget Variables</a></li>
  1162. <li><a class="reference internal" href="#the-window-manager">The Window Manager</a></li>
  1163. <li><a class="reference internal" href="#tk-option-data-types">Tk Option Data Types</a></li>
  1164. <li><a class="reference internal" href="#bindings-and-events">Bindings and Events</a></li>
  1165. <li><a class="reference internal" href="#the-index-parameter">The index Parameter</a></li>
  1166. <li><a class="reference internal" href="#images">Images</a></li>
  1167. </ul>
  1168. </li>
  1169. <li><a class="reference internal" href="#file-handlers">File Handlers</a></li>
  1170. </ul>
  1171. </li>
  1172. </ul>
  1173. </div>
  1174. <div>
  1175. <h4>Previous topic</h4>
  1176. <p class="topless"><a href="tk.html"
  1177. title="previous chapter">Graphical User Interfaces with Tk</a></p>
  1178. </div>
  1179. <div>
  1180. <h4>Next topic</h4>
  1181. <p class="topless"><a href="tkinter.colorchooser.html"
  1182. title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.colorchooser</span></code> — Color choosing dialog</a></p>
  1183. </div>
  1184. <div role="note" aria-label="source link">
  1185. <h3>This Page</h3>
  1186. <ul class="this-page-menu">
  1187. <li><a href="../bugs.html">Report a Bug</a></li>
  1188. <li>
  1189. <a href="https://github.com/python/cpython/blob/main/Doc/library/tkinter.rst"
  1190. rel="nofollow">Show Source
  1191. </a>
  1192. </li>
  1193. </ul>
  1194. </div>
  1195. </div>
  1196. <div id="sidebarbutton" title="Collapse sidebar">
  1197. <span>«</span>
  1198. </div>
  1199. </div>
  1200. <div class="clearer"></div>
  1201. </div>
  1202. <div class="related" role="navigation" aria-label="related navigation">
  1203. <h3>Navigation</h3>
  1204. <ul>
  1205. <li class="right" style="margin-right: 10px">
  1206. <a href="../genindex.html" title="General Index"
  1207. >index</a></li>
  1208. <li class="right" >
  1209. <a href="../py-modindex.html" title="Python Module Index"
  1210. >modules</a> |</li>
  1211. <li class="right" >
  1212. <a href="tkinter.colorchooser.html" title="tkinter.colorchooser — Color choosing dialog"
  1213. >next</a> |</li>
  1214. <li class="right" >
  1215. <a href="tk.html" title="Graphical User Interfaces with Tk"
  1216. >previous</a> |</li>
  1217. <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"/></li>
  1218. <li><a href="https://www.python.org/">Python</a> &#187;</li>
  1219. <li class="switchers">
  1220. <div class="language_switcher_placeholder"></div>
  1221. <div class="version_switcher_placeholder"></div>
  1222. </li>
  1223. <li>
  1224. </li>
  1225. <li id="cpython-language-and-version">
  1226. <a href="../index.html">3.12.3 Documentation</a> &#187;
  1227. </li>
  1228. <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
  1229. <li class="nav-item nav-item-2"><a href="tk.html" >Graphical User Interfaces with Tk</a> &#187;</li>
  1230. <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> — Python interface to Tcl/Tk</a></li>
  1231. <li class="right">
  1232. <div class="inline-search" role="search">
  1233. <form class="inline-search" action="../search.html" method="get">
  1234. <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box" />
  1235. <input type="submit" value="Go" />
  1236. </form>
  1237. </div>
  1238. |
  1239. </li>
  1240. <li class="right">
  1241. <label class="theme-selector-label">
  1242. Theme
  1243. <select class="theme-selector" oninput="activateTheme(this.value)">
  1244. <option value="auto" selected>Auto</option>
  1245. <option value="light">Light</option>
  1246. <option value="dark">Dark</option>
  1247. </select>
  1248. </label> |</li>
  1249. </ul>
  1250. </div>
  1251. <div class="footer">
  1252. &copy;
  1253. <a href="../copyright.html">
  1254. Copyright
  1255. </a>
  1256. 2001-2024, Python Software Foundation.
  1257. <br />
  1258. This page is licensed under the Python Software Foundation License Version 2.
  1259. <br />
  1260. Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
  1261. <br />
  1262. See <a href="/license.html">History and License</a> for more information.<br />
  1263. <br />
  1264. The Python Software Foundation is a non-profit corporation.
  1265. <a href="https://www.python.org/psf/donations/">Please donate.</a>
  1266. <br />
  1267. <br />
  1268. Last updated on Apr 09, 2024 (13:47 UTC).
  1269. <a href="/bugs.html">Found a bug</a>?
  1270. <br />
  1271. Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 7.2.6.
  1272. </div>
  1273. </body>
  1274. </html>
上海开阖软件有限公司 沪ICP备12045867号-1