-
-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
gh-126349 Add context managers to turtle for fill
, poly
and no_animation
#126350
Open
MarieRoald
wants to merge
19
commits into
python:main
Choose a base branch
from
MarieRoald:fix-issue-126349
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+278
−14
Open
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
3379b3d
Add context managers for turtle.fill and turtle.poly
yngvem 879d886
Add documentation
MarieRoald 54f709c
Add context manager to turtle.TurtleScreen for disabling auto-update
MarieRoald 437c8ce
Forward functions correctly
yngvem d11d8f9
Add documentation
yngvem 1f04ee4
📜🤖 Added by blurb_it.
blurb-it[bot] 3947de1
Fix typo in docs
MarieRoald 5ee489b
Apply suggestions from code review
MarieRoald 28a5ac6
Address review comments
MarieRoald 6621bd3
Apply suggestions from code review
MarieRoald 2f5c488
Address review comments
MarieRoald 302a1ed
Apply suggestions from code review
MarieRoald ad779d5
Use setUp for unit tests
yngvem fd89c95
Address review comments
yngvem fe39751
Add missing blank line
MarieRoald 0a5e250
Update Lib/turtle.py
MarieRoald 6d381ec
Apply suggestions from code review
MarieRoald 42d678d
Update Lib/turtle.py
MarieRoald 7928306
Address reviewer comments
MarieRoald File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -213,6 +213,32 @@ useful when working with learners for whom typing is not a skill. | |||||||||||||||||
use turtle graphics with a learner. | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
Automatically begin and end filling | ||||||||||||||||||
----------------------------------- | ||||||||||||||||||
|
||||||||||||||||||
If you have Python 3.14 or later, you don't need to call :func:`begin_fill` and | ||||||||||||||||||
:func:`end_fill` for filling. Instead, you can use the :func:`fill` | ||||||||||||||||||
:term:`context manager` to automatically begin and end fill. Here is an | ||||||||||||||||||
example:: | ||||||||||||||||||
|
||||||||||||||||||
with fill(): | ||||||||||||||||||
for i in range(4): | ||||||||||||||||||
forward(100) | ||||||||||||||||||
right(90) | ||||||||||||||||||
|
||||||||||||||||||
forward(200) | ||||||||||||||||||
|
||||||||||||||||||
The code above is equivalent to:: | ||||||||||||||||||
|
||||||||||||||||||
begin_fill() | ||||||||||||||||||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
for i in range(4): | ||||||||||||||||||
forward(100) | ||||||||||||||||||
right(90) | ||||||||||||||||||
end_fill() | ||||||||||||||||||
|
||||||||||||||||||
forward(200) | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
Use the ``turtle`` module namespace | ||||||||||||||||||
----------------------------------- | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -351,6 +377,7 @@ Pen control | |||||||||||||||||
|
||||||||||||||||||
Filling | ||||||||||||||||||
| :func:`filling` | ||||||||||||||||||
| :func:`fill` | ||||||||||||||||||
| :func:`begin_fill` | ||||||||||||||||||
| :func:`end_fill` | ||||||||||||||||||
|
||||||||||||||||||
|
@@ -381,6 +408,7 @@ Using events | |||||||||||||||||
| :func:`ondrag` | ||||||||||||||||||
|
||||||||||||||||||
Special Turtle methods | ||||||||||||||||||
| :func:`poly` | ||||||||||||||||||
| :func:`begin_poly` | ||||||||||||||||||
| :func:`end_poly` | ||||||||||||||||||
| :func:`get_poly` | ||||||||||||||||||
|
@@ -403,6 +431,7 @@ Window control | |||||||||||||||||
| :func:`setworldcoordinates` | ||||||||||||||||||
|
||||||||||||||||||
Animation control | ||||||||||||||||||
| :func:`no_animation` | ||||||||||||||||||
| :func:`delay` | ||||||||||||||||||
| :func:`tracer` | ||||||||||||||||||
| :func:`update` | ||||||||||||||||||
|
@@ -1275,6 +1304,29 @@ Filling | |||||||||||||||||
... else: | ||||||||||||||||||
... turtle.pensize(3) | ||||||||||||||||||
|
||||||||||||||||||
.. function:: fill() | ||||||||||||||||||
hugovk marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
|
||||||||||||||||||
Fill the shape drawn in the ``with turtle.fill():`` block. | ||||||||||||||||||
|
||||||||||||||||||
.. doctest:: | ||||||||||||||||||
:skipif: _tkinter is None | ||||||||||||||||||
|
||||||||||||||||||
>>> turtle.color("black", "red") | ||||||||||||||||||
>>> with turtle.fill(): | ||||||||||||||||||
... turtle.circle(80) | ||||||||||||||||||
|
||||||||||||||||||
Using ``fill`` is equivalent to adding the :func:`begin_fill` before the | ||||||||||||||||||
MarieRoald marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
fill-block and :func:`end_fill` after the fill-block: | ||||||||||||||||||
|
||||||||||||||||||
.. doctest:: | ||||||||||||||||||
:skipif: _tkinter is None | ||||||||||||||||||
|
||||||||||||||||||
>>> turtle.color("black", "red") | ||||||||||||||||||
>>> turtle.begin_fill() | ||||||||||||||||||
>>> turtle.circle(80) | ||||||||||||||||||
>>> turtle.end_fill() | ||||||||||||||||||
|
||||||||||||||||||
.. versionadded:: next | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
.. function:: begin_fill() | ||||||||||||||||||
|
@@ -1648,6 +1700,23 @@ Using events | |||||||||||||||||
Special Turtle methods | ||||||||||||||||||
---------------------- | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
.. function:: poly() | ||||||||||||||||||
|
||||||||||||||||||
Record the vertices of a polygon drawn in the ``with turtle.poly():`` block. | ||||||||||||||||||
The first and last vertices will be connected. | ||||||||||||||||||
|
||||||||||||||||||
.. doctest:: | ||||||||||||||||||
:skipif: _tkinter is None | ||||||||||||||||||
|
||||||||||||||||||
>>> with turtle.poly(): | ||||||||||||||||||
... turtle.forward(100) | ||||||||||||||||||
... turtle.right(60) | ||||||||||||||||||
... turtle.forward(100) | ||||||||||||||||||
|
||||||||||||||||||
.. versionadded:: next | ||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
.. function:: begin_poly() | ||||||||||||||||||
|
||||||||||||||||||
Start recording the vertices of a polygon. Current turtle position is first | ||||||||||||||||||
|
@@ -1925,6 +1994,25 @@ Window control | |||||||||||||||||
Animation control | ||||||||||||||||||
----------------- | ||||||||||||||||||
|
||||||||||||||||||
.. function:: no_animation() | ||||||||||||||||||
|
||||||||||||||||||
Temporarilly disable turtle animation. The code written inside the | ||||||||||||||||||
MarieRoald marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
``no_animation`` block will not be animated, and once the code block is | ||||||||||||||||||
exited, the drawing will appear. | ||||||||||||||||||
MarieRoald marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
|
||||||||||||||||||
.. doctest:: | ||||||||||||||||||
:skipif: _tkinter is None | ||||||||||||||||||
|
||||||||||||||||||
>>> with screen.no_animation(): | ||||||||||||||||||
... dist = 2 | ||||||||||||||||||
... for i in range(200): | ||||||||||||||||||
... fd(dist) | ||||||||||||||||||
... rt(90) | ||||||||||||||||||
... dist += 2 | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can also be written more compact. Though, I'm not sure it is more readable, so feel free to ignore this :)
Suggested change
|
||||||||||||||||||
|
||||||||||||||||||
.. versionadded:: next | ||||||||||||||||||
picnixz marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||
|
||||||||||||||||||
|
||||||||||||||||||
.. function:: delay(delay=None) | ||||||||||||||||||
|
||||||||||||||||||
:param delay: positive integer | ||||||||||||||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't usually include things like "If you have Python 3.14 or later" because each set of docs relates to one specific Python version.
But perhaps it's okay here because turtle is often used by beginners who might read these docs but have 3.12 or 3.13 installed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes a lot of sense, but in this specific case I think it's useful. Many learners are at universities or schools with old versions of Python, and they might not be aware that there are several versions of Python. So I think it's useful to make it very clear that this is a new feature.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry to reopen, but IMO this reads a little bit unexpected. How about something in the lines of:
Starting with Python 3.14, you can use [...] instead of [...].
What do you think?