From 98758bc67fb39b74bab368bef8ff3b34554c77c8 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Tue, 12 Sep 2017 09:05:16 -0400 Subject: [PATCH] bpo-31421: Document how IDLE runs tkinter programs. (#3513) IDLE calls tcl/tk update in the background in order to make live interaction and experimentatin with tkinter applications much easier. --- Doc/library/idle.rst | 35 +++++++++++++--- Lib/idlelib/help.html | 40 ++++++++++++++----- .../2017-09-12-08-38-27.bpo-31421.mYfQNq.rst | 4 ++ 3 files changed, 63 insertions(+), 16 deletions(-) create mode 100644 Misc/NEWS.d/next/IDLE/2017-09-12-08-38-27.bpo-31421.mYfQNq.rst diff --git a/Doc/library/idle.rst b/Doc/library/idle.rst index 38ab4f114b..0faeb6df93 100644 --- a/Doc/library/idle.rst +++ b/Doc/library/idle.rst @@ -599,15 +599,15 @@ starting from a console (``python -m idlelib)`` and see if a message appears. IDLE-console differences ^^^^^^^^^^^^^^^^^^^^^^^^ -As much as possible, the result of executing Python code with IDLE is the -same as executing the same code in a console window. However, the different -interface and operation occasionally affect visible results. For instance, -``sys.modules`` starts with more entries. +With rare exceptions, the result of executing Python code with IDLE is +intended to be the same as executing the same code in a console window. +However, the different interface and operation occasionally affect +visible results. For instance, ``sys.modules`` starts with more entries. IDLE also replaces ``sys.stdin``, ``sys.stdout``, and ``sys.stderr`` with objects that get input from and send output to the Shell window. -When this window has the focus, it controls the keyboard and screen. -This is normally transparent, but functions that directly access the keyboard +When Shell has the focus, it controls the keyboard and screen. This is +normally transparent, but functions that directly access the keyboard and screen will not work. If ``sys`` is reset with ``importlib.reload(sys)``, IDLE's changes are lost and things like ``input``, ``raw_input``, and ``print`` will not work correctly. @@ -617,6 +617,29 @@ Some consoles only work with a single physical line at a time. IDLE uses ``exec`` to run each statement. As a result, ``'__builtins__'`` is always defined for each statement. +Developing tkinter applications +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +IDLE is intentionally different from standard Python in order to +facilitate development of tkinter programs. Enter ``import tkinter as tk; +root = tk.Tk()`` in standard Python and nothing appears. Enter the same +in IDLE and a tk window appears. In standard Python, one must also enter +``root.update()`` to see the window. IDLE does the equivalent in the +background, about 20 times a second, which is about every 50 milleseconds. +Next enter ``b = tk.Button(root, text='button'); b.pack()``. Again, +nothing visibly changes in standard Python until one enters ``root.update()``. + +Most tkinter programs run ``root.mainloop()``, which usually does not +return until the tk app is destroyed. If the program is run with +``python -i`` or from an IDLE editor, a ``>>>`` shell prompt does not +appear until ``mainloop()`` returns, at which time there is nothing left +to interact with. + +When running a tkinter program from an IDLE editor, one can comment out +the mainloop call. One then gets a shell prompt immediately and can +interact with the live application. One just has to remember to +re-enable the mainloop call when running in standard Python. + Running without a subprocess ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/Lib/idlelib/help.html b/Lib/idlelib/help.html index 07fcac5fb3..9005157dd9 100644 --- a/Lib/idlelib/help.html +++ b/Lib/idlelib/help.html @@ -573,14 +573,14 @@ starting from a console (python

25.5.3.3. IDLE-console differences¶

-

As much as possible, the result of executing Python code with IDLE is the -same as executing the same code in a console window. However, the different -interface and operation occasionally affect visible results. For instance, -sys.modules starts with more entries.

+

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

IDLE also replaces sys.stdin, sys.stdout, and sys.stderr with objects that get input from and send output to the Shell window. -When this window has the focus, it controls the keyboard and screen. -This is normally transparent, but functions that directly access the keyboard +When Shell has the focus, it controls the keyboard and screen. This is +normally transparent, but functions that directly access the keyboard and screen will not work. If sys is reset with importlib.reload(sys), IDLE’s changes are lost and things like input, raw_input, and print will not work correctly.

@@ -589,8 +589,28 @@ Some consoles only work with a single physical line at a time. IDLE uses exec to run each statement. As a result, '__builtins__' is always defined for each statement.

+
+

25.5.3.4. Developing tkinter applications¶

+

IDLE is intentionally different from standard Python in order to +facilitate development of tkinter programs. Enter import tkinter as tk; +root = tk.Tk() in standard Python and nothing appears. Enter the same +in IDLE and a tk window appears. In standard Python, one must also enter +root.update() to see the window. IDLE does the equivalent in the +background, about 20 times a second, which is about every 50 milleseconds. +Next enter b = tk.Button(root, text='button'); b.pack(). Again, +nothing visibly changes in standard Python until one enters root.update().

+

Most tkinter programs run root.mainloop(), which usually does not +return until the tk app is destroyed. If the program is run with +python -i or from an IDLE editor, a >>> shell prompt does not +appear until mainloop() returns, at which time there is nothing left +to interact with.

+

When running a tkinter program from an IDLE editor, one can comment out +the mainloop call. One then gets a shell prompt immediately and can +interact with the live application. One just has to remember to +re-enable the mainloop call when running in standard Python.

+
-

25.5.3.4. Running without a subprocess¶

+

25.5.3.5. Running without a subprocess¶

By default, IDLE executes user code in a separate subprocess via a socket, which uses the internal loopback interface. This connection is not externally visible and no data is sent to or received from the Internet. @@ -638,8 +658,7 @@ custom key set in the Configure IDLE dialog under the keys tab.

changed with the Extensions tab of the preferences dialog. See the beginning of config-extensions.def in the idlelib directory for further information. The only current default extension is zzdummy, an example -also used for testing. -

+also used for testing.

@@ -678,7 +697,8 @@ also used for testing.
  • 25.5.3.1. Command line usage
  • 25.5.3.2. Startup failure
  • 25.5.3.3. IDLE-console differences
  • -
  • 25.5.3.4. Running without a subprocess
  • +
  • 25.5.3.4. Developing tkinter applications
  • +
  • 25.5.3.5. Running without a subprocess
  • 25.5.4. Help and preferences
      diff --git a/Misc/NEWS.d/next/IDLE/2017-09-12-08-38-27.bpo-31421.mYfQNq.rst b/Misc/NEWS.d/next/IDLE/2017-09-12-08-38-27.bpo-31421.mYfQNq.rst new file mode 100644 index 0000000000..f3e4ab2232 --- /dev/null +++ b/Misc/NEWS.d/next/IDLE/2017-09-12-08-38-27.bpo-31421.mYfQNq.rst @@ -0,0 +1,4 @@ +Document how IDLE runs tkinter programs. IDLE calls tcl/tk update in the +background in order to make live + +interaction and experimentatin with tkinter applications much easier. -- 2.40.0