parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="tkinter — Python interface to Tcl/Tk" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/tkinter.html" />10<meta property="og:site_name" content="Python documentation" />11<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..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_tkinter_5fa89168.png" />15<meta property="og:image:alt" 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..." />16<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..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>tkinter — Python interface to Tcl/Tk — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="tkinter.colorchooser — Color choosing dialog" href="tkinter.colorchooser.html" />43 <link rel="prev" title="Graphical user interfaces with Tk" href="tk.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/tkinter.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <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>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<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>108<li><a class="reference internal" href="#architecture">Architecture</a></li>109<li><a class="reference internal" href="#tkinter-modules">Tkinter Modules</a></li>110<li><a class="reference internal" href="#tkinter-life-preserver">Tkinter Life Preserver</a><ul>111<li><a class="reference internal" href="#a-hello-world-program">A Hello World Program</a></li>112<li><a class="reference internal" href="#important-tk-concepts">Important Tk Concepts</a></li>113<li><a class="reference internal" href="#understanding-how-tkinter-wraps-tcl-tk">Understanding How Tkinter Wraps Tcl/Tk</a></li>114<li><a class="reference internal" href="#how-do-i-what-option-does">How do I…? What option does…?</a></li>115<li><a class="reference internal" href="#navigating-the-tcl-tk-reference-manual">Navigating the Tcl/Tk Reference Manual</a></li>116</ul>117</li>118<li><a class="reference internal" href="#threading-model">Threading model</a></li>119<li><a class="reference internal" href="#handy-reference">Handy Reference</a><ul>120<li><a class="reference internal" href="#setting-options">Setting Options</a></li>121<li><a class="reference internal" href="#the-packer">The Packer</a></li>122<li><a class="reference internal" href="#packer-options">Packer Options</a></li>123<li><a class="reference internal" href="#coupling-widget-variables">Coupling Widget Variables</a></li>124<li><a class="reference internal" href="#the-window-manager">The Window Manager</a></li>125<li><a class="reference internal" href="#tk-option-data-types">Tk Option Data Types</a></li>126<li><a class="reference internal" href="#bindings-and-events">Bindings and Events</a></li>127<li><a class="reference internal" href="#the-index-parameter">The index Parameter</a></li>128<li><a class="reference internal" href="#images">Images</a></li>129</ul>130</li>131<li><a class="reference internal" href="#file-handlers">File Handlers</a></li>132</ul>133</li>134</ul>135 136 </div>137 <div>138 <h4>Previous topic</h4>139 <p class="topless"><a href="tk.html"140 title="previous chapter">Graphical user interfaces with Tk</a></p>141 </div>142 <div>143 <h4>Next topic</h4>144 <p class="topless"><a href="tkinter.colorchooser.html"145 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter.colorchooser</span></code> — Color choosing dialog</a></p>146 </div>147 <script>148 document.addEventListener('DOMContentLoaded', () => {149 const title = document.querySelector('meta[property="og:title"]').content;150 const elements = document.querySelectorAll('.improvepage');151 const pageurl = window.location.href.split('?')[0];152 elements.forEach(element => {153 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));154 url.searchParams.set('pagetitle', title);155 url.searchParams.set('pageurl', pageurl);156 url.searchParams.set('pagesource', "library/tkinter.rst");157 element.href = url.toString();158 });159 });160 </script>161 <div role="note" aria-label="source link">162 <h3>This page</h3>163 <ul class="this-page-menu">164 <li><a href="../bugs.html">Report a bug</a></li>165 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>166 <li>167 <a href="https://github.com/python/cpython/blob/main/Doc/library/tkinter.rst?plain=1"168 rel="nofollow">Show source169 </a>170 </li>171 172 </ul>173 </div>174 </nav>175 </div>176</div>177 178 179 <div class="related" role="navigation" aria-label="Related">180 <h3>Navigation</h3>181 <ul>182 <li class="right" style="margin-right: 10px">183 <a href="../genindex.html" title="General Index"184 accesskey="I">index</a></li>185 <li class="right" >186 <a href="../py-modindex.html" title="Python Module Index"187 >modules</a> |</li>188 <li class="right" >189 <a href="tkinter.colorchooser.html" title="tkinter.colorchooser — Color choosing dialog"190 accesskey="N">next</a> |</li>191 <li class="right" >192 <a href="tk.html" title="Graphical user interfaces with Tk"193 accesskey="P">previous</a> |</li>194 195 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>196 <li><a href="https://www.python.org/">Python</a> »</li>197 <li class="switchers">198 <div class="language_switcher_placeholder"></div>199 <div class="version_switcher_placeholder"></div>200 </li>201 <li>202 203 </li>204 <li id="cpython-language-and-version">205 <a href="../index.html">3.15.0a6 Documentation</a> »206 </li>207 208 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>209 <li class="nav-item nav-item-2"><a href="tk.html" accesskey="U">Graphical user interfaces with Tk</a> »</li>210 <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>211 <li class="right">212 213 214 <div class="inline-search" role="search">215 <form class="inline-search" action="../search.html" method="get">216 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">217 <input type="submit" value="Go">218 </form>219 </div>220 |221 </li>222 <li class="right">223<label class="theme-selector-label">224 Theme225 <select class="theme-selector" oninput="activateTheme(this.value)">226 <option value="auto" selected>Auto</option>227 <option value="light">Light</option>228 <option value="dark">Dark</option>229 </select>230</label> |</li>231 232 </ul>233 </div> 234 235 <div class="document">236 <div class="documentwrapper">237 <div class="bodywrapper">238 <div class="body" role="main">239 240 <section id="module-tkinter">241<span id="tkinter-python-interface-to-tcl-tk"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> — Python interface to Tcl/Tk<a class="headerlink" href="#module-tkinter" title="Link to this heading">¶</a></h1>242<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/tkinter/__init__.py">Lib/tkinter/__init__.py</a></p>243<hr class="docutils" />244<p>The <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> package (“Tk interface”) is the standard Python interface to245the Tcl/Tk GUI toolkit. Both Tk and <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> are available on most Unix246platforms, including macOS, as well as on Windows systems.</p>247<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 window248demonstrating a simple Tk interface, letting you know that <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> is249properly installed on your system, and also showing what version of Tcl/Tk is250installed, so you can read the Tcl/Tk documentation specific to that version.</p>251<p>Tkinter supports a range of Tcl/Tk versions, built either with or252without thread support. The official Python binary release bundles Tcl/Tk 8.6253threaded. 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> module254for more information about supported versions.</p>255<p>Tkinter is not a thin wrapper, but adds a fair amount of its own logic to256make the experience more pythonic. This documentation will concentrate on these257additions and changes, and refer to the official Tcl/Tk documentation for258details that are unchanged.</p>259<div class="admonition note">260<p class="admonition-title">Note</p>261<p>Tcl/Tk 8.5 (2007) introduced a modern set of themed user interface components262along with a new API to use them. Both old and new APIs are still available.263Most documentation you will find online still uses the old API and264can be woefully outdated.</p>265</div>266<p>This is an <a class="reference internal" href="../glossary.html#term-optional-module"><span class="xref std std-term">optional module</span></a>.267If it is missing from your copy of CPython,268look for documentation from your distributor (that is,269whoever provided Python to you).270If you are the distributor, see <a class="reference internal" href="../using/configure.html#optional-module-requirements"><span class="std std-ref">Requirements for optional modules</span></a>.</p>271<div class="admonition seealso">272<p class="admonition-title">See also</p>273<ul class="simple">274<li><dl class="simple">275<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,276and illustrates recommended approaches using the modern API.</p>277</dd>278</dl>279</li>280<li><dl class="simple">281<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>282</dd>283</dl>284</li>285</ul>286<p>Tcl/Tk Resources:</p>287<ul class="simple">288<li><dl class="simple">289<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>290</dd>291</dl>292</li>293<li><dl class="simple">294<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>295</dd>296</dl>297</li>298</ul>299<p>Books:</p>300<ul class="simple">301<li><dl class="simple">302<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>303</dd>304</dl>305</li>306<li><dl class="simple">307<dt><a class="reference external" href="https://www.packtpub.com/en-us/product/python-gui-programming-with-tkinter-9781788835886">Python GUI programming with Tkinter</a></dt><dd><p>By Alan D. Moore. (ISBN 978-1788835886)</p>308</dd>309</dl>310</li>311<li><dl class="simple">312<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>313</dd>314</dl>315</li>316<li><dl class="simple">317<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>318</dd>319</dl>320</li>321</ul>322</div>323<section id="architecture">324<h2>Architecture<a class="headerlink" href="#architecture" title="Link to this heading">¶</a></h2>325<p>Tcl/Tk is not a single library but rather consists of a few distinct326modules, each with separate functionality and its own official327documentation. Python’s binary releases also ship an add-on module328together with it.</p>329<dl class="simple">330<dt>Tcl</dt><dd><p>Tcl is a dynamic interpreted programming language, just like Python. Though331it can be used on its own as a general-purpose programming language, it is332most commonly embedded into C applications as a scripting engine or an333interface to the Tk toolkit. The Tcl library has a C interface to334create and manage one or more instances of a Tcl interpreter, run Tcl335commands and scripts in those instances, and add custom commands336implemented in either Tcl or C. Each interpreter has an event queue,337and there are facilities to send events to it and process them.338Unlike Python, Tcl’s execution model is designed around cooperative339multitasking, and Tkinter bridges this difference340(see <a class="reference internal" href="#threading-model">Threading model</a> for details).</p>341</dd>342<dt>Tk</dt><dd><p>Tk is a <a class="reference external" href="https://wiki.tcl-lang.org/37432">Tcl package</a> implemented in C343that adds custom commands to create and manipulate GUI widgets. Each344<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 into345it. Tk’s widgets are very customizable, though at the cost of a dated appearance.346Tk uses Tcl’s event queue to generate and process GUI events.</p>347</dd>348<dt>Ttk</dt><dd><p>Themed Tk (Ttk) is a newer family of Tk widgets that provide a much better349appearance on different platforms than many of the classic Tk widgets.350Ttk is distributed as part of Tk, starting with Tk version 8.5. Python351bindings 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>352</dd>353</dl>354<p>Internally, Tk and Ttk use facilities of the underlying operating system,355i.e., Xlib on Unix/X11, Cocoa on macOS, GDI on Windows.</p>356<p>When your Python application uses a class in Tkinter, e.g., to create a widget,357the <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> module first assembles a Tcl/Tk command string. It passes that358Tcl 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 then359calls the Tcl interpreter to evaluate it. The Tcl interpreter will then call into the360Tk and/or Ttk packages, which will in turn make calls to Xlib, Cocoa, or GDI.</p>361</section>362<section id="tkinter-modules">363<h2>Tkinter Modules<a class="headerlink" href="#tkinter-modules" title="Link to this heading">¶</a></h2>364<p>Support for Tkinter is spread across several modules. Most applications will need the365main <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> 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 provides366the modern themed widget set and API:</p>367<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">tkinter</span><span class="w"> </span><span class="kn">import</span> <span class="o">*</span>368<span class="kn">from</span><span class="w"> </span><span class="nn">tkinter</span><span class="w"> </span><span class="kn">import</span> <span class="n">ttk</span>369</pre></div>370</div>371<dl class="py class">372<dt class="sig sig-object py" id="tkinter.Tk">373<em class="property"><span class="k"><span class="pre">class</span></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>374<dd><p>Construct a toplevel Tk widget, which is usually the main window of an375application, and initialize a Tcl interpreter for this widget. Each376instance has its own associated Tcl interpreter.</p>377<p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">Tk</span></code> class is typically instantiated using all default values.378However, the following keyword arguments are currently recognized:</p>379<dl>380<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> environment381variable. (X11 only)</p>382</dd>383<dt><em>baseName</em></dt><dd><p>Name of the profile file. By default, <em>baseName</em> is derived from the384program name (<code class="docutils literal notranslate"><span class="pre">sys.argv[0]</span></code>).</p>385</dd>386<dt><em>className</em></dt><dd><p>Name of the widget class. Used as a profile file and also as the name387with which Tcl is invoked (<em>argv0</em> in <em>interp</em>).</p>388</dd>389<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>390function sets this to <code class="docutils literal notranslate"><span class="pre">False</span></code>.</p>391</dd>392<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 errors393are reported immediately. Can be used for debugging. (X11 only)</p>394</dd>395<dt><em>use</em></dt><dd><p>Specifies the <em>id</em> of the window in which to embed the application,396instead of it being created as an independent toplevel window. <em>id</em> must397be specified in the same way as the value for the -use option for398toplevel widgets (that is, it has a form like that returned by399<code class="xref py py-meth docutils literal notranslate"><span class="pre">winfo_id()</span></code>).</p>400<p>Note that on some platforms this will only work correctly if <em>id</em> refers401to a Tk frame or toplevel that has its -container option enabled.</p>402</dd>403</dl>404<p><code class="xref py py-class docutils literal notranslate"><span class="pre">Tk</span></code> reads and interprets profile files, named405<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 Tcl406interpreter 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 of407<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 the408profile 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 that409isn’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>410<dl class="py attribute">411<dt class="sig sig-object py" id="tkinter.Tk.tk">412<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>413<dd><p>The Tk application object created by instantiating <code class="xref py py-class docutils literal notranslate"><span class="pre">Tk</span></code>. This414provides access to the Tcl interpreter. Each widget that is attached415the same instance of <code class="xref py py-class docutils literal notranslate"><span class="pre">Tk</span></code> 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>416attribute.</p>417</dd></dl>418 419<dl class="py attribute">420<dt class="sig sig-object py" id="tkinter.Tk.master">421<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>422<dd><p>The widget object that contains this widget. For <code class="xref py py-class docutils literal notranslate"><span class="pre">Tk</span></code>, the423<code class="xref py py-attr docutils literal notranslate"><span class="pre">master</span></code> 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 terms424<em>master</em> and <em>parent</em> are similar and sometimes used interchangeably425as argument names; however, calling <code class="xref py py-meth docutils literal notranslate"><span class="pre">winfo_parent()</span></code> returns a426string of the widget name whereas <code class="xref py py-attr docutils literal notranslate"><span class="pre">master</span></code> returns the object.427<em>parent</em>/<em>child</em> reflects the tree-like relationship while428<em>master</em> (or <em>container</em>)/<em>content</em> reflects the container structure.</p>429</dd></dl>430 431<dl class="py attribute">432<dt class="sig sig-object py" id="tkinter.Tk.children">433<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>434<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 the435child widget names as the keys and the child instance objects as the436values.</p>437</dd></dl>438 439</dd></dl>440 441<dl class="py function">442<dt class="sig sig-object py" id="tkinter.Tcl">443<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>444<dd><p>The <code class="xref py py-func docutils literal notranslate"><span class="pre">Tcl()</span></code> function is a factory function which creates an object much like445that 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 Tk446subsystem. This is most often useful when driving the Tcl interpreter in an447environment where one doesn’t want to create extraneous toplevel windows, or448where one cannot (such as Unix/Linux systems without an X server). An object449created by the <code class="xref py py-func docutils literal notranslate"><span class="pre">Tcl()</span></code> object can have a Toplevel window created (and the Tk450subsystem initialized) by calling its <code class="xref py py-meth docutils literal notranslate"><span class="pre">loadtk()</span></code> method.</p>451</dd></dl>452 453<p>The modules that provide Tk support include:</p>454<dl class="simple">455<dt><code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code></dt><dd><p>Main Tkinter module.</p>456</dd>457<dt><a class="reference internal" href="tkinter.colorchooser.html#module-tkinter.colorchooser" title="tkinter.colorchooser: Color choosing dialog"><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>458</dd>459<dt><a class="reference internal" href="dialog.html#module-tkinter.commondialog" title="tkinter.commondialog: Tkinter base class for dialogs"><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>460</dd>461<dt><a class="reference internal" href="dialog.html#module-tkinter.filedialog" title="tkinter.filedialog: Dialog classes for file selection"><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>462</dd>463<dt><a class="reference internal" href="tkinter.font.html#module-tkinter.font" title="tkinter.font: Tkinter font-wrapping class"><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>464</dd>465<dt><a class="reference internal" href="tkinter.messagebox.html#module-tkinter.messagebox" title="tkinter.messagebox: Various types of alert dialogs"><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>466</dd>467<dt><a class="reference internal" href="tkinter.scrolledtext.html#module-tkinter.scrolledtext" title="tkinter.scrolledtext: Text widget with a vertical scroll bar."><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>468</dd>469<dt><a class="reference internal" href="dialog.html#module-tkinter.simpledialog" title="tkinter.simpledialog: Simple dialog windows"><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>470</dd>471<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 alternatives472for many of the classic widgets in the main <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> module.</p>473</dd>474</dl>475<p>Additional modules:</p>476<dl class="simple" id="module-_tkinter">477<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.478It is automatically imported by the main <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> module,479and should never be used directly by application programmers.480It is usually a shared library (or DLL), but might in some cases be481statically linked with the Python interpreter.</p>482</dd>483<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). Based484on <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code>.</p>485</dd>486<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 passing487various parameters to Tkinter calls. Automatically imported by the488main <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> module.</p>489</dd>490<dt><a class="reference internal" href="tkinter.dnd.html#module-tkinter.dnd" title="tkinter.dnd: Tkinter drag-and-drop interface"><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 <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code>. This will491become deprecated when it is replaced with the Tk DND.</p>492</dd>493<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>494</dd>495</dl>496</section>497<section id="tkinter-life-preserver">498<h2>Tkinter Life Preserver<a class="headerlink" href="#tkinter-life-preserver" title="Link to this heading">¶</a></h2>499<p>This section is not designed to be an exhaustive tutorial on either Tk or500Tkinter. For that, refer to one of the external resources noted earlier.501Instead, this section provides a very quick orientation to what a Tkinter502application looks like, identifies foundational Tk concepts, and503explains how the Tkinter wrapper is structured.</p>504<p>The remainder of this section will help you to identify the classes,505methods, and options you’ll need in your Tkinter application, and where to506find more detailed documentation on them, including in the official Tcl/Tk507reference manual.</p>508<section id="a-hello-world-program">509<h3>A Hello World Program<a class="headerlink" href="#a-hello-world-program" title="Link to this heading">¶</a></h3>510<p>We’ll start by walking through a “Hello World” application in Tkinter. This511isn’t the smallest one we could write, but has enough to illustrate some512key concepts you’ll need to know.</p>513<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span><span class="w"> </span><span class="nn">tkinter</span><span class="w"> </span><span class="kn">import</span> <span class="o">*</span>514<span class="kn">from</span><span class="w"> </span><span class="nn">tkinter</span><span class="w"> </span><span class="kn">import</span> <span class="n">ttk</span>515<span class="n">root</span> <span class="o">=</span> <span class="n">Tk</span><span class="p">()</span>516<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>517<span class="n">frm</span><span class="o">.</span><span class="n">grid</span><span class="p">()</span>518<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">"Hello World!"</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>519<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">"Quit"</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>520<span class="n">root</span><span class="o">.</span><span class="n">mainloop</span><span class="p">()</span>521</pre></div>522</div>523<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,524which initializes Tk and creates its associated Tcl interpreter. It also525creates a toplevel window, known as the root window, which serves as the main526window of the application.</p>527<p>The following line creates a frame widget, which in this case will contain528a label and a button we’ll create next. The frame is fit inside the root529window.</p>530<p>The next line creates a label widget holding a static text string. The531<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 the532label within its containing frame widget, similar to how tables in HTML work.</p>533<p>A button widget is then created, and placed to the right of the label. When534pressed, 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>535<p>Finally, the <code class="xref py py-meth docutils literal notranslate"><span class="pre">mainloop()</span></code> method puts everything on the display, and536responds to user input until the program terminates.</p>537</section>538<section id="important-tk-concepts">539<h3>Important Tk Concepts<a class="headerlink" href="#important-tk-concepts" title="Link to this heading">¶</a></h3>540<p>Even this simple program illustrates the following key Tk concepts:</p>541<dl class="simple">542<dt>widgets</dt><dd><p>A Tkinter user interface is made up of individual <em>widgets</em>. Each widget is543represented as a Python object, instantiated from classes like544<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>545</dd>546<dt>widget hierarchy</dt><dd><p>Widgets are arranged in a <em>hierarchy</em>. The label and button were contained547within a frame, which in turn was contained within the root window. When548creating each <em>child</em> widget, its <em>parent</em> widget is passed as the first549argument to the widget constructor.</p>550</dd>551<dt>configuration options</dt><dd><p>Widgets have <em>configuration options</em>, which modify their appearance and552behavior, such as the text to display in a label or button. Different553classes of widgets will have different sets of options.</p>554</dd>555<dt>geometry management</dt><dd><p>Widgets aren’t automatically added to the user interface when they are556created. A <em>geometry manager</em> like <code class="docutils literal notranslate"><span class="pre">grid</span></code> controls where in the557user interface they are placed.</p>558</dd>559<dt>event loop</dt><dd><p>Tkinter reacts to user input, changes from your program, and even refreshes560the display only when actively running an <em>event loop</em>. If your program561isn’t running the event loop, your user interface won’t update.</p>562</dd>563</dl>564</section>565<section id="understanding-how-tkinter-wraps-tcl-tk">566<h3>Understanding How Tkinter Wraps Tcl/Tk<a class="headerlink" href="#understanding-how-tkinter-wraps-tcl-tk" title="Link to this heading">¶</a></h3>567<p>When your application uses Tkinter’s classes and methods, internally Tkinter568is assembling strings representing Tcl/Tk commands, and executing those569commands in the Tcl interpreter attached to your application’s <code class="xref py py-class docutils literal notranslate"><span class="pre">Tk</span></code>570instance.</p>571<p>Whether it’s trying to navigate reference documentation, trying to find572the right method or option, adapting some existing code, or debugging your573Tkinter application, there are times that it will be useful to understand574what those underlying Tcl/Tk commands look like.</p>575<p>To illustrate, here is the Tcl/Tk equivalent of the main part of the Tkinter576script above.</p>577<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>578<span class="n">grid</span> <span class="o">.</span><span class="n">frm</span>579<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">"Hello World!"</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>580<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">"Quit"</span> <span class="o">-</span><span class="n">command</span> <span class="s2">"destroy ."</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>581</pre></div>582</div>583<p>Tcl’s syntax is similar to many shell languages, where the first word is the584command to be executed, with arguments to that command following it, separated585by spaces. Without getting into too many details, notice the following:</p>586<ul class="simple">587<li><p>The commands used to create widgets (like <code class="docutils literal notranslate"><span class="pre">ttk::frame</span></code>) correspond to588widget classes in Tkinter.</p></li>589<li><p>Tcl widget options (like <code class="docutils literal notranslate"><span class="pre">-text</span></code>) correspond to keyword arguments in590Tkinter.</p></li>591<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>),592whereas Tkinter doesn’t use names but object references.</p></li>593<li><p>A widget’s place in the widget hierarchy is encoded in its (hierarchical)594pathname, which uses a <code class="docutils literal notranslate"><span class="pre">.</span></code> (dot) as a path separator. The pathname for595the root window is just <code class="docutils literal notranslate"><span class="pre">.</span></code> (dot). In Tkinter, the hierarchy is defined596not by pathname but by specifying the parent widget when creating each597child widget.</p></li>598<li><p>Operations which are implemented as separate <em>commands</em> in Tcl (like599<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 widget600objects. As you’ll see shortly, at other times Tcl uses what appear to be601method calls on widget objects, which more closely mirror what is602used in Tkinter.</p></li>603</ul>604</section>605<section id="how-do-i-what-option-does">606<h3>How do I…? What option does…?<a class="headerlink" href="#how-do-i-what-option-does" title="Link to this heading">¶</a></h3>607<p>If you’re not sure how to do something in Tkinter, and you can’t immediately608find it in the tutorial or reference documentation you’re using, there are a609few strategies that can be helpful.</p>610<p>First, remember that the details of how individual widgets work may vary611across different versions of both Tkinter and Tcl/Tk. If you’re searching612documentation, make sure it corresponds to the Python and Tcl/Tk versions613installed on your system.</p>614<p>When searching for how to use an API, it helps to know the exact name of the615class, option, or method that you’re using. Introspection, either in an616interactive 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 what617you need.</p>618<p>To find out what configuration options are available on any widget, call its619<code class="xref py py-meth docutils literal notranslate"><span class="pre">configure()</span></code> method, which returns a dictionary containing a variety of620information about each object, including its default and current values. Use621<code class="xref py py-meth docutils literal notranslate"><span class="pre">keys()</span></code> to get just the names of each option.</p>622<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>623<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>624</pre></div>625</div>626<p>As most widgets have many configuration options in common, it can be useful627to find out which are specific to a particular widget class. Comparing the628list of options to that of a simpler widget, like a frame, is one way to629do that.</p>630<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>631</pre></div>632</div>633<p>Similarly, you can find the available methods for a widget object using the634standard <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 200635common widget methods, so again identifying those specific to a widget class636is helpful.</p>637<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>638<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>639</pre></div>640</div>641</section>642<section id="navigating-the-tcl-tk-reference-manual">643<h3>Navigating the Tcl/Tk Reference Manual<a class="headerlink" href="#navigating-the-tcl-tk-reference-manual" title="Link to this heading">¶</a></h3>644<p>As noted, the official <a class="reference external" href="https://www.tcl.tk/man/tcl8.6/TkCmd/contents.htm">Tk commands</a>645reference manual (man pages) is often the most accurate description of what646specific operations on widgets do. Even when you know the name of the option647or method that you need, you may still have a few places to look.</p>648<p>While all operations in Tkinter are implemented as method calls on widget649objects, you’ve seen that many Tcl/Tk operations appear as commands that650take a widget pathname as its first parameter, followed by optional651parameters, e.g.</p>652<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">destroy</span> <span class="o">.</span>653<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>654</pre></div>655</div>656<p>Others, however, look more like methods called on a widget object (in fact,657when you create a widget in Tcl/Tk, it creates a Tcl command with the name658of the widget pathname, with the first parameter to that command being the659name of a method to call).</p>660<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>661<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">"Goodbye"</span>662</pre></div>663</div>664<p>In the official Tcl/Tk reference documentation, you’ll find most operations665that look like method calls on the man page for a specific widget (e.g.,666you’ll find the <code class="xref py py-meth docutils literal notranslate"><span class="pre">invoke()</span></code> method on the667<a class="reference external" href="https://www.tcl.tk/man/tcl8.6/TkCmd/ttk_button.htm">ttk::button</a>668man page), while functions that take a widget as a parameter often have669their own man page (e.g.,670<a class="reference external" href="https://www.tcl.tk/man/tcl8.6/TkCmd/grid.htm">grid</a>).</p>671<p>You’ll find many common options and methods in the672<a class="reference external" href="https://www.tcl.tk/man/tcl8.6/TkCmd/options.htm">options</a> or673<a class="reference external" href="https://www.tcl.tk/man/tcl8.6/TkCmd/ttk_widget.htm">ttk::widget</a> man674pages, while others are found in the man page for a specific widget class.</p>675<p>You’ll also find that many Tkinter methods have compound names, e.g.,676<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 find677documentation for all of these in the678<a class="reference external" href="https://www.tcl.tk/man/tcl8.6/TkCmd/winfo.htm">winfo</a> man page.</p>679<div class="admonition note">680<p class="admonition-title">Note</p>681<p>Somewhat confusingly, there are also methods on all Tkinter widgets682that don’t actually operate on the widget, but operate at a global683scope, independent of any widget. Examples are methods for accessing684the clipboard or the system bell. (They happen to be implemented as685methods in the base <code class="xref py py-class docutils literal notranslate"><span class="pre">Widget</span></code> class that all Tkinter widgets686inherit from).</p>687</div>688</section>689</section>690<section id="threading-model">691<h2>Threading model<a class="headerlink" href="#threading-model" title="Link to this heading">¶</a></h2>692<p>Python and Tcl/Tk have very different threading models, which <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code>693tries to bridge. If you use threads, you may need to be aware of this.</p>694<p>A Python interpreter may have many threads associated with it. In Tcl, multiple695threads can be created, but each thread has a separate Tcl interpreter instance696associated with it. Threads can also create more than one interpreter instance,697though each interpreter instance can be used only by the one thread that created it.</p>698<p>Each <code class="xref py py-class docutils literal notranslate"><span class="pre">Tk</span></code> object created by <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> contains a Tcl interpreter.699It also keeps track of which thread created that interpreter. Calls to700<code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> can be made from any Python thread. Internally, if a call comes701from 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 event702is posted to the interpreter’s event queue, and when executed, the result is703returned to the calling Python thread.</p>704<p>Tcl/Tk applications are normally event-driven, meaning that after initialization,705the 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.706Because it is single-threaded, event handlers must respond quickly, otherwise they707will block other events from being processed. To avoid this, any long-running708computations should not run in an event handler, but are either broken into smaller709pieces using timers, or run in another thread. This is different from many GUI710toolkits where the GUI runs in a completely separate thread from all application711code including event handlers.</p>712<p>If the Tcl interpreter is not running the event loop and processing events, any713<code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> calls made from threads other than the one running the Tcl714interpreter will fail.</p>715<p>A number of special cases exist:</p>716<ul class="simple">717<li><p>Tcl/Tk libraries can be built so they are not thread-aware. In this case,718<code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> calls the library from the originating Python thread, even719if this is different than the thread that created the Tcl interpreter. A global720lock ensures only one call occurs at a time.</p></li>721<li><p>While <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> 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>722object (with its own interpreter), all interpreters that are part of the same723thread share a common event queue, which gets ugly fast. In practice, don’t create724more 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 create725them in separate threads and ensure you’re running a thread-aware Tcl/Tk build.</p></li>726<li><p>Blocking event handlers are not the only way to prevent the Tcl interpreter from727reentering the event loop. It is even possible to run multiple nested event loops728or abandon the event loop entirely. If you’re doing anything tricky when it comes729to events or threads, be aware of these possibilities.</p></li>730<li><p>There are a few select <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> functions that presently work only when731called from the thread that created the Tcl interpreter.</p></li>732</ul>733</section>734<section id="handy-reference">735<h2>Handy Reference<a class="headerlink" href="#handy-reference" title="Link to this heading">¶</a></h2>736<section id="setting-options">737<span id="tkinter-setting-options"></span><h3>Setting Options<a class="headerlink" href="#setting-options" title="Link to this heading">¶</a></h3>738<p>Options control things like the color and border width of a widget. Options can739be set in three ways:</p>740<dl>741<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">"red"</span><span class="p">,</span> <span class="n">bg</span><span class="o">=</span><span class="s2">"blue"</span><span class="p">)</span>742</pre></div>743</div>744</dd>745<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">"fg"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"red"</span>746<span class="n">fred</span><span class="p">[</span><span class="s2">"bg"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"blue"</span>747</pre></div>748</div>749</dd>750<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">"red"</span><span class="p">,</span> <span class="n">bg</span><span class="o">=</span><span class="s2">"blue"</span><span class="p">)</span>751</pre></div>752</div>753</dd>754</dl>755<p>For a complete explanation of a given option and its behavior, see the Tk man756pages for the widget in question.</p>757<p>Note that the man pages list “STANDARD OPTIONS” and “WIDGET SPECIFIC OPTIONS”758for each widget. The former is a list of options that are common to many759widgets, the latter are the options that are idiosyncratic to that particular760widget. 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> man761page.</p>762<p>No distinction between standard and widget-specific options is made in this763document. Some options don’t apply to some kinds of widgets. Whether a given764widget responds to a particular option depends on the class of the widget;765buttons have a <code class="docutils literal notranslate"><span class="pre">command</span></code> option, labels do not.</p>766<p>The options supported by a given widget are listed in that widget’s man page, or767can be queried at runtime by calling the <code class="xref py py-meth docutils literal notranslate"><span class="pre">config()</span></code> method without768arguments, or by calling the <code class="xref py py-meth docutils literal notranslate"><span class="pre">keys()</span></code> method on that widget. The return769value of these calls is a dictionary whose key is the name of the option as a770string (for example, <code class="docutils literal notranslate"><span class="pre">'relief'</span></code>) and whose values are 5-tuples.</p>771<p>Some options, like <code class="docutils literal notranslate"><span class="pre">bg</span></code> are synonyms for common options with long names772(<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 name773of a shorthand option will return a 2-tuple, not 5-tuple. The 2-tuple passed774back will contain the name of the synonym and the “real” option (such as775<code class="docutils literal notranslate"><span class="pre">('bg',</span> <span class="pre">'background')</span></code>).</p>776<table class="docutils align-default">777<thead>778<tr class="row-odd"><th class="head"><p>Index</p></th>779<th class="head"><p>Meaning</p></th>780<th class="head"><p>Example</p></th>781</tr>782</thead>783<tbody>784<tr class="row-even"><td><p>0</p></td>785<td><p>option name</p></td>786<td><p><code class="docutils literal notranslate"><span class="pre">'relief'</span></code></p></td>787</tr>788<tr class="row-odd"><td><p>1</p></td>789<td><p>option name for database lookup</p></td>790<td><p><code class="docutils literal notranslate"><span class="pre">'relief'</span></code></p></td>791</tr>792<tr class="row-even"><td><p>2</p></td>793<td><p>option class for database794lookup</p></td>795<td><p><code class="docutils literal notranslate"><span class="pre">'Relief'</span></code></p></td>796</tr>797<tr class="row-odd"><td><p>3</p></td>798<td><p>default value</p></td>799<td><p><code class="docutils literal notranslate"><span class="pre">'raised'</span></code></p></td>800</tr>801<tr class="row-even"><td><p>4</p></td>802<td><p>current value</p></td>803<td><p><code class="docutils literal notranslate"><span class="pre">'groove'</span></code></p></td>804</tr>805</tbody>806</table>807<p>Example:</p>808<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </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>809<span class="go">{'relief': ('relief', 'relief', 'Relief', 'raised', 'groove')}</span>810</pre></div>811</div>812<p>Of course, the dictionary printed will include all the options available and813their values. This is meant only as an example.</p>814</section>815<section id="the-packer">816<h3>The Packer<a class="headerlink" href="#the-packer" title="Link to this heading">¶</a></h3>817<p id="index-2">The packer is one of Tk’s geometry-management mechanisms. Geometry managers818are used to specify the relative positioning of widgets within their container.819In contrast to the more cumbersome <em>placer</em> (which is820used less commonly, and we do not cover here), the packer takes qualitative821relationship specification - <em>above</em>, <em>to the left of</em>, <em>filling</em>, etc - and822works everything out to determine the exact placement coordinates for you.</p>823<p>The size of any container widget is determined by the size of the “content widgets”824inside. The packer is used to control where content widgets appear inside the825container into which they are packed. You can pack widgets into frames, and frames826into other frames, in order to achieve the kind of layout you desire.827Additionally, the arrangement is dynamically adjusted to accommodate incremental828changes to the configuration, once it is packed.</p>829<p>Note that widgets do not appear until they have had their geometry specified830with a geometry manager. It’s a common early mistake to leave out the geometry831specification, and then be surprised when the widget is created but nothing832appears. A widget will appear only after it has had, for example, the packer’s833<code class="xref py py-meth docutils literal notranslate"><span class="pre">pack()</span></code> method applied to it.</p>834<p>The pack() method can be called with keyword-option/value pairs that control835where the widget is to appear within its container, and how it is to behave when836the main application window is resized. Here are some examples:</p>837<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 = "top"</span>838<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">"left"</span><span class="p">)</span>839<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>840</pre></div>841</div>842</section>843<section id="packer-options">844<h3>Packer Options<a class="headerlink" href="#packer-options" title="Link to this heading">¶</a></h3>845<p>For more extensive information on the packer and the options that it can take,846see the man pages and page 183 of John Ousterhout’s book.</p>847<dl class="simple">848<dt>anchor</dt><dd><p>Anchor type. Denotes where the packer is to place each content in its parcel.</p>849</dd>850<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>851</dd>852<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>853</dd>854<dt>ipadx and ipady</dt><dd><p>A distance - designating internal padding on each side of the content.</p>855</dd>856<dt>padx and pady</dt><dd><p>A distance - designating external padding on each side of the content.</p>857</dd>858<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>859</dd>860</dl>861</section>862<section id="coupling-widget-variables">863<h3>Coupling Widget Variables<a class="headerlink" href="#coupling-widget-variables" title="Link to this heading">¶</a></h3>864<p>The current-value setting of some widgets (like text entry widgets) can be865connected directly to application variables by using special options. These866options 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>, and867<code class="docutils literal notranslate"><span class="pre">value</span></code>. This connection works both ways: if the variable changes for any868reason, the widget it’s connected to will be updated to reflect the new value.</p>869<p>Unfortunately, in the current implementation of <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> it is not870possible to hand over an arbitrary Python variable to a widget through a871<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 which872this works are variables that are subclassed from a class called Variable,873defined in <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code>.</p>874<p>There are many useful subclasses of Variable already defined:875<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>, and876<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 the877<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>878method. If you follow this protocol, the widget will always track the value of879the variable, with no further intervention on your part.</p>880<p>For example:</p>881<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">tkinter</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">tk</span>882 883<span class="k">class</span><span class="w"> </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>884 <span class="k">def</span><span class="w"> </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>885 <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>886 <span class="bp">self</span><span class="o">.</span><span class="n">pack</span><span class="p">()</span>887 888 <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>889 <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>890 891 <span class="c1"># Create the application variable.</span>892 <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>893 <span class="c1"># Set it to some value.</span>894 <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">"this is a variable"</span><span class="p">)</span>895 <span class="c1"># Tell the entry widget to watch this variable.</span>896 <span class="bp">self</span><span class="o">.</span><span class="n">entrythingy</span><span class="p">[</span><span class="s2">"textvariable"</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">contents</span>897 898 <span class="c1"># Define a callback for when the user hits return.</span>899 <span class="c1"># It prints the current value of the variable.</span>900 <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">'<Key-Return>'</span><span class="p">,</span>901 <span class="bp">self</span><span class="o">.</span><span class="n">print_contents</span><span class="p">)</span>902 903 <span class="k">def</span><span class="w"> </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>904 <span class="nb">print</span><span class="p">(</span><span class="s2">"Hi. The current entry content is:"</span><span class="p">,</span>905 <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>906 907<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>908<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>909<span class="n">myapp</span><span class="o">.</span><span class="n">mainloop</span><span class="p">()</span>910</pre></div>911</div>912</section>913<section id="the-window-manager">914<h3>The Window Manager<a class="headerlink" href="#the-window-manager" title="Link to this heading">¶</a></h3>915<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 window916manager. Options to the <code class="docutils literal notranslate"><span class="pre">wm</span></code> command allow you to control things like titles,917placement, icon bitmaps, and the like. In <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code>, these commands have918been implemented as methods on the <code class="xref py py-class docutils literal notranslate"><span class="pre">Wm</span></code> class. Toplevel widgets are919subclassed 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> methods920directly.</p>921<p>To get at the toplevel window that contains a given widget, you can often just922refer to the widget’s <code class="xref py py-attr docutils literal notranslate"><span class="pre">master</span></code>. Of course if the widget has been packed inside of923a frame, the <code class="xref py py-attr docutils literal notranslate"><span class="pre">master</span></code> won’t represent a toplevel window. To get at the toplevel924window 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.925This method begins with an underscore to denote the fact that this function is926part of the implementation, and not an interface to Tk functionality.</p>927<p>Here are some examples of typical usage:</p>928<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">tkinter</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">tk</span>929 930<span class="k">class</span><span class="w"> </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>931 <span class="k">def</span><span class="w"> </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>932 <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>933 <span class="bp">self</span><span class="o">.</span><span class="n">pack</span><span class="p">()</span>934 935<span class="c1"># create the application</span>936<span class="n">myapp</span> <span class="o">=</span> <span class="n">App</span><span class="p">()</span>937 938<span class="c1">#</span>939<span class="c1"># here are method calls to the window manager class</span>940<span class="c1">#</span>941<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">"My Do-Nothing Application"</span><span class="p">)</span>942<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>943 944<span class="c1"># start the program</span>945<span class="n">myapp</span><span class="o">.</span><span class="n">mainloop</span><span class="p">()</span>946</pre></div>947</div>948</section>949<section id="tk-option-data-types">950<h3>Tk Option Data Types<a class="headerlink" href="#tk-option-data-types" title="Link to this heading">¶</a></h3>951<dl id="index-4">952<dt>anchor</dt><dd><p>Legal values are points of the compass: <code class="docutils literal notranslate"><span class="pre">"n"</span></code>, <code class="docutils literal notranslate"><span class="pre">"ne"</span></code>, <code class="docutils literal notranslate"><span class="pre">"e"</span></code>, <code class="docutils literal notranslate"><span class="pre">"se"</span></code>,953<code class="docutils literal notranslate"><span class="pre">"s"</span></code>, <code class="docutils literal notranslate"><span class="pre">"sw"</span></code>, <code class="docutils literal notranslate"><span class="pre">"w"</span></code>, <code class="docutils literal notranslate"><span class="pre">"nw"</span></code>, and also <code class="docutils literal notranslate"><span class="pre">"center"</span></code>.</p>954</dd>955<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>,956<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>,957<code class="docutils literal notranslate"><span class="pre">'warning'</span></code>. To specify an X bitmap filename, give the full path to the file,958preceded with an <code class="docutils literal notranslate"><span class="pre">@</span></code>, as in <code class="docutils literal notranslate"><span class="pre">"@/usr/contrib/bitmap/gumby.bit"</span></code>.</p>959</dd>960<dt>boolean</dt><dd><p>You can pass integers 0 or 1 or the strings <code class="docutils literal notranslate"><span class="pre">"yes"</span></code> or <code class="docutils literal notranslate"><span class="pre">"no"</span></code>.</p>961</dd>962<dt>callback</dt><dd><p>This is any Python function that takes no arguments. For example:</p>963<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">print_it</span><span class="p">():</span>964 <span class="nb">print</span><span class="p">(</span><span class="s2">"hi there"</span><span class="p">)</span>965<span class="n">fred</span><span class="p">[</span><span class="s2">"command"</span><span class="p">]</span> <span class="o">=</span> <span class="n">print_it</span>966</pre></div>967</div>968</dd>969<dt>color</dt><dd><p>Colors can be given as the names of X colors in the rgb.txt file, or as strings970representing RGB values in 4 bit: <code class="docutils literal notranslate"><span class="pre">"#RGB"</span></code>, 8 bit: <code class="docutils literal notranslate"><span class="pre">"#RRGGBB"</span></code>, 12 bit:971<code class="docutils literal notranslate"><span class="pre">"#RRRGGGBBB"</span></code>, or 16 bit: <code class="docutils literal notranslate"><span class="pre">"#RRRRGGGGBBBB"</span></code> ranges, where R,G,B here972represent any legal hex digit. See page 160 of Ousterhout’s book for details.</p>973</dd>974<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 the975<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 the976string <code class="docutils literal notranslate"><span class="pre">"hand2"</span></code>. You can also specify a bitmap and mask file of your own.977See page 179 of Ousterhout’s book.</p>978</dd>979<dt>distance</dt><dd><p>Screen distances can be specified in either pixels or absolute distances.980Pixels are given as numbers and absolute distances as strings, with the trailing981character 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> for982millimetres, <code class="docutils literal notranslate"><span class="pre">p</span></code> for printer’s points. For example, 3.5 inches is expressed983as <code class="docutils literal notranslate"><span class="pre">"3.5i"</span></code>.</p>984</dd>985<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 with986positive numbers are measured in points; sizes with negative numbers are987measured in pixels.</p>988</dd>989<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 are990measured in pixels for most widgets (in characters for widgets displaying text).991For example: <code class="docutils literal notranslate"><span class="pre">fred["geometry"]</span> <span class="pre">=</span> <span class="pre">"200x100"</span></code>.</p>992</dd>993<dt>justify</dt><dd><p>Legal values are the strings: <code class="docutils literal notranslate"><span class="pre">"left"</span></code>, <code class="docutils literal notranslate"><span class="pre">"center"</span></code>, and <code class="docutils literal notranslate"><span class="pre">"right"</span></code>.</p>994</dd>995<dt>region</dt><dd><p>This is a string with four space-delimited elements, each of which is a legal996distance (see above). For example: <code class="docutils literal notranslate"><span class="pre">"2</span> <span class="pre">3</span> <span class="pre">4</span> <span class="pre">5"</span></code> and <code class="docutils literal notranslate"><span class="pre">"3i</span> <span class="pre">2i</span> <span class="pre">4.5i</span> <span class="pre">2i"</span></code> and997<code class="docutils literal notranslate"><span class="pre">"3c</span> <span class="pre">2c</span> <span class="pre">4c</span> <span class="pre">10.43c"</span></code> are all legal regions.</p>998</dd>999<dt>relief</dt><dd><p>Determines what the border style of a widget will be. Legal values are:1000<code class="docutils literal notranslate"><span class="pre">"raised"</span></code>, <code class="docutils literal notranslate"><span class="pre">"sunken"</span></code>, <code class="docutils literal notranslate"><span class="pre">"flat"</span></code>, <code class="docutils literal notranslate"><span class="pre">"groove"</span></code>, and <code class="docutils literal notranslate"><span class="pre">"ridge"</span></code>.</p>1001</dd>1002<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 can1003be any widget method that takes a single argument.</p>1004</dd>1005<dt>wrap</dt><dd><p>Must be one of: <code class="docutils literal notranslate"><span class="pre">"none"</span></code>, <code class="docutils literal notranslate"><span class="pre">"char"</span></code>, or <code class="docutils literal notranslate"><span class="pre">"word"</span></code>.</p>1006</dd>1007</dl>1008</section>1009<section id="bindings-and-events">1010<span id="id2"></span><h3>Bindings and Events<a class="headerlink" href="#bindings-and-events" title="Link to this heading">¶</a></h3>1011<p id="index-5">The bind method from the widget command allows you to watch for certain events1012and to have a callback function trigger when that event type occurs. The form1013of the bind method is:</p>1014<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </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">''</span><span class="p">):</span>1015</pre></div>1016</div>1017<p>where:</p>1018<dl class="simple">1019<dt>sequence</dt><dd><p>is a string that denotes the target kind of event. (See the1020<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,1021<cite>Tcl and the Tk Toolkit (2nd edition)</cite>, for details).</p>1022</dd>1023<dt>func</dt><dd><p>is a Python function, taking one argument, to be invoked when the event occurs.1024An Event instance will be passed as the argument. (Functions deployed this way1025are commonly known as <em>callbacks</em>.)</p>1026</dd>1027<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 that1028this binding is to replace any other bindings that this event is associated1029with. Passing a <code class="docutils literal notranslate"><span class="pre">'+'</span></code> means that this function is to be added to the list1030of functions bound to this event type.</p>1031</dd>1032</dl>1033<p>For example:</p>1034<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </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>1035 <span class="n">event</span><span class="o">.</span><span class="n">widget</span><span class="p">[</span><span class="s2">"activeforeground"</span><span class="p">]</span> <span class="o">=</span> <span class="s2">"red"</span>1036 1037<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">"<Enter>"</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">turn_red</span><span class="p">)</span>1038</pre></div>1039</div>1040<p>Notice how the widget field of the event is being accessed in the1041<code class="docutils literal notranslate"><span class="pre">turn_red()</span></code> callback. This field contains the widget that caught the X1042event. The following table lists the other event fields you can access, and how1043they are denoted in Tk, which can be useful when referring to the Tk man pages.</p>1044<table class="docutils align-default">1045<thead>1046<tr class="row-odd"><th class="head"><p>Tk</p></th>1047<th class="head"><p>Tkinter Event Field</p></th>1048<th class="head"><p>Tk</p></th>1049<th class="head"><p>Tkinter Event Field</p></th>1050</tr>1051</thead>1052<tbody>1053<tr class="row-even"><td><p>%f</p></td>1054<td><p>focus</p></td>1055<td><p>%A</p></td>1056<td><p>char</p></td>1057</tr>1058<tr class="row-odd"><td><p>%h</p></td>1059<td><p>height</p></td>1060<td><p>%E</p></td>1061<td><p>send_event</p></td>1062</tr>1063<tr class="row-even"><td><p>%k</p></td>1064<td><p>keycode</p></td>1065<td><p>%K</p></td>1066<td><p>keysym</p></td>1067</tr>1068<tr class="row-odd"><td><p>%s</p></td>1069<td><p>state</p></td>1070<td><p>%N</p></td>1071<td><p>keysym_num</p></td>1072</tr>1073<tr class="row-even"><td><p>%t</p></td>1074<td><p>time</p></td>1075<td><p>%T</p></td>1076<td><p>type</p></td>1077</tr>1078<tr class="row-odd"><td><p>%w</p></td>1079<td><p>width</p></td>1080<td><p>%W</p></td>1081<td><p>widget</p></td>1082</tr>1083<tr class="row-even"><td><p>%x</p></td>1084<td><p>x</p></td>1085<td><p>%X</p></td>1086<td><p>x_root</p></td>1087</tr>1088<tr class="row-odd"><td><p>%y</p></td>1089<td><p>y</p></td>1090<td><p>%Y</p></td>1091<td><p>y_root</p></td>1092</tr>1093</tbody>1094</table>1095</section>1096<section id="the-index-parameter">1097<h3>The index Parameter<a class="headerlink" href="#the-index-parameter" title="Link to this heading">¶</a></h3>1098<p>A number of widgets require “index” parameters to be passed. These are used to1099point at a specific place in a Text widget, or to particular characters in an1100Entry widget, or to particular menu items in a Menu widget.</p>1101<dl class="simple">1102<dt>Entry widget indexes (index, view index, etc.)</dt><dd><p>Entry widgets have options that refer to character positions in the text being1103displayed. You can use these <code class="xref py py-mod docutils literal notranslate"><span class="pre">tkinter</span></code> functions to access these special1104points in text widgets:</p>1105</dd>1106<dt>Text widget indexes</dt><dd><p>The index notation for Text widgets is very rich and is best described in the Tk1107man pages.</p>1108</dd>1109<dt>Menu indexes (menu.invoke(), menu.entryconfig(), etc.)</dt><dd><p>Some options and methods for menus manipulate specific menu entries. Anytime a1110menu index is needed for an option or a parameter, you may pass in:</p>1111<ul class="simple">1112<li><p>an integer which refers to the numeric position of the entry in the widget,1113counted from the top, starting with 0;</p></li>1114<li><p>the string <code class="docutils literal notranslate"><span class="pre">"active"</span></code>, which refers to the menu position that is currently1115under the cursor;</p></li>1116<li><p>the string <code class="docutils literal notranslate"><span class="pre">"last"</span></code> which refers to the last menu item;</p></li>1117<li><p>An integer preceded by <code class="docutils literal notranslate"><span class="pre">@</span></code>, as in <code class="docutils literal notranslate"><span class="pre">@6</span></code>, where the integer is interpreted1118as a y pixel coordinate in the menu’s coordinate system;</p></li>1119<li><p>the string <code class="docutils literal notranslate"><span class="pre">"none"</span></code>, which indicates no menu entry at all, most often used1120with menu.activate() to deactivate all entries, and finally,</p></li>1121<li><p>a text string that is pattern matched against the label of the menu entry, as1122scanned from the top of the menu to the bottom. Note that this index type is1123considered after all the others, which means that matches for menu items1124labelled <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 above1125literals, instead.</p></li>1126</ul>1127</dd>1128</dl>1129</section>1130<section id="images">1131<h3>Images<a class="headerlink" href="#images" title="Link to this heading">¶</a></h3>1132<p>Images of different formats can be created through the corresponding subclass1133of <code class="xref py py-class docutils literal notranslate"><span class="pre">tkinter.Image</span></code>:</p>1134<ul class="simple">1135<li><p><code class="xref py py-class docutils literal notranslate"><span class="pre">BitmapImage</span></code> for images in XBM format.</p></li>1136<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 latter1137is supported starting with Tk 8.6.</p></li>1138</ul>1139<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>1140option (other options are available as well).</p>1141<div class="versionchanged">1142<p><span class="versionmodified changed">Changed in version 3.13: </span>Added the <code class="xref py py-class docutils literal notranslate"><span class="pre">PhotoImage</span></code> method <code class="xref py py-meth docutils literal notranslate"><span class="pre">copy_replace()</span></code> to copy a region1143from one image to other image, possibly with pixel zooming and/or1144subsampling.1145Add <em>from_coords</em> parameter to <code class="xref py py-class docutils literal notranslate"><span class="pre">PhotoImage</span></code> methods <code class="xref py py-meth docutils literal notranslate"><span class="pre">copy()</span></code>,1146<code class="xref py py-meth docutils literal notranslate"><span class="pre">zoom()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">subsample()</span></code>.1147Add <em>zoom</em> and <em>subsample</em> parameters to <code class="xref py py-class docutils literal notranslate"><span class="pre">PhotoImage</span></code> method1148<code class="xref py py-meth docutils literal notranslate"><span class="pre">copy()</span></code>.</p>1149</div>1150<p>The image object can then be used wherever an <code class="docutils literal notranslate"><span class="pre">image</span></code> option is supported by1151some widget (e.g. labels, buttons, menus). In these cases, Tk will not keep a1152reference to the image. When the last Python reference to the image object is1153deleted, the image data is deleted as well, and Tk will display an empty box1154wherever the image was used.</p>1155<div class="admonition seealso">1156<p class="admonition-title">See also</p>1157<p>The <a class="reference external" href="https://python-pillow.org/">Pillow</a> package adds support for1158formats such as BMP, JPEG, TIFF, and WebP, among others.</p>1159</div>1160</section>1161</section>1162<section id="file-handlers">1163<span id="tkinter-file-handlers"></span><h2>File Handlers<a class="headerlink" href="#file-handlers" title="Link to this heading">¶</a></h2>1164<p>Tk allows you to register and unregister a callback function which will be1165called from the Tk mainloop when I/O is possible on a file descriptor.1166Only one handler may be registered per file descriptor. Example code:</p>1167<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">tkinter</span>1168<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>1169<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>1170<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>1171<span class="o">...</span>1172<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>1173</pre></div>1174</div>1175<p>This feature is not available on Windows.</p>1176<p>Since you don’t know how many bytes are available for reading, you may not1177want 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>1178<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,1179since these will insist on reading a predefined number of bytes.1180For 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> or1181<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,1182use raw reads or <code class="docutils literal notranslate"><span class="pre">os.read(file.fileno(),</span> <span class="pre">maxbytecount)</span></code>.</p>1183<dl class="py method">1184<dt class="sig sig-object py" id="tkinter.Widget.tk.createfilehandler">1185<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>1186<dd><p>Registers the file handler callback function <em>func</em>. The <em>file</em> argument1187may 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 as1188a file or socket object), or an integer file descriptor. The <em>mask</em>1189argument is an ORed combination of any of the three constants below.1190The callback is called as follows:</p>1191<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>1192</pre></div>1193</div>1194</dd></dl>1195 1196<dl class="py method">1197<dt class="sig sig-object py" id="tkinter.Widget.tk.deletefilehandler">1198<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>1199<dd><p>Unregisters a file handler.</p>1200</dd></dl>