Skip to content

Commit 0e78932

Browse files
[3.13] pythongh-138772: Fix and improve documentation for turtle color functions (pythonGH-139325) (pythonGH-140048)
Use multiple signatures for clarity. Explain different forms of bgcolor() in details. Fix outdated docstrings. (cherry picked from commit 525dcfe) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 2783841 commit 0e78932

File tree

2 files changed

+110
-57
lines changed

2 files changed

+110
-57
lines changed

Doc/library/turtle.rst

Lines changed: 41 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -743,13 +743,17 @@ Turtle motion
743743
180.0
744744

745745

746-
.. function:: dot(size=None, *color)
746+
.. function:: dot()
747+
dot(size)
748+
dot(color, /)
749+
dot(size, color, /)
750+
dot(size, r, g, b, /)
747751

748752
:param size: an integer >= 1 (if given)
749753
:param color: a colorstring or a numeric color tuple
750754

751755
Draw a circular dot with diameter *size*, using *color*. If *size* is
752-
not given, the maximum of pensize+4 and 2*pensize is used.
756+
not given, the maximum of ``pensize+4`` and ``2*pensize`` is used.
753757

754758

755759
.. doctest::
@@ -1118,7 +1122,9 @@ Drawing state
11181122
Color control
11191123
~~~~~~~~~~~~~
11201124

1121-
.. function:: pencolor(*args)
1125+
.. function:: pencolor()
1126+
pencolor(color, /)
1127+
pencolor(r, g, b, /)
11221128

11231129
Return or set the pencolor.
11241130

@@ -1127,7 +1133,7 @@ Color control
11271133
``pencolor()``
11281134
Return the current pencolor as color specification string or
11291135
as a tuple (see example). May be used as input to another
1130-
color/pencolor/fillcolor call.
1136+
color/pencolor/fillcolor/bgcolor call.
11311137

11321138
``pencolor(colorstring)``
11331139
Set pencolor to *colorstring*, which is a Tk color specification string,
@@ -1167,7 +1173,9 @@ Color control
11671173
(50.0, 193.0, 143.0)
11681174

11691175

1170-
.. function:: fillcolor(*args)
1176+
.. function:: fillcolor()
1177+
fillcolor(color, /)
1178+
fillcolor(r, g, b, /)
11711179

11721180
Return or set the fillcolor.
11731181

@@ -1176,7 +1184,7 @@ Color control
11761184
``fillcolor()``
11771185
Return the current fillcolor as color specification string, possibly
11781186
in tuple format (see example). May be used as input to another
1179-
color/pencolor/fillcolor call.
1187+
color/pencolor/fillcolor/bgcolor call.
11801188

11811189
``fillcolor(colorstring)``
11821190
Set fillcolor to *colorstring*, which is a Tk color specification string,
@@ -1210,7 +1218,10 @@ Color control
12101218
(255.0, 255.0, 255.0)
12111219

12121220

1213-
.. function:: color(*args)
1221+
.. function:: color()
1222+
color(color, /)
1223+
color(r, g, b, /)
1224+
color(pencolor, fillcolor, /)
12141225

12151226
Return or set pencolor and fillcolor.
12161227

@@ -1796,13 +1807,32 @@ Most of the examples in this section refer to a TurtleScreen instance called
17961807
Window control
17971808
--------------
17981809

1799-
.. function:: bgcolor(*args)
1810+
.. function:: bgcolor()
1811+
bgcolor(color, /)
1812+
bgcolor(r, g, b, /)
18001813

1801-
:param args: a color string or three numbers in the range 0..colormode or a
1802-
3-tuple of such numbers
1814+
Return or set the background color of the TurtleScreen.
18031815

1816+
Four input formats are allowed:
1817+
1818+
``bgcolor()``
1819+
Return the current background color as color specification string or
1820+
as a tuple (see example). May be used as input to another
1821+
color/pencolor/fillcolor/bgcolor call.
1822+
1823+
``bgcolor(colorstring)``
1824+
Set the background color to *colorstring*, which is a Tk color
1825+
specification string, such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
18041826

1805-
Set or return background color of the TurtleScreen.
1827+
``bgcolor((r, g, b))``
1828+
Set the background color to the RGB color represented by the tuple of
1829+
*r*, *g*, and *b*.
1830+
Each of *r*, *g*, and *b* must be in the range 0..colormode, where
1831+
colormode is either 1.0 or 255 (see :func:`colormode`).
1832+
1833+
``bgcolor(r, g, b)``
1834+
Set the background color to the RGB color represented by *r*, *g*, and *b*. Each of
1835+
*r*, *g*, and *b* must be in the range 0..colormode.
18061836

18071837
.. doctest::
18081838
:skipif: _tkinter is None

Lib/turtle.py

Lines changed: 69 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,16 +1213,32 @@ def turtles(self):
12131213
def bgcolor(self, *args):
12141214
"""Set or return backgroundcolor of the TurtleScreen.
12151215
1216-
Arguments (if given): a color string or three numbers
1217-
in the range 0..colormode or a 3-tuple of such numbers.
1216+
Four input formats are allowed:
1217+
- bgcolor()
1218+
Return the current background color as color specification
1219+
string or as a tuple (see example). May be used as input
1220+
to another color/pencolor/fillcolor/bgcolor call.
1221+
- bgcolor(colorstring)
1222+
Set the background color to colorstring, which is a Tk color
1223+
specification string, such as "red", "yellow", or "#33cc8c".
1224+
- bgcolor((r, g, b))
1225+
Set the background color to the RGB color represented by
1226+
the tuple of r, g, and b. Each of r, g, and b must be in
1227+
the range 0..colormode, where colormode is either 1.0 or 255
1228+
(see colormode()).
1229+
- bgcolor(r, g, b)
1230+
Set the background color to the RGB color represented by
1231+
r, g, and b. Each of r, g, and b must be in the range
1232+
0..colormode.
12181233
12191234
Example (for a TurtleScreen instance named screen):
12201235
>>> screen.bgcolor("orange")
12211236
>>> screen.bgcolor()
12221237
'orange'
1223-
>>> screen.bgcolor(0.5,0,0.5)
1238+
>>> colormode(255)
1239+
>>> screen.bgcolor('#800080')
12241240
>>> screen.bgcolor()
1225-
'#800080'
1241+
(128.0, 0.0, 128.0)
12261242
"""
12271243
if args:
12281244
color = self._colorstr(args)
@@ -1624,7 +1640,7 @@ def forward(self, distance):
16241640
16251641
Example (for a Turtle instance named turtle):
16261642
>>> turtle.position()
1627-
(0.00, 0.00)
1643+
(0.00,0.00)
16281644
>>> turtle.forward(25)
16291645
>>> turtle.position()
16301646
(25.00,0.00)
@@ -1647,10 +1663,10 @@ def back(self, distance):
16471663
16481664
Example (for a Turtle instance named turtle):
16491665
>>> turtle.position()
1650-
(0.00, 0.00)
1666+
(0.00,0.00)
16511667
>>> turtle.backward(30)
16521668
>>> turtle.position()
1653-
(-30.00, 0.00)
1669+
(-30.00,0.00)
16541670
"""
16551671
self._go(-distance)
16561672

@@ -1757,7 +1773,7 @@ def goto(self, x, y=None):
17571773
Example (for a Turtle instance named turtle):
17581774
>>> tp = turtle.pos()
17591775
>>> tp
1760-
(0.00, 0.00)
1776+
(0.00,0.00)
17611777
>>> turtle.setpos(60,30)
17621778
>>> turtle.pos()
17631779
(60.00,30.00)
@@ -1837,7 +1853,7 @@ def distance(self, x, y=None):
18371853
18381854
Example (for a Turtle instance named turtle):
18391855
>>> turtle.pos()
1840-
(0.00, 0.00)
1856+
(0.00,0.00)
18411857
>>> turtle.distance(30,40)
18421858
50.0
18431859
>>> pen = Turtle()
@@ -2176,19 +2192,17 @@ def color(self, *args):
21762192
21772193
Arguments:
21782194
Several input formats are allowed.
2179-
They use 0, 1, 2, or 3 arguments as follows:
2180-
2181-
color()
2182-
Return the current pencolor and the current fillcolor
2183-
as a pair of color specification strings as are returned
2184-
by pencolor and fillcolor.
2185-
color(colorstring), color((r,g,b)), color(r,g,b)
2186-
inputs as in pencolor, set both, fillcolor and pencolor,
2195+
They use 0 to 3 arguments as follows:
2196+
- color()
2197+
Return the current pencolor and the current fillcolor as
2198+
a pair of color specification strings or tuples as returned
2199+
by pencolor() and fillcolor().
2200+
- color(colorstring), color((r,g,b)), color(r,g,b)
2201+
Inputs as in pencolor(), set both, fillcolor and pencolor,
21872202
to the given value.
2188-
color(colorstring1, colorstring2),
2189-
color((r1,g1,b1), (r2,g2,b2))
2190-
equivalent to pencolor(colorstring1) and fillcolor(colorstring2)
2191-
and analogously, if the other input format is used.
2203+
- color(colorstring1, colorstring2), color((r1,g1,b1), (r2,g2,b2))
2204+
Equivalent to pencolor(colorstring1) and fillcolor(colorstring2)
2205+
and analogously if the other input format is used.
21922206
21932207
If turtleshape is a polygon, outline and interior of that polygon
21942208
is drawn with the newly set colors.
@@ -2199,9 +2213,9 @@ def color(self, *args):
21992213
>>> turtle.color()
22002214
('red', 'green')
22012215
>>> colormode(255)
2202-
>>> color((40, 80, 120), (160, 200, 240))
2216+
>>> color(('#285078', '#a0c8f0'))
22032217
>>> color()
2204-
('#285078', '#a0c8f0')
2218+
((40.0, 80.0, 120.0), (160.0, 200.0, 240.0))
22052219
"""
22062220
if args:
22072221
l = len(args)
@@ -2223,28 +2237,32 @@ def pencolor(self, *args):
22232237
Arguments:
22242238
Four input formats are allowed:
22252239
- pencolor()
2226-
Return the current pencolor as color specification string,
2227-
possibly in hex-number format (see example).
2228-
May be used as input to another color/pencolor/fillcolor call.
2240+
Return the current pencolor as color specification string or
2241+
as a tuple (see example). May be used as input to another
2242+
color/pencolor/fillcolor/bgcolor call.
22292243
- pencolor(colorstring)
2230-
s is a Tk color specification string, such as "red" or "yellow"
2244+
Set pencolor to colorstring, which is a Tk color
2245+
specification string, such as "red", "yellow", or "#33cc8c".
22312246
- pencolor((r, g, b))
2232-
*a tuple* of r, g, and b, which represent, an RGB color,
2233-
and each of r, g, and b are in the range 0..colormode,
2234-
where colormode is either 1.0 or 255
2247+
Set pencolor to the RGB color represented by the tuple of
2248+
r, g, and b. Each of r, g, and b must be in the range
2249+
0..colormode, where colormode is either 1.0 or 255 (see
2250+
colormode()).
22352251
- pencolor(r, g, b)
2236-
r, g, and b represent an RGB color, and each of r, g, and b
2237-
are in the range 0..colormode
2252+
Set pencolor to the RGB color represented by r, g, and b.
2253+
Each of r, g, and b must be in the range 0..colormode.
22382254
22392255
If turtleshape is a polygon, the outline of that polygon is drawn
22402256
with the newly set pencolor.
22412257
22422258
Example (for a Turtle instance named turtle):
22432259
>>> turtle.pencolor('brown')
2244-
>>> tup = (0.2, 0.8, 0.55)
2245-
>>> turtle.pencolor(tup)
22462260
>>> turtle.pencolor()
2247-
'#33cc8c'
2261+
'brown'
2262+
>>> colormode(255)
2263+
>>> turtle.pencolor('#32c18f')
2264+
>>> turtle.pencolor()
2265+
(50.0, 193.0, 143.0)
22482266
"""
22492267
if args:
22502268
color = self._colorstr(args)
@@ -2261,26 +2279,31 @@ def fillcolor(self, *args):
22612279
Four input formats are allowed:
22622280
- fillcolor()
22632281
Return the current fillcolor as color specification string,
2264-
possibly in hex-number format (see example).
2265-
May be used as input to another color/pencolor/fillcolor call.
2282+
possibly in tuple format (see example). May be used as
2283+
input to another color/pencolor/fillcolor/bgcolor call.
22662284
- fillcolor(colorstring)
2267-
s is a Tk color specification string, such as "red" or "yellow"
2285+
Set fillcolor to colorstring, which is a Tk color
2286+
specification string, such as "red", "yellow", or "#33cc8c".
22682287
- fillcolor((r, g, b))
2269-
*a tuple* of r, g, and b, which represent, an RGB color,
2270-
and each of r, g, and b are in the range 0..colormode,
2271-
where colormode is either 1.0 or 255
2288+
Set fillcolor to the RGB color represented by the tuple of
2289+
r, g, and b. Each of r, g, and b must be in the range
2290+
0..colormode, where colormode is either 1.0 or 255 (see
2291+
colormode()).
22722292
- fillcolor(r, g, b)
2273-
r, g, and b represent an RGB color, and each of r, g, and b
2274-
are in the range 0..colormode
2293+
Set fillcolor to the RGB color represented by r, g, and b.
2294+
Each of r, g, and b must be in the range 0..colormode.
22752295
22762296
If turtleshape is a polygon, the interior of that polygon is drawn
22772297
with the newly set fillcolor.
22782298
22792299
Example (for a Turtle instance named turtle):
22802300
>>> turtle.fillcolor('violet')
2281-
>>> col = turtle.pencolor()
2282-
>>> turtle.fillcolor(col)
2283-
>>> turtle.fillcolor(0, .5, 0)
2301+
>>> turtle.fillcolor()
2302+
'violet'
2303+
>>> colormode(255)
2304+
>>> turtle.fillcolor('#ffffff')
2305+
>>> turtle.fillcolor()
2306+
(255.0, 255.0, 255.0)
22842307
"""
22852308
if args:
22862309
color = self._colorstr(args)

0 commit comments

Comments
 (0)