Skip to content

Commit 71cbffd

Browse files
gh-143754: Add Tkinter methods pack_content(), place_content() and grid_content() (GH-143845)
They use Tk commands with new name like "pack content instead of old "pack slaves".
1 parent 05eab96 commit 71cbffd

File tree

4 files changed

+102
-19
lines changed

4 files changed

+102
-19
lines changed

Doc/whatsnew/3.15.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -792,6 +792,12 @@ tkinter
792792
using Tcl's ``-all`` and ``-overlap`` options.
793793
(Contributed by Rihaan Meher in :gh:`130848`)
794794

795+
* Added new methods :meth:`!pack_content`, :meth:`!place_content` and
796+
:meth:`!grid_content` which use Tk commands with new names (introduced
797+
in Tk 8.6) instead of :meth:`!*_slaves` methods which use Tk commands
798+
with outdated names.
799+
(Contributed by Serhiy Storchaka in :gh:`143754`)
800+
795801
types
796802
------
797803

Lib/test/test_tkinter/test_geometry_managers.py

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ def test_pack_configure_after(self):
4040
b.pack_configure(side='top')
4141
c.pack_configure(side='top')
4242
d.pack_configure(side='top')
43-
self.assertEqual(pack.pack_slaves(), [a, b, c, d])
43+
self.assertEqual(pack.pack_content(), [a, b, c, d])
4444
a.pack_configure(after=b)
45-
self.assertEqual(pack.pack_slaves(), [b, a, c, d])
45+
self.assertEqual(pack.pack_content(), [b, a, c, d])
4646
a.pack_configure(after=a)
47-
self.assertEqual(pack.pack_slaves(), [b, a, c, d])
47+
self.assertEqual(pack.pack_content(), [b, a, c, d])
4848

4949
def test_pack_configure_anchor(self):
5050
pack, a, b, c, d = self.create2()
@@ -73,11 +73,11 @@ def test_pack_configure_before(self):
7373
b.pack_configure(side='top')
7474
c.pack_configure(side='top')
7575
d.pack_configure(side='top')
76-
self.assertEqual(pack.pack_slaves(), [a, b, c, d])
76+
self.assertEqual(pack.pack_content(), [a, b, c, d])
7777
a.pack_configure(before=d)
78-
self.assertEqual(pack.pack_slaves(), [b, c, a, d])
78+
self.assertEqual(pack.pack_content(), [b, c, a, d])
7979
a.pack_configure(before=a)
80-
self.assertEqual(pack.pack_slaves(), [b, c, a, d])
80+
self.assertEqual(pack.pack_content(), [b, c, a, d])
8181

8282
def test_pack_configure_expand(self):
8383
pack, a, b, c, d = self.create2()
@@ -110,10 +110,10 @@ def test_pack_configure_in(self):
110110
c.pack_configure(side='top')
111111
d.pack_configure(side='top')
112112
a.pack_configure(in_=pack)
113-
self.assertEqual(pack.pack_slaves(), [b, c, d, a])
113+
self.assertEqual(pack.pack_content(), [b, c, d, a])
114114
a.pack_configure(in_=c)
115-
self.assertEqual(pack.pack_slaves(), [b, c, d])
116-
self.assertEqual(c.pack_slaves(), [a])
115+
self.assertEqual(pack.pack_content(), [b, c, d])
116+
self.assertEqual(c.pack_content(), [a])
117117
with self.assertRaisesRegex(
118118
TclError, """can't pack "?%s"? inside itself""" % (a,)):
119119
a.pack_configure(in_=a)
@@ -223,11 +223,11 @@ def test_pack_forget(self):
223223
a.pack_configure()
224224
b.pack_configure()
225225
c.pack_configure()
226-
self.assertEqual(pack.pack_slaves(), [a, b, c])
226+
self.assertEqual(pack.pack_content(), [a, b, c])
227227
b.pack_forget()
228-
self.assertEqual(pack.pack_slaves(), [a, c])
228+
self.assertEqual(pack.pack_content(), [a, c])
229229
b.pack_forget()
230-
self.assertEqual(pack.pack_slaves(), [a, c])
230+
self.assertEqual(pack.pack_content(), [a, c])
231231
d.pack_forget()
232232

233233
def test_pack_info(self):
@@ -273,6 +273,14 @@ def test_pack_propagate(self):
273273
self.assertEqual(pack.winfo_reqwidth(), 20)
274274
self.assertEqual(pack.winfo_reqheight(), 40)
275275

276+
def test_pack_content(self):
277+
pack, a, b, c, d = self.create2()
278+
self.assertEqual(pack.pack_content(), [])
279+
a.pack_configure()
280+
self.assertEqual(pack.pack_content(), [a])
281+
b.pack_configure()
282+
self.assertEqual(pack.pack_content(), [a, b])
283+
276284
def test_pack_slaves(self):
277285
pack, a, b, c, d = self.create2()
278286
self.assertEqual(pack.pack_slaves(), [])
@@ -477,6 +485,15 @@ def test_place_info(self):
477485
with self.assertRaises(TypeError):
478486
f2.place_info(0)
479487

488+
def test_place_content(self):
489+
foo = tkinter.Frame(self.root)
490+
bar = tkinter.Frame(self.root)
491+
self.assertEqual(foo.place_content(), [])
492+
bar.place_configure(in_=foo)
493+
self.assertEqual(foo.place_content(), [bar])
494+
with self.assertRaises(TypeError):
495+
foo.place_content(0)
496+
480497
def test_place_slaves(self):
481498
foo = tkinter.Frame(self.root)
482499
bar = tkinter.Frame(self.root)
@@ -729,10 +746,10 @@ def test_grid_forget(self):
729746
c = tkinter.Button(self.root)
730747
b.grid_configure(row=2, column=2, rowspan=2, columnspan=2,
731748
padx=3, pady=4, sticky='ns')
732-
self.assertEqual(self.root.grid_slaves(), [b])
749+
self.assertEqual(self.root.grid_content(), [b])
733750
b.grid_forget()
734751
c.grid_forget()
735-
self.assertEqual(self.root.grid_slaves(), [])
752+
self.assertEqual(self.root.grid_content(), [])
736753
self.assertEqual(b.grid_info(), {})
737754
b.grid_configure(row=0, column=0)
738755
info = b.grid_info()
@@ -749,10 +766,10 @@ def test_grid_remove(self):
749766
c = tkinter.Button(self.root)
750767
b.grid_configure(row=2, column=2, rowspan=2, columnspan=2,
751768
padx=3, pady=4, sticky='ns')
752-
self.assertEqual(self.root.grid_slaves(), [b])
769+
self.assertEqual(self.root.grid_content(), [b])
753770
b.grid_remove()
754771
c.grid_remove()
755-
self.assertEqual(self.root.grid_slaves(), [])
772+
self.assertEqual(self.root.grid_content(), [])
756773
self.assertEqual(b.grid_info(), {})
757774
b.grid_configure(row=0, column=0)
758775
info = b.grid_info()
@@ -887,6 +904,23 @@ def test_grid_size(self):
887904
f.grid_configure(row=4, column=5)
888905
self.assertEqual(self.root.grid_size(), (6, 5))
889906

907+
def test_grid_content(self):
908+
self.assertEqual(self.root.grid_content(), [])
909+
a = tkinter.Label(self.root)
910+
a.grid_configure(row=0, column=1)
911+
b = tkinter.Label(self.root)
912+
b.grid_configure(row=1, column=0)
913+
c = tkinter.Label(self.root)
914+
c.grid_configure(row=1, column=1)
915+
d = tkinter.Label(self.root)
916+
d.grid_configure(row=1, column=1)
917+
self.assertEqual(self.root.grid_content(), [d, c, b, a])
918+
self.assertEqual(self.root.grid_content(row=0), [a])
919+
self.assertEqual(self.root.grid_content(row=1), [d, c, b])
920+
self.assertEqual(self.root.grid_content(column=0), [b])
921+
self.assertEqual(self.root.grid_content(column=1), [d, c, a])
922+
self.assertEqual(self.root.grid_content(row=1, column=1), [d, c])
923+
890924
def test_grid_slaves(self):
891925
self.assertEqual(self.root.grid_slaves(), [])
892926
a = tkinter.Label(self.root)

Lib/tkinter/__init__.py

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,19 +1885,41 @@ def pack_propagate(self, flag=_noarg_):
18851885

18861886
propagate = pack_propagate
18871887

1888-
def pack_slaves(self):
1888+
def pack_content(self):
18891889
"""Returns a list of all of the content widgets in the packing order
18901890
for this container."""
1891+
try:
1892+
res = self.tk.call('pack', 'content', self._w)
1893+
except TclError:
1894+
if self.info_patchlevel() >= (8, 6):
1895+
raise
1896+
res = self.tk.call('pack', 'slaves', self._w)
1897+
return [self._nametowidget(x) for x in self.tk.splitlist(res)]
1898+
1899+
content = pack_content
1900+
1901+
def pack_slaves(self):
1902+
"""Synonym for pack_content()."""
18911903
return [self._nametowidget(x) for x in
18921904
self.tk.splitlist(
18931905
self.tk.call('pack', 'slaves', self._w))]
18941906

18951907
slaves = pack_slaves
18961908

18971909
# Place method that applies to the container widget
1898-
def place_slaves(self):
1910+
def place_content(self):
18991911
"""Returns a list of all the content widgets for which this widget is
19001912
the container."""
1913+
try:
1914+
res = self.tk.call('place', 'content', self._w)
1915+
except TclError:
1916+
if self.info_patchlevel() >= (8, 6):
1917+
raise
1918+
res = self.tk.call('place', 'slaves', self._w)
1919+
return [self._nametowidget(x) for x in self.tk.splitlist(res)]
1920+
1921+
def place_slaves(self):
1922+
"""Synonym for place_content()."""
19011923
return [self._nametowidget(x) for x in
19021924
self.tk.splitlist(
19031925
self.tk.call(
@@ -2018,7 +2040,7 @@ def grid_size(self):
20182040

20192041
size = grid_size
20202042

2021-
def grid_slaves(self, row=None, column=None):
2043+
def grid_content(self, row=None, column=None):
20222044
"""Returns a list of the content widgets.
20232045
20242046
If no arguments are supplied, a list of all of the content in this
@@ -2027,6 +2049,21 @@ def grid_slaves(self, row=None, column=None):
20272049
column is returned.
20282050
"""
20292051
args = ()
2052+
if row is not None:
2053+
args = args + ('-row', row)
2054+
if column is not None:
2055+
args = args + ('-column', column)
2056+
try:
2057+
res = self.tk.call('grid', 'content', self._w, *args)
2058+
except TclError:
2059+
if self.info_patchlevel() >= (8, 6):
2060+
raise
2061+
res = self.tk.call('grid', 'slaves', self._w, *args)
2062+
return [self._nametowidget(x) for x in self.tk.splitlist(res)]
2063+
2064+
def grid_slaves(self, row=None, column=None):
2065+
"""Synonym for grid_content()."""
2066+
args = ()
20302067
if row is not None:
20312068
args = args + ('-row', row)
20322069
if column is not None:
@@ -2641,6 +2678,7 @@ def pack_info(self):
26412678

26422679
info = pack_info
26432680
propagate = pack_propagate = Misc.pack_propagate
2681+
content = pack_content = Misc.pack_content
26442682
slaves = pack_slaves = Misc.pack_slaves
26452683

26462684

@@ -2698,6 +2736,7 @@ def place_info(self):
26982736
return d
26992737

27002738
info = place_info
2739+
content = place_content = Misc.place_content
27012740
slaves = place_slaves = Misc.place_slaves
27022741

27032742

@@ -2753,6 +2792,7 @@ def grid_info(self):
27532792
propagate = grid_propagate = Misc.grid_propagate
27542793
rowconfigure = grid_rowconfigure = Misc.grid_rowconfigure
27552794
size = grid_size = Misc.grid_size
2795+
content = grid_content = Misc.grid_content
27562796
slaves = grid_slaves = Misc.grid_slaves
27572797

27582798

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Add new :mod:`tkinter` widget methods :meth:`!pack_content`,
2+
:meth:`!place_content` and :meth:`!grid_content` which are alternative
3+
spelling of old :meth:`!*_slaves` methods.

0 commit comments

Comments
 (0)