Skip to content

Commit 98758bc

Browse files
authored
bpo-31421: Document how IDLE runs tkinter programs. (python#3513)
IDLE calls tcl/tk update in the background in order to make live interaction and experimentatin with tkinter applications much easier.
1 parent adb4cd2 commit 98758bc

File tree

3 files changed

+63
-16
lines changed

3 files changed

+63
-16
lines changed

Doc/library/idle.rst

+29-6
Original file line numberDiff line numberDiff line change
@@ -599,15 +599,15 @@ starting from a console (``python -m idlelib)`` and see if a message appears.
599599
IDLE-console differences
600600
^^^^^^^^^^^^^^^^^^^^^^^^
601601

602-
As much as possible, the result of executing Python code with IDLE is the
603-
same as executing the same code in a console window. However, the different
604-
interface and operation occasionally affect visible results. For instance,
605-
``sys.modules`` starts with more entries.
602+
With rare exceptions, the result of executing Python code with IDLE is
603+
intended to be the same as executing the same code in a console window.
604+
However, the different interface and operation occasionally affect
605+
visible results. For instance, ``sys.modules`` starts with more entries.
606606

607607
IDLE also replaces ``sys.stdin``, ``sys.stdout``, and ``sys.stderr`` with
608608
objects that get input from and send output to the Shell window.
609-
When this window has the focus, it controls the keyboard and screen.
610-
This is normally transparent, but functions that directly access the keyboard
609+
When Shell has the focus, it controls the keyboard and screen. This is
610+
normally transparent, but functions that directly access the keyboard
611611
and screen will not work. If ``sys`` is reset with ``importlib.reload(sys)``,
612612
IDLE's changes are lost and things like ``input``, ``raw_input``, and
613613
``print`` will not work correctly.
@@ -617,6 +617,29 @@ Some consoles only work with a single physical line at a time. IDLE uses
617617
``exec`` to run each statement. As a result, ``'__builtins__'`` is always
618618
defined for each statement.
619619

620+
Developing tkinter applications
621+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
622+
623+
IDLE is intentionally different from standard Python in order to
624+
facilitate development of tkinter programs. Enter ``import tkinter as tk;
625+
root = tk.Tk()`` in standard Python and nothing appears. Enter the same
626+
in IDLE and a tk window appears. In standard Python, one must also enter
627+
``root.update()`` to see the window. IDLE does the equivalent in the
628+
background, about 20 times a second, which is about every 50 milleseconds.
629+
Next enter ``b = tk.Button(root, text='button'); b.pack()``. Again,
630+
nothing visibly changes in standard Python until one enters ``root.update()``.
631+
632+
Most tkinter programs run ``root.mainloop()``, which usually does not
633+
return until the tk app is destroyed. If the program is run with
634+
``python -i`` or from an IDLE editor, a ``>>>`` shell prompt does not
635+
appear until ``mainloop()`` returns, at which time there is nothing left
636+
to interact with.
637+
638+
When running a tkinter program from an IDLE editor, one can comment out
639+
the mainloop call. One then gets a shell prompt immediately and can
640+
interact with the live application. One just has to remember to
641+
re-enable the mainloop call when running in standard Python.
642+
620643
Running without a subprocess
621644
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
622645

Lib/idlelib/help.html

+30-10
Original file line numberDiff line numberDiff line change
@@ -573,14 +573,14 @@ <h3>25.5.3.2. Startup failure<a class="headerlink" href="#startup-failure" title
573573
</div>
574574
<div class="section" id="idle-console-differences">
575575
<h3>25.5.3.3. IDLE-console differences<a class="headerlink" href="#idle-console-differences" title="Permalink to this headline"></a></h3>
576-
<p>As much as possible, the result of executing Python code with IDLE is the
577-
same as executing the same code in a console window. However, the different
578-
interface and operation occasionally affect visible results. For instance,
579-
<code class="docutils literal"><span class="pre">sys.modules</span></code> starts with more entries.</p>
576+
<p>With rare exceptions, the result of executing Python code with IDLE is
577+
intended to be the same as executing the same code in a console window.
578+
However, the different interface and operation occasionally affect
579+
visible results. For instance, <code class="docutils literal"><span class="pre">sys.modules</span></code> starts with more entries.</p>
580580
<p>IDLE also replaces <code class="docutils literal"><span class="pre">sys.stdin</span></code>, <code class="docutils literal"><span class="pre">sys.stdout</span></code>, and <code class="docutils literal"><span class="pre">sys.stderr</span></code> with
581581
objects that get input from and send output to the Shell window.
582-
When this window has the focus, it controls the keyboard and screen.
583-
This is normally transparent, but functions that directly access the keyboard
582+
When Shell has the focus, it controls the keyboard and screen. This is
583+
normally transparent, but functions that directly access the keyboard
584584
and screen will not work. If <code class="docutils literal"><span class="pre">sys</span></code> is reset with <code class="docutils literal"><span class="pre">importlib.reload(sys)</span></code>,
585585
IDLE&#8217;s changes are lost and things like <code class="docutils literal"><span class="pre">input</span></code>, <code class="docutils literal"><span class="pre">raw_input</span></code>, and
586586
<code class="docutils literal"><span class="pre">print</span></code> will not work correctly.</p>
@@ -589,8 +589,28 @@ <h3>25.5.3.3. IDLE-console differences<a class="headerlink" href="#idle-console-
589589
<code class="docutils literal"><span class="pre">exec</span></code> to run each statement. As a result, <code class="docutils literal"><span class="pre">'__builtins__'</span></code> is always
590590
defined for each statement.</p>
591591
</div>
592+
<div class="section" id="developing-tkinter-applications">
593+
<h3>25.5.3.4. Developing tkinter applications<a class="headerlink" href="#developing-tkinter-applications" title="Permalink to this headline"></a></h3>
594+
<p>IDLE is intentionally different from standard Python in order to
595+
facilitate development of tkinter programs. Enter <code class="docutils literal"><span class="pre">import</span> <span class="pre">tkinter</span> <span class="pre">as</span> <span class="pre">tk;</span>
596+
<span class="pre">root</span> <span class="pre">=</span> <span class="pre">tk.Tk()</span></code> in standard Python and nothing appears. Enter the same
597+
in IDLE and a tk window appears. In standard Python, one must also enter
598+
<code class="docutils literal"><span class="pre">root.update()</span></code> to see the window. IDLE does the equivalent in the
599+
background, about 20 times a second, which is about every 50 milleseconds.
600+
Next enter <code class="docutils literal"><span class="pre">b</span> <span class="pre">=</span> <span class="pre">tk.Button(root,</span> <span class="pre">text='button');</span> <span class="pre">b.pack()</span></code>. Again,
601+
nothing visibly changes in standard Python until one enters <code class="docutils literal"><span class="pre">root.update()</span></code>.</p>
602+
<p>Most tkinter programs run <code class="docutils literal"><span class="pre">root.mainloop()</span></code>, which usually does not
603+
return until the tk app is destroyed. If the program is run with
604+
<code class="docutils literal"><span class="pre">python</span> <span class="pre">-i</span></code> or from an IDLE editor, a <code class="docutils literal"><span class="pre">&gt;&gt;&gt;</span></code> shell prompt does not
605+
appear until <code class="docutils literal"><span class="pre">mainloop()</span></code> returns, at which time there is nothing left
606+
to interact with.</p>
607+
<p>When running a tkinter program from an IDLE editor, one can comment out
608+
the mainloop call. One then gets a shell prompt immediately and can
609+
interact with the live application. One just has to remember to
610+
re-enable the mainloop call when running in standard Python.</p>
611+
</div>
592612
<div class="section" id="running-without-a-subprocess">
593-
<h3>25.5.3.4. Running without a subprocess<a class="headerlink" href="#running-without-a-subprocess" title="Permalink to this headline"></a></h3>
613+
<h3>25.5.3.5. Running without a subprocess<a class="headerlink" href="#running-without-a-subprocess" title="Permalink to this headline"></a></h3>
594614
<p>By default, IDLE executes user code in a separate subprocess via a socket,
595615
which uses the internal loopback interface. This connection is not
596616
externally visible and no data is sent to or received from the Internet.
@@ -638,8 +658,7 @@ <h3>25.5.4.3. Extensions<a class="headerlink" href="#extensions" title="Permalin
638658
changed with the Extensions tab of the preferences dialog. See the
639659
beginning of config-extensions.def in the idlelib directory for further
640660
information. The only current default extension is zzdummy, an example
641-
also used for testing.
642-
</p>
661+
also used for testing.</p>
643662
</div>
644663
</div>
645664
</div>
@@ -678,7 +697,8 @@ <h3><a href="../contents.html">Table Of Contents</a></h3>
678697
<li><a class="reference internal" href="#command-line-usage">25.5.3.1. Command line usage</a></li>
679698
<li><a class="reference internal" href="#startup-failure">25.5.3.2. Startup failure</a></li>
680699
<li><a class="reference internal" href="#idle-console-differences">25.5.3.3. IDLE-console differences</a></li>
681-
<li><a class="reference internal" href="#running-without-a-subprocess">25.5.3.4. Running without a subprocess</a></li>
700+
<li><a class="reference internal" href="#developing-tkinter-applications">25.5.3.4. Developing tkinter applications</a></li>
701+
<li><a class="reference internal" href="#running-without-a-subprocess">25.5.3.5. Running without a subprocess</a></li>
682702
</ul>
683703
</li>
684704
<li><a class="reference internal" href="#help-and-preferences">25.5.4. Help and preferences</a><ul>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Document how IDLE runs tkinter programs. IDLE calls tcl/tk update in the
2+
background in order to make live
3+
4+
interaction and experimentatin with tkinter applications much easier.

0 commit comments

Comments
 (0)