From b900632390260bcd780578433e3dd796dc2be0b5 Mon Sep 17 00:00:00 2001 From: williamnavaraj Date: Mon, 3 Jan 2022 12:26:32 +0000 Subject: [PATCH 1/5] Reinit class variable _RUNNING Initialising the class variable at the end of the object initialisation method. This will help to run this library smoothly in use cases such as in notebooks or GUI or other cell based or multiple turtle object instance execution. Otherwise it will raise Terminator error every second time. Will solve problems highlighted repeatedly in these stack overflow posts (With 1000s of views, current suggestions are some non-elegant workarounds such as importlib.reload(turtle) or or try-except and initializing again in the except as the _RUNNING becomes false at the end of last run or manually setting the _RUNNING flag). Logical to turn this to True after the initialization rather than during terminator exception. Change tested extensively and no issues found in any of the turtle-demo examples, notebooks or usual self-contained single instance execution. https://stackoverflow.com/questions/45534458/python-turtle-terminator-even-after-using-exitonclick https://stackoverflow.com/questions/34956937/weird-terminator-error-when-running-python-3-turtle-in-os-x https://stackoverflow.com/questions/70252586/turtle-module-for-langtons-ant-in-python-giving-me-raise-terminator-turtle-ter https://stackoverflow.com/questions/58078376/a-function-i-created-can-only-be-called-once-second-time-it-shows-an-error https://stackoverflow.com/questions/54198108/how-can-i-close-a-window-after-using-turtle https://stackoverflow.com/questions/67734319/running-turtle-multiple-times-but-receiving-a-terminator-error-after-the-first-r https://stackoverflow.com/questions/43294665/turtle-done-not-working-in-spyder https://stackoverflow.com/questions/50438762/python-turtle-window-crashes-every-2nd-time-running https://stackoverflow.com/questions/41644324/how-to-close-python-turtle-properly https://stackoverflow.com/questions/41548813/using-turtle-module-exitonclick/70552164 https://stackoverflow.com/questions/66750634/why-do-i-get-a-terminator-error-when-i-run-this-program https://stackoverflow.com/questions/65546812/python-turtle-terminator https://stackoverflow.com/questions/61963565/why-do-i-keep-getting-a-terminator-error-for-python-turtle --- Lib/turtle.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Lib/turtle.py b/Lib/turtle.py index af2c7448a0af17f..914a6d2719dd5db 100644 --- a/Lib/turtle.py +++ b/Lib/turtle.py @@ -1001,7 +1001,13 @@ def __init__(self, cv, mode=_CFG["mode"], rootwindow = cv.winfo_toplevel() rootwindow.wm_attributes(topmost=True) rootwindow.wm_attributes(topmost=False) - + + # The class variable _RUNNING being persistent once this module is imported, this should be made True when the next object is created. + # This is particularly needed for running in notebooks or GUI application but does not affect the usual self-contained single instance execution + # nor any of the turtle-demos + + TurtleScreen._RUNNING = True + def clear(self): """Delete all drawings and all turtles from the TurtleScreen. From 341e1735c69e672d8556c34a04240a7e63e7d241 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 3 Jan 2022 12:50:43 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Tools-Demos/2022-01-03-12-50-42.bpo-26571.2NjFW_.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Tools-Demos/2022-01-03-12-50-42.bpo-26571.2NjFW_.rst diff --git a/Misc/NEWS.d/next/Tools-Demos/2022-01-03-12-50-42.bpo-26571.2NjFW_.rst b/Misc/NEWS.d/next/Tools-Demos/2022-01-03-12-50-42.bpo-26571.2NjFW_.rst new file mode 100644 index 000000000000000..f31aacd7a20556a --- /dev/null +++ b/Misc/NEWS.d/next/Tools-Demos/2022-01-03-12-50-42.bpo-26571.2NjFW_.rst @@ -0,0 +1 @@ +Enables turtle to be instantiated multiple times. Fixes error which causes it to through error every other time when running in notebooks or shell or other GUI based application involving close-and-reinstantiate operations of turtle screen. \ No newline at end of file From 6487268fbd462e2e520f09432f7b75995c2652a4 Mon Sep 17 00:00:00 2001 From: williamnavaraj Date: Mon, 3 Jan 2022 13:50:42 +0000 Subject: [PATCH 3/5] Minor description correction and typo fix. --- .../next/Tools-Demos/2022-01-03-12-50-42.bpo-26571.2NjFW_.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Tools-Demos/2022-01-03-12-50-42.bpo-26571.2NjFW_.rst b/Misc/NEWS.d/next/Tools-Demos/2022-01-03-12-50-42.bpo-26571.2NjFW_.rst index f31aacd7a20556a..afa37e12b1488cc 100644 --- a/Misc/NEWS.d/next/Tools-Demos/2022-01-03-12-50-42.bpo-26571.2NjFW_.rst +++ b/Misc/NEWS.d/next/Tools-Demos/2022-01-03-12-50-42.bpo-26571.2NjFW_.rst @@ -1 +1 @@ -Enables turtle to be instantiated multiple times. Fixes error which causes it to through error every other time when running in notebooks or shell or other GUI based application involving close-and-reinstantiate operations of turtle screen. \ No newline at end of file +Enables turtle object to be instantiated multiple times. Fixes error thrown every other time when turtle screen is closed and turtle object is again reinstantiated through code. This particularly helps to use this module smoothly in notebooks or shell or other GUI based applications. From 4d574e0337aa40eb86f3b0afe7b0300ead1fb6e9 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 9 Aug 2026 22:13:14 +0300 Subject: [PATCH 4/5] Simplify the comment and move the NEWS entry to Library --- Lib/turtle.py | 9 +++------ .../2022-01-03-12-50-42.gh-issue-70758.2NjFW_.rst | 2 ++ .../Tools-Demos/2022-01-03-12-50-42.bpo-26571.2NjFW_.rst | 1 - 3 files changed, 5 insertions(+), 7 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2022-01-03-12-50-42.gh-issue-70758.2NjFW_.rst delete mode 100644 Misc/NEWS.d/next/Tools-Demos/2022-01-03-12-50-42.bpo-26571.2NjFW_.rst diff --git a/Lib/turtle.py b/Lib/turtle.py index 914a6d2719dd5db..b49df26bd07bc5a 100644 --- a/Lib/turtle.py +++ b/Lib/turtle.py @@ -1001,13 +1001,10 @@ def __init__(self, cv, mode=_CFG["mode"], rootwindow = cv.winfo_toplevel() rootwindow.wm_attributes(topmost=True) rootwindow.wm_attributes(topmost=False) - - # The class variable _RUNNING being persistent once this module is imported, this should be made True when the next object is created. - # This is particularly needed for running in notebooks or GUI application but does not affect the usual self-contained single instance execution - # nor any of the turtle-demos - + + # A new screen means that turtle graphics is running again. TurtleScreen._RUNNING = True - + def clear(self): """Delete all drawings and all turtles from the TurtleScreen. diff --git a/Misc/NEWS.d/next/Library/2022-01-03-12-50-42.gh-issue-70758.2NjFW_.rst b/Misc/NEWS.d/next/Library/2022-01-03-12-50-42.gh-issue-70758.2NjFW_.rst new file mode 100644 index 000000000000000..9cd0cce0f00af31 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-01-03-12-50-42.gh-issue-70758.2NjFW_.rst @@ -0,0 +1,2 @@ +Creating a new :mod:`turtle` screen after the previous one was closed no +longer raises :exc:`turtle.Terminator` on the first turtle command. diff --git a/Misc/NEWS.d/next/Tools-Demos/2022-01-03-12-50-42.bpo-26571.2NjFW_.rst b/Misc/NEWS.d/next/Tools-Demos/2022-01-03-12-50-42.bpo-26571.2NjFW_.rst deleted file mode 100644 index afa37e12b1488cc..000000000000000 --- a/Misc/NEWS.d/next/Tools-Demos/2022-01-03-12-50-42.bpo-26571.2NjFW_.rst +++ /dev/null @@ -1 +0,0 @@ -Enables turtle object to be instantiated multiple times. Fixes error thrown every other time when turtle screen is closed and turtle object is again reinstantiated through code. This particularly helps to use this module smoothly in notebooks or shell or other GUI based applications. From 68366d1095e229742cb601eb982e6bbc2e210f83 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Sun, 9 Aug 2026 23:29:31 +0300 Subject: [PATCH 5/5] Do not reference undocumented turtle.Terminator in the NEWS entry --- .../next/Library/2022-01-03-12-50-42.gh-issue-70758.2NjFW_.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2022-01-03-12-50-42.gh-issue-70758.2NjFW_.rst b/Misc/NEWS.d/next/Library/2022-01-03-12-50-42.gh-issue-70758.2NjFW_.rst index 9cd0cce0f00af31..c59f6fed93fd630 100644 --- a/Misc/NEWS.d/next/Library/2022-01-03-12-50-42.gh-issue-70758.2NjFW_.rst +++ b/Misc/NEWS.d/next/Library/2022-01-03-12-50-42.gh-issue-70758.2NjFW_.rst @@ -1,2 +1,2 @@ Creating a new :mod:`turtle` screen after the previous one was closed no -longer raises :exc:`turtle.Terminator` on the first turtle command. +longer raises ``turtle.Terminator`` on the first turtle command.