Skip to content

Commit 17d9f71

Browse files
[3.14] pythongh-138772: Fix and improve documentation for turtle color functions (pythonGH-139325) (pythonGH-140047)
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 a5bf8a9 commit 17d9f71

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
@@ -777,13 +777,17 @@ Turtle motion
777777
180.0
778778

779779

780-
.. function:: dot(size=None, *color)
780+
.. function:: dot()
781+
dot(size)
782+
dot(color, /)
783+
dot(size, color, /)
784+
dot(size, r, g, b, /)
781785

782786
:param size: an integer >= 1 (if given)
783787
:param color: a colorstring or a numeric color tuple
784788

785789
Draw a circular dot with diameter *size*, using *color*. If *size* is
786-
not given, the maximum of pensize+4 and 2*pensize is used.
790+
not given, the maximum of ``pensize+4`` and ``2*pensize`` is used.
787791

788792

789793
.. doctest::
@@ -1152,7 +1156,9 @@ Drawing state
11521156
Color control
11531157
~~~~~~~~~~~~~
11541158

1155-
.. function:: pencolor(*args)
1159+
.. function:: pencolor()
1160+
pencolor(color, /)
1161+
pencolor(r, g, b, /)
11561162

11571163
Return or set the pencolor.
11581164

@@ -1161,7 +1167,7 @@ Color control
11611167
``pencolor()``
11621168
Return the current pencolor as color specification string or
11631169
as a tuple (see example). May be used as input to another
1164-
color/pencolor/fillcolor call.
1170+
color/pencolor/fillcolor/bgcolor call.
11651171

11661172
``pencolor(colorstring)``
11671173
Set pencolor to *colorstring*, which is a Tk color specification string,
@@ -1201,7 +1207,9 @@ Color control
12011207
(50.0, 193.0, 143.0)
12021208

12031209

1204-
.. function:: fillcolor(*args)
1210+
.. function:: fillcolor()
1211+
fillcolor(color, /)
1212+
fillcolor(r, g, b, /)
12051213

12061214
Return or set the fillcolor.
12071215

@@ -1210,7 +1218,7 @@ Color control
12101218
``fillcolor()``
12111219
Return the current fillcolor as color specification string, possibly
12121220
in tuple format (see example). May be used as input to another
1213-
color/pencolor/fillcolor call.
1221+
color/pencolor/fillcolor/bgcolor call.
12141222

12151223
``fillcolor(colorstring)``
12161224
Set fillcolor to *colorstring*, which is a Tk color specification string,
@@ -1244,7 +1252,10 @@ Color control
12441252
(255.0, 255.0, 255.0)
12451253

12461254

1247-
.. function:: color(*args)
1255+
.. function:: color()
1256+
color(color, /)
1257+
color(r, g, b, /)
1258+
color(pencolor, fillcolor, /)
12481259

12491260
Return or set pencolor and fillcolor.
12501261

@@ -1870,13 +1881,32 @@ Most of the examples in this section refer to a TurtleScreen instance called
18701881
Window control
18711882
--------------
18721883

1873-
.. function:: bgcolor(*args)
1884+
.. function:: bgcolor()
1885+
bgcolor(color, /)
1886+
bgcolor(r, g, b, /)
18741887

1875-
:param args: a color string or three numbers in the range 0..colormode or a
1876-
3-tuple of such numbers
1888+
Return or set the background color of the TurtleScreen.
18771889

1890+
Four input formats are allowed:
1891+
1892+
``bgcolor()``
1893+
Return the current background color as color specification string or
1894+
as a tuple (see example). May be used as input to another
1895+
color/pencolor/fillcolor/bgcolor call.
1896+
1897+
``bgcolor(colorstring)``
1898+
Set the background color to *colorstring*, which is a Tk color
1899+
specification string, such as ``"red"``, ``"yellow"``, or ``"#33cc8c"``.
18781900

1879-
Set or return background color of the TurtleScreen.
1901+
``bgcolor((r, g, b))``
1902+
Set the background color to the RGB color represented by the tuple of
1903+
*r*, *g*, and *b*.
1904+
Each of *r*, *g*, and *b* must be in the range 0..colormode, where
1905+
colormode is either 1.0 or 255 (see :func:`colormode`).
1906+
1907+
``bgcolor(r, g, b)``
1908+
Set the background color to the RGB color represented by *r*, *g*, and *b*. Each of
1909+
*r*, *g*, and *b* must be in the range 0..colormode.
18801910

18811911
.. doctest::
18821912
:skipif: _tkinter is None

Lib/turtle.py

Lines changed: 69 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1214,16 +1214,32 @@ def turtles(self):
12141214
def bgcolor(self, *args):
12151215
"""Set or return backgroundcolor of the TurtleScreen.
12161216
1217-
Arguments (if given): a color string or three numbers
1218-
in the range 0..colormode or a 3-tuple of such numbers.
1217+
Four input formats are allowed:
1218+
- bgcolor()
1219+
Return the current background color as color specification
1220+
string or as a tuple (see example). May be used as input
1221+
to another color/pencolor/fillcolor/bgcolor call.
1222+
- bgcolor(colorstring)
1223+
Set the background color to colorstring, which is a Tk color
1224+
specification string, such as "red", "yellow", or "#33cc8c".
1225+
- bgcolor((r, g, b))
1226+
Set the background color to the RGB color represented by
1227+
the tuple of r, g, and b. Each of r, g, and b must be in
1228+
the range 0..colormode, where colormode is either 1.0 or 255
1229+
(see colormode()).
1230+
- bgcolor(r, g, b)
1231+
Set the background color to the RGB color represented by
1232+
r, g, and b. Each of r, g, and b must be in the range
1233+
0..colormode.
12191234
12201235
Example (for a TurtleScreen instance named screen):
12211236
>>> screen.bgcolor("orange")
12221237
>>> screen.bgcolor()
12231238
'orange'
1224-
>>> screen.bgcolor(0.5,0,0.5)
1239+
>>> colormode(255)
1240+
>>> screen.bgcolor('#800080')
12251241
>>> screen.bgcolor()
1226-
'#800080'
1242+
(128.0, 0.0, 128.0)
12271243
"""
12281244
if args:
12291245
color = self._colorstr(args)
@@ -1678,7 +1694,7 @@ def forward(self, distance):
16781694
16791695
Example (for a Turtle instance named turtle):
16801696
>>> turtle.position()
1681-
(0.00, 0.00)
1697+
(0.00,0.00)
16821698
>>> turtle.forward(25)
16831699
>>> turtle.position()
16841700
(25.00,0.00)
@@ -1701,10 +1717,10 @@ def back(self, distance):
17011717
17021718
Example (for a Turtle instance named turtle):
17031719
>>> turtle.position()
1704-
(0.00, 0.00)
1720+
(0.00,0.00)
17051721
>>> turtle.backward(30)
17061722
>>> turtle.position()
1707-
(-30.00, 0.00)
1723+
(-30.00,0.00)
17081724
"""
17091725
self._go(-distance)
17101726

@@ -1811,7 +1827,7 @@ def goto(self, x, y=None):
18111827
Example (for a Turtle instance named turtle):
18121828
>>> tp = turtle.pos()
18131829
>>> tp
1814-
(0.00, 0.00)
1830+
(0.00,0.00)
18151831
>>> turtle.setpos(60,30)
18161832
>>> turtle.pos()
18171833
(60.00,30.00)
@@ -1891,7 +1907,7 @@ def distance(self, x, y=None):
18911907
18921908
Example (for a Turtle instance named turtle):
18931909
>>> turtle.pos()
1894-
(0.00, 0.00)
1910+
(0.00,0.00)
18951911
>>> turtle.distance(30,40)
18961912
50.0
18971913
>>> pen = Turtle()
@@ -2230,19 +2246,17 @@ def color(self, *args):
22302246
22312247
Arguments:
22322248
Several input formats are allowed.
2233-
They use 0, 1, 2, or 3 arguments as follows:
2234-
2235-
color()
2236-
Return the current pencolor and the current fillcolor
2237-
as a pair of color specification strings as are returned
2238-
by pencolor and fillcolor.
2239-
color(colorstring), color((r,g,b)), color(r,g,b)
2240-
inputs as in pencolor, set both, fillcolor and pencolor,
2249+
They use 0 to 3 arguments as follows:
2250+
- color()
2251+
Return the current pencolor and the current fillcolor as
2252+
a pair of color specification strings or tuples as returned
2253+
by pencolor() and fillcolor().
2254+
- color(colorstring), color((r,g,b)), color(r,g,b)
2255+
Inputs as in pencolor(), set both, fillcolor and pencolor,
22412256
to the given value.
2242-
color(colorstring1, colorstring2),
2243-
color((r1,g1,b1), (r2,g2,b2))
2244-
equivalent to pencolor(colorstring1) and fillcolor(colorstring2)
2245-
and analogously, if the other input format is used.
2257+
- color(colorstring1, colorstring2), color((r1,g1,b1), (r2,g2,b2))
2258+
Equivalent to pencolor(colorstring1) and fillcolor(colorstring2)
2259+
and analogously if the other input format is used.
22462260
22472261
If turtleshape is a polygon, outline and interior of that polygon
22482262
is drawn with the newly set colors.
@@ -2253,9 +2267,9 @@ def color(self, *args):
22532267
>>> turtle.color()
22542268
('red', 'green')
22552269
>>> colormode(255)
2256-
>>> color((40, 80, 120), (160, 200, 240))
2270+
>>> color(('#285078', '#a0c8f0'))
22572271
>>> color()
2258-
('#285078', '#a0c8f0')
2272+
((40.0, 80.0, 120.0), (160.0, 200.0, 240.0))
22592273
"""
22602274
if args:
22612275
l = len(args)
@@ -2277,28 +2291,32 @@ def pencolor(self, *args):
22772291
Arguments:
22782292
Four input formats are allowed:
22792293
- pencolor()
2280-
Return the current pencolor as color specification string,
2281-
possibly in hex-number format (see example).
2282-
May be used as input to another color/pencolor/fillcolor call.
2294+
Return the current pencolor as color specification string or
2295+
as a tuple (see example). May be used as input to another
2296+
color/pencolor/fillcolor/bgcolor call.
22832297
- pencolor(colorstring)
2284-
s is a Tk color specification string, such as "red" or "yellow"
2298+
Set pencolor to colorstring, which is a Tk color
2299+
specification string, such as "red", "yellow", or "#33cc8c".
22852300
- pencolor((r, g, b))
2286-
*a tuple* of r, g, and b, which represent, an RGB color,
2287-
and each of r, g, and b are in the range 0..colormode,
2288-
where colormode is either 1.0 or 255
2301+
Set pencolor to the RGB color represented by the tuple of
2302+
r, g, and b. Each of r, g, and b must be in the range
2303+
0..colormode, where colormode is either 1.0 or 255 (see
2304+
colormode()).
22892305
- pencolor(r, g, b)
2290-
r, g, and b represent an RGB color, and each of r, g, and b
2291-
are in the range 0..colormode
2306+
Set pencolor to the RGB color represented by r, g, and b.
2307+
Each of r, g, and b must be in the range 0..colormode.
22922308
22932309
If turtleshape is a polygon, the outline of that polygon is drawn
22942310
with the newly set pencolor.
22952311
22962312
Example (for a Turtle instance named turtle):
22972313
>>> turtle.pencolor('brown')
2298-
>>> tup = (0.2, 0.8, 0.55)
2299-
>>> turtle.pencolor(tup)
23002314
>>> turtle.pencolor()
2301-
'#33cc8c'
2315+
'brown'
2316+
>>> colormode(255)
2317+
>>> turtle.pencolor('#32c18f')
2318+
>>> turtle.pencolor()
2319+
(50.0, 193.0, 143.0)
23022320
"""
23032321
if args:
23042322
color = self._colorstr(args)
@@ -2315,26 +2333,31 @@ def fillcolor(self, *args):
23152333
Four input formats are allowed:
23162334
- fillcolor()
23172335
Return the current fillcolor as color specification string,
2318-
possibly in hex-number format (see example).
2319-
May be used as input to another color/pencolor/fillcolor call.
2336+
possibly in tuple format (see example). May be used as
2337+
input to another color/pencolor/fillcolor/bgcolor call.
23202338
- fillcolor(colorstring)
2321-
s is a Tk color specification string, such as "red" or "yellow"
2339+
Set fillcolor to colorstring, which is a Tk color
2340+
specification string, such as "red", "yellow", or "#33cc8c".
23222341
- fillcolor((r, g, b))
2323-
*a tuple* of r, g, and b, which represent, an RGB color,
2324-
and each of r, g, and b are in the range 0..colormode,
2325-
where colormode is either 1.0 or 255
2342+
Set fillcolor to the RGB color represented by the tuple of
2343+
r, g, and b. Each of r, g, and b must be in the range
2344+
0..colormode, where colormode is either 1.0 or 255 (see
2345+
colormode()).
23262346
- fillcolor(r, g, b)
2327-
r, g, and b represent an RGB color, and each of r, g, and b
2328-
are in the range 0..colormode
2347+
Set fillcolor to the RGB color represented by r, g, and b.
2348+
Each of r, g, and b must be in the range 0..colormode.
23292349
23302350
If turtleshape is a polygon, the interior of that polygon is drawn
23312351
with the newly set fillcolor.
23322352
23332353
Example (for a Turtle instance named turtle):
23342354
>>> turtle.fillcolor('violet')
2335-
>>> col = turtle.pencolor()
2336-
>>> turtle.fillcolor(col)
2337-
>>> turtle.fillcolor(0, .5, 0)
2355+
>>> turtle.fillcolor()
2356+
'violet'
2357+
>>> colormode(255)
2358+
>>> turtle.fillcolor('#ffffff')
2359+
>>> turtle.fillcolor()
2360+
(255.0, 255.0, 255.0)
23382361
"""
23392362
if args:
23402363
color = self._colorstr(args)

0 commit comments

Comments
 (0)