Skip to content

Commit 4a630d7

Browse files
[3.13] gh-126008: Improve docstrings for Tkinter cget and configure methods (GH-133303) (GH-137661)
* Explain the behavior of Widget.configure() depending on arguments. * Unify descriptions. * Replace "resource" with "option". (cherry picked from commit 027cacb) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent da39cb9 commit 4a630d7

File tree

2 files changed

+70
-63
lines changed

2 files changed

+70
-63
lines changed

Lib/tkinter/__init__.py

Lines changed: 69 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,25 +7,25 @@
77
LabelFrame and PanedWindow.
88
99
Properties of the widgets are specified with keyword arguments.
10-
Keyword arguments have the same name as the corresponding resource
10+
Keyword arguments have the same name as the corresponding options
1111
under Tk.
1212
1313
Widgets are positioned with one of the geometry managers Place, Pack
1414
or Grid. These managers can be called with methods place, pack, grid
1515
available in every Widget.
1616
17-
Actions are bound to events by resources (e.g. keyword argument
18-
command) or with the method bind.
17+
Actions are bound to events by options (e.g. the command
18+
keyword argument) or with the bind() method.
1919
2020
Example (Hello, World):
2121
import tkinter
2222
from tkinter.constants import *
2323
tk = tkinter.Tk()
2424
frame = tkinter.Frame(tk, relief=RIDGE, borderwidth=2)
25-
frame.pack(fill=BOTH,expand=1)
25+
frame.pack(fill=BOTH, expand=1)
2626
label = tkinter.Label(frame, text="Hello, World")
2727
label.pack(fill=X, expand=1)
28-
button = tkinter.Button(frame,text="Exit",command=tk.destroy)
28+
button = tkinter.Button(frame, text="Exit", command=tk.destroy)
2929
button.pack(side=BOTTOM)
3030
tk.mainloop()
3131
"""
@@ -833,7 +833,7 @@ def tk_focusNext(self):
833833
The focus order first goes to the next child, then to
834834
the children of the child recursively and then to the
835835
next sibling which is higher in the stacking order. A
836-
widget is omitted if it has the takefocus resource set
836+
widget is omitted if it has the takefocus option set
837837
to 0."""
838838
name = self.tk.call('tk_focusNext', self._w)
839839
if not name: return None
@@ -1813,18 +1813,24 @@ def _configure(self, cmd, cnf, kw):
18131813
# These used to be defined in Widget:
18141814

18151815
def configure(self, cnf=None, **kw):
1816-
"""Configure resources of a widget.
1816+
"""Query or modify the configuration options of the widget.
18171817
1818-
The values for resources are specified as keyword
1819-
arguments. To get an overview about
1820-
the allowed keyword arguments call the method keys.
1818+
If no arguments are specified, return a dictionary describing
1819+
all of the available options for the widget.
1820+
1821+
If an option name is specified, then return a tuple describing
1822+
the one named option.
1823+
1824+
If one or more keyword arguments are specified or a dictionary
1825+
is specified, then modify the widget option(s) to have the given
1826+
value(s).
18211827
"""
18221828
return self._configure('configure', cnf, kw)
18231829

18241830
config = configure
18251831

18261832
def cget(self, key):
1827-
"""Return the resource value for a KEY given as string."""
1833+
"""Return the current value of the configuration option."""
18281834
return self.tk.call(self._w, 'cget', '-' + key)
18291835

18301836
__getitem__ = cget
@@ -1833,7 +1839,7 @@ def __setitem__(self, key, value):
18331839
self.configure({key: value})
18341840

18351841
def keys(self):
1836-
"""Return a list of all resource names of this widget."""
1842+
"""Return a list of all option names of this widget."""
18371843
splitlist = self.tk.splitlist
18381844
return [splitlist(x)[0][1:] for x in
18391845
splitlist(self.tk.call(self._w, 'configure'))]
@@ -1952,7 +1958,7 @@ def _grid_configure(self, command, index, cnf, kw):
19521958
def grid_columnconfigure(self, index, cnf={}, **kw):
19531959
"""Configure column INDEX of a grid.
19541960
1955-
Valid resources are minsize (minimum size of the column),
1961+
Valid options are minsize (minimum size of the column),
19561962
weight (how much does additional space propagate to this column)
19571963
and pad (how much space to let additionally)."""
19581964
return self._grid_configure('columnconfigure', index, cnf, kw)
@@ -1983,7 +1989,7 @@ def grid_propagate(self, flag=_noarg_):
19831989
def grid_rowconfigure(self, index, cnf={}, **kw):
19841990
"""Configure row INDEX of a grid.
19851991
1986-
Valid resources are minsize (minimum size of the row),
1992+
Valid options are minsize (minimum size of the row),
19871993
weight (how much does additional space propagate to this row)
19881994
and pad (how much space to let additionally)."""
19891995
return self._grid_configure('rowconfigure', index, cnf, kw)
@@ -2803,7 +2809,7 @@ class Toplevel(BaseWidget, Wm):
28032809
def __init__(self, master=None, cnf={}, **kw):
28042810
"""Construct a toplevel widget with the parent MASTER.
28052811
2806-
Valid resource names: background, bd, bg, borderwidth, class,
2812+
Valid option names: background, bd, bg, borderwidth, class,
28072813
colormap, container, cursor, height, highlightbackground,
28082814
highlightcolor, highlightthickness, menu, relief, screen, takefocus,
28092815
use, visual, width."""
@@ -2880,7 +2886,7 @@ class Canvas(Widget, XView, YView):
28802886
def __init__(self, master=None, cnf={}, **kw):
28812887
"""Construct a canvas widget with the parent MASTER.
28822888
2883-
Valid resource names: background, bd, bg, borderwidth, closeenough,
2889+
Valid option names: background, bd, bg, borderwidth, closeenough,
28842890
confine, cursor, height, highlightbackground, highlightcolor,
28852891
highlightthickness, insertbackground, insertborderwidth,
28862892
insertofftime, insertontime, insertwidth, offset, relief,
@@ -3089,16 +3095,14 @@ def insert(self, *args):
30893095
self.tk.call((self._w, 'insert') + args)
30903096

30913097
def itemcget(self, tagOrId, option):
3092-
"""Return the resource value for an OPTION for item TAGORID."""
3098+
"""Return the value of OPTION for item TAGORID."""
30933099
return self.tk.call(
30943100
(self._w, 'itemcget') + (tagOrId, '-'+option))
30953101

30963102
def itemconfigure(self, tagOrId, cnf=None, **kw):
3097-
"""Configure resources of an item TAGORID.
3103+
"""Query or modify the configuration options of an item TAGORID.
30983104
3099-
The values for resources are specified as keyword
3100-
arguments. To get an overview about
3101-
the allowed keyword arguments call the method without arguments.
3105+
Similar to configure() except that it applies to the specified item.
31023106
"""
31033107
return self._configure(('itemconfigure', tagOrId), cnf, kw)
31043108

@@ -3190,7 +3194,7 @@ class Checkbutton(Widget):
31903194
def __init__(self, master=None, cnf={}, **kw):
31913195
"""Construct a checkbutton widget with the parent MASTER.
31923196
3193-
Valid resource names: activebackground, activeforeground, anchor,
3197+
Valid option names: activebackground, activeforeground, anchor,
31943198
background, bd, bg, bitmap, borderwidth, command, cursor,
31953199
disabledforeground, fg, font, foreground, height,
31963200
highlightbackground, highlightcolor, highlightthickness, image,
@@ -3221,7 +3225,7 @@ def flash(self):
32213225
self.tk.call(self._w, 'flash')
32223226

32233227
def invoke(self):
3224-
"""Toggle the button and invoke a command if given as resource."""
3228+
"""Toggle the button and invoke a command if given as option."""
32253229
return self.tk.call(self._w, 'invoke')
32263230

32273231
def select(self):
@@ -3239,7 +3243,7 @@ class Entry(Widget, XView):
32393243
def __init__(self, master=None, cnf={}, **kw):
32403244
"""Construct an entry widget with the parent MASTER.
32413245
3242-
Valid resource names: background, bd, bg, borderwidth, cursor,
3246+
Valid option names: background, bd, bg, borderwidth, cursor,
32433247
exportselection, fg, font, foreground, highlightbackground,
32443248
highlightcolor, highlightthickness, insertbackground,
32453249
insertborderwidth, insertofftime, insertontime, insertwidth,
@@ -3325,7 +3329,7 @@ class Frame(Widget):
33253329
def __init__(self, master=None, cnf={}, **kw):
33263330
"""Construct a frame widget with the parent MASTER.
33273331
3328-
Valid resource names: background, bd, bg, borderwidth, class,
3332+
Valid option names: background, bd, bg, borderwidth, class,
33293333
colormap, container, cursor, height, highlightbackground,
33303334
highlightcolor, highlightthickness, relief, takefocus, visual, width."""
33313335
cnf = _cnfmerge((cnf, kw))
@@ -3369,7 +3373,7 @@ class Listbox(Widget, XView, YView):
33693373
def __init__(self, master=None, cnf={}, **kw):
33703374
"""Construct a listbox widget with the parent MASTER.
33713375
3372-
Valid resource names: background, bd, bg, borderwidth, cursor,
3376+
Valid option names: background, bd, bg, borderwidth, cursor,
33733377
exportselection, fg, font, foreground, height, highlightbackground,
33743378
highlightcolor, highlightthickness, relief, selectbackground,
33753379
selectborderwidth, selectforeground, selectmode, setgrid, takefocus,
@@ -3462,18 +3466,15 @@ def size(self):
34623466
return self.tk.getint(self.tk.call(self._w, 'size'))
34633467

34643468
def itemcget(self, index, option):
3465-
"""Return the resource value for an ITEM and an OPTION."""
3469+
"""Return the value of OPTION for item at INDEX."""
34663470
return self.tk.call(
34673471
(self._w, 'itemcget') + (index, '-'+option))
34683472

34693473
def itemconfigure(self, index, cnf=None, **kw):
3470-
"""Configure resources of an ITEM.
3474+
"""Query or modify the configuration options of an item at INDEX.
34713475
3472-
The values for resources are specified as keyword arguments.
3473-
To get an overview about the allowed keyword arguments
3474-
call the method without arguments.
3475-
Valid resource names: background, bg, foreground, fg,
3476-
selectbackground, selectforeground."""
3476+
Similar to configure() except that it applies to the specified item.
3477+
"""
34773478
return self._configure(('itemconfigure', index), cnf, kw)
34783479

34793480
itemconfig = itemconfigure
@@ -3485,7 +3486,7 @@ class Menu(Widget):
34853486
def __init__(self, master=None, cnf={}, **kw):
34863487
"""Construct menu widget with the parent MASTER.
34873488
3488-
Valid resource names: activebackground, activeborderwidth,
3489+
Valid option names: activebackground, activeborderwidth,
34893490
activeforeground, background, bd, bg, borderwidth, cursor,
34903491
disabledforeground, fg, font, foreground, postcommand, relief,
34913492
selectcolor, takefocus, tearoff, tearoffcommand, title, type."""
@@ -3566,11 +3567,15 @@ def delete(self, index1, index2=None):
35663567
self.tk.call(self._w, 'delete', index1, index2)
35673568

35683569
def entrycget(self, index, option):
3569-
"""Return the resource value of a menu item for OPTION at INDEX."""
3570+
"""Return the value of OPTION for a menu item at INDEX."""
35703571
return self.tk.call(self._w, 'entrycget', index, '-' + option)
35713572

35723573
def entryconfigure(self, index, cnf=None, **kw):
3573-
"""Configure a menu item at INDEX."""
3574+
"""Query or modify the configuration options of a menu item at INDEX.
3575+
3576+
Similar to configure() except that it applies to the specified
3577+
menu item.
3578+
"""
35743579
return self._configure(('entryconfigure', index), cnf, kw)
35753580

35763581
entryconfig = entryconfigure
@@ -3628,7 +3633,7 @@ class Radiobutton(Widget):
36283633
def __init__(self, master=None, cnf={}, **kw):
36293634
"""Construct a radiobutton widget with the parent MASTER.
36303635
3631-
Valid resource names: activebackground, activeforeground, anchor,
3636+
Valid option names: activebackground, activeforeground, anchor,
36323637
background, bd, bg, bitmap, borderwidth, command, cursor,
36333638
disabledforeground, fg, font, foreground, height,
36343639
highlightbackground, highlightcolor, highlightthickness, image,
@@ -3647,7 +3652,7 @@ def flash(self):
36473652
self.tk.call(self._w, 'flash')
36483653

36493654
def invoke(self):
3650-
"""Toggle the button and invoke a command if given as resource."""
3655+
"""Toggle the button and invoke a command if given as option."""
36513656
return self.tk.call(self._w, 'invoke')
36523657

36533658
def select(self):
@@ -3661,7 +3666,7 @@ class Scale(Widget):
36613666
def __init__(self, master=None, cnf={}, **kw):
36623667
"""Construct a scale widget with the parent MASTER.
36633668
3664-
Valid resource names: activebackground, background, bigincrement, bd,
3669+
Valid option names: activebackground, background, bigincrement, bd,
36653670
bg, borderwidth, command, cursor, digits, fg, font, foreground, from,
36663671
highlightbackground, highlightcolor, highlightthickness, label,
36673672
length, orient, relief, repeatdelay, repeatinterval, resolution,
@@ -3700,7 +3705,7 @@ class Scrollbar(Widget):
37003705
def __init__(self, master=None, cnf={}, **kw):
37013706
"""Construct a scrollbar widget with the parent MASTER.
37023707
3703-
Valid resource names: activebackground, activerelief,
3708+
Valid option names: activebackground, activerelief,
37043709
background, bd, bg, borderwidth, command, cursor,
37053710
elementborderwidth, highlightbackground,
37063711
highlightcolor, highlightthickness, jump, orient,
@@ -3944,7 +3949,11 @@ def image_cget(self, index, option):
39443949
return self.tk.call(self._w, "image", "cget", index, option)
39453950

39463951
def image_configure(self, index, cnf=None, **kw):
3947-
"""Configure an embedded image at INDEX."""
3952+
"""Query or modify the configuration options of an embedded image at INDEX.
3953+
3954+
Similar to configure() except that it applies to the specified
3955+
embedded image.
3956+
"""
39483957
return self._configure(('image', 'configure', index), cnf, kw)
39493958

39503959
def image_create(self, index, cnf={}, **kw):
@@ -4082,7 +4091,10 @@ def tag_cget(self, tagName, option):
40824091
return self.tk.call(self._w, 'tag', 'cget', tagName, option)
40834092

40844093
def tag_configure(self, tagName, cnf=None, **kw):
4085-
"""Configure a tag TAGNAME."""
4094+
"""Query or modify the configuration options of a tag TAGNAME.
4095+
4096+
Similar to configure() except that it applies to the specified tag.
4097+
"""
40864098
return self._configure(('tag', 'configure', tagName), cnf, kw)
40874099

40884100
tag_config = tag_configure
@@ -4140,7 +4152,11 @@ def window_cget(self, index, option):
41404152
return self.tk.call(self._w, 'window', 'cget', index, option)
41414153

41424154
def window_configure(self, index, cnf=None, **kw):
4143-
"""Configure an embedded window at INDEX."""
4155+
"""Query or modify the configuration options of an embedded window at INDEX.
4156+
4157+
Similar to configure() except that it applies to the specified
4158+
embedded window.
4159+
"""
41444160
return self._configure(('window', 'configure', index), cnf, kw)
41454161

41464162
window_config = window_configure
@@ -4180,7 +4196,7 @@ class OptionMenu(Menubutton):
41804196

41814197
def __init__(self, master, variable, value, *values, **kwargs):
41824198
"""Construct an optionmenu widget with the parent MASTER, with
4183-
the resource textvariable set to VARIABLE, the initially selected
4199+
the option textvariable set to VARIABLE, the initially selected
41844200
value VALUE, the other menu values VALUES and an additional
41854201
keyword argument command."""
41864202
kw = {"borderwidth": 2, "textvariable": variable,
@@ -4282,7 +4298,7 @@ class PhotoImage(Image):
42824298
def __init__(self, name=None, cnf={}, master=None, **kw):
42834299
"""Create an image with NAME.
42844300
4285-
Valid resource names: data, format, file, gamma, height, palette,
4301+
Valid option names: data, format, file, gamma, height, palette,
42864302
width."""
42874303
Image.__init__(self, 'photo', name, cnf, master, **kw)
42884304

@@ -4545,7 +4561,7 @@ class BitmapImage(Image):
45454561
def __init__(self, name=None, cnf={}, master=None, **kw):
45464562
"""Create a bitmap with NAME.
45474563
4548-
Valid resource names: background, data, file, foreground, maskdata, maskfile."""
4564+
Valid option names: background, data, file, foreground, maskdata, maskfile."""
45494565
Image.__init__(self, 'bitmap', name, cnf, master, **kw)
45504566

45514567

@@ -4863,26 +4879,17 @@ def sash_place(self, index, x, y):
48634879
return self.sash("place", index, x, y)
48644880

48654881
def panecget(self, child, option):
4866-
"""Query a management option for window.
4867-
4868-
Option may be any value allowed by the paneconfigure subcommand
4869-
"""
4882+
"""Return the value of option for a child window."""
48704883
return self.tk.call(
48714884
(self._w, 'panecget') + (child, '-'+option))
48724885

48734886
def paneconfigure(self, tagOrId, cnf=None, **kw):
4874-
"""Query or modify the management options for window.
4875-
4876-
If no option is specified, returns a list describing all
4877-
of the available options for pathName. If option is
4878-
specified with no value, then the command returns a list
4879-
describing the one named option (this list will be identical
4880-
to the corresponding sublist of the value returned if no
4881-
option is specified). If one or more option-value pairs are
4882-
specified, then the command modifies the given widget
4883-
option(s) to have the given value(s); in this case the
4884-
command returns an empty string. The following options
4885-
are supported:
4887+
"""Query or modify the configuration options for a child window.
4888+
4889+
Similar to configure() except that it applies to the specified
4890+
window.
4891+
4892+
The following options are supported:
48864893
48874894
after window
48884895
Insert the window after the window specified. window

Lib/tkinter/ttk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1587,7 +1587,7 @@ class OptionMenu(Menubutton):
15871587

15881588
def __init__(self, master, variable, default=None, *values, **kwargs):
15891589
"""Construct a themed OptionMenu widget with master as the parent,
1590-
the resource textvariable set to variable, the initially selected
1590+
the option textvariable set to variable, the initially selected
15911591
value specified by the default parameter, the menu values given by
15921592
*values and additional keywords.
15931593

0 commit comments

Comments
 (0)