Skip to content

Commit 92374bb

Browse files
gh-143754: Add Tkinter methods pack_content(), place_content() and grid_content()
They use Tk commands with new name like "pack content instead of old "pack slaves".
1 parent cd9ede3 commit 92374bb

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
@@ -39,11 +39,11 @@ def test_pack_configure_after(self):
3939
b.pack_configure(side='top')
4040
c.pack_configure(side='top')
4141
d.pack_configure(side='top')
42-
self.assertEqual(pack.pack_slaves(), [a, b, c, d])
42+
self.assertEqual(pack.pack_content(), [a, b, c, d])
4343
a.pack_configure(after=b)
44-
self.assertEqual(pack.pack_slaves(), [b, a, c, d])
44+
self.assertEqual(pack.pack_content(), [b, a, c, d])
4545
a.pack_configure(after=a)
46-
self.assertEqual(pack.pack_slaves(), [b, a, c, d])
46+
self.assertEqual(pack.pack_content(), [b, a, c, d])
4747

4848
def test_pack_configure_anchor(self):
4949
pack, a, b, c, d = self.create2()
@@ -72,11 +72,11 @@ def test_pack_configure_before(self):
7272
b.pack_configure(side='top')
7373
c.pack_configure(side='top')
7474
d.pack_configure(side='top')
75-
self.assertEqual(pack.pack_slaves(), [a, b, c, d])
75+
self.assertEqual(pack.pack_content(), [a, b, c, d])
7676
a.pack_configure(before=d)
77-
self.assertEqual(pack.pack_slaves(), [b, c, a, d])
77+
self.assertEqual(pack.pack_content(), [b, c, a, d])
7878
a.pack_configure(before=a)
79-
self.assertEqual(pack.pack_slaves(), [b, c, a, d])
79+
self.assertEqual(pack.pack_content(), [b, c, a, d])
8080

8181
def test_pack_configure_expand(self):
8282
pack, a, b, c, d = self.create2()
@@ -109,10 +109,10 @@ def test_pack_configure_in(self):
109109
c.pack_configure(side='top')
110110
d.pack_configure(side='top')
111111
a.pack_configure(in_=pack)
112-
self.assertEqual(pack.pack_slaves(), [b, c, d, a])
112+
self.assertEqual(pack.pack_content(), [b, c, d, a])
113113
a.pack_configure(in_=c)
114-
self.assertEqual(pack.pack_slaves(), [b, c, d])
115-
self.assertEqual(c.pack_slaves(), [a])
114+
self.assertEqual(pack.pack_content(), [b, c, d])
115+
self.assertEqual(c.pack_content(), [a])
116116
with self.assertRaisesRegex(
117117
TclError, """can't pack "?%s"? inside itself""" % (a,)):
118118
a.pack_configure(in_=a)
@@ -222,11 +222,11 @@ def test_pack_forget(self):
222222
a.pack_configure()
223223
b.pack_configure()
224224
c.pack_configure()
225-
self.assertEqual(pack.pack_slaves(), [a, b, c])
225+
self.assertEqual(pack.pack_content(), [a, b, c])
226226
b.pack_forget()
227-
self.assertEqual(pack.pack_slaves(), [a, c])
227+
self.assertEqual(pack.pack_content(), [a, c])
228228
b.pack_forget()
229-
self.assertEqual(pack.pack_slaves(), [a, c])
229+
self.assertEqual(pack.pack_content(), [a, c])
230230
d.pack_forget()
231231

232232
def test_pack_info(self):
@@ -272,6 +272,14 @@ def test_pack_propagate(self):
272272
self.assertEqual(pack.winfo_reqwidth(), 20)
273273
self.assertEqual(pack.winfo_reqheight(), 40)
274274

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

487+
def test_place_content(self):
488+
foo = tkinter.Frame(self.root)
489+
bar = tkinter.Frame(self.root)
490+
self.assertEqual(foo.place_content(), [])
491+
bar.place_configure(in_=foo)
492+
self.assertEqual(foo.place_content(), [bar])
493+
with self.assertRaises(TypeError):
494+
foo.place_content(0)
495+
479496
def test_place_slaves(self):
480497
foo = tkinter.Frame(self.root)
481498
bar = tkinter.Frame(self.root)
@@ -728,10 +745,10 @@ def test_grid_forget(self):
728745
c = tkinter.Button(self.root)
729746
b.grid_configure(row=2, column=2, rowspan=2, columnspan=2,
730747
padx=3, pady=4, sticky='ns')
731-
self.assertEqual(self.root.grid_slaves(), [b])
748+
self.assertEqual(self.root.grid_content(), [b])
732749
b.grid_forget()
733750
c.grid_forget()
734-
self.assertEqual(self.root.grid_slaves(), [])
751+
self.assertEqual(self.root.grid_content(), [])
735752
self.assertEqual(b.grid_info(), {})
736753
b.grid_configure(row=0, column=0)
737754
info = b.grid_info()
@@ -748,10 +765,10 @@ def test_grid_remove(self):
748765
c = tkinter.Button(self.root)
749766
b.grid_configure(row=2, column=2, rowspan=2, columnspan=2,
750767
padx=3, pady=4, sticky='ns')
751-
self.assertEqual(self.root.grid_slaves(), [b])
768+
self.assertEqual(self.root.grid_content(), [b])
752769
b.grid_remove()
753770
c.grid_remove()
754-
self.assertEqual(self.root.grid_slaves(), [])
771+
self.assertEqual(self.root.grid_content(), [])
755772
self.assertEqual(b.grid_info(), {})
756773
b.grid_configure(row=0, column=0)
757774
info = b.grid_info()
@@ -886,6 +903,23 @@ def test_grid_size(self):
886903
f.grid_configure(row=4, column=5)
887904
self.assertEqual(self.root.grid_size(), (6, 5))
888905

906+
def test_grid_content(self):
907+
self.assertEqual(self.root.grid_content(), [])
908+
a = tkinter.Label(self.root)
909+
a.grid_configure(row=0, column=1)
910+
b = tkinter.Label(self.root)
911+
b.grid_configure(row=1, column=0)
912+
c = tkinter.Label(self.root)
913+
c.grid_configure(row=1, column=1)
914+
d = tkinter.Label(self.root)
915+
d.grid_configure(row=1, column=1)
916+
self.assertEqual(self.root.grid_content(), [d, c, b, a])
917+
self.assertEqual(self.root.grid_content(row=0), [a])
918+
self.assertEqual(self.root.grid_content(row=1), [d, c, b])
919+
self.assertEqual(self.root.grid_content(column=0), [b])
920+
self.assertEqual(self.root.grid_content(column=1), [d, c, a])
921+
self.assertEqual(self.root.grid_content(row=1, column=1), [d, c])
922+
889923
def test_grid_slaves(self):
890924
self.assertEqual(self.root.grid_slaves(), [])
891925
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)