Skip to content

Commit dd8739a

Browse files
gh-153422: Return bool from some tkinter query methods (GH-153429)
The winfo_exists(), winfo_ismapped() and winfo_viewable() methods of tkinter widgets and Text.edit_modified() now return a bool instead of an integer or, depending on wantobjects, a string. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 1d19650 commit dd8739a

9 files changed

Lines changed: 51 additions & 32 deletions

File tree

Doc/library/tkinter.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2066,7 +2066,7 @@ Base and mixin classes
20662066

20672067
.. method:: winfo_exists()
20682068

2069-
Return ``1`` if the widget exists, ``0`` otherwise.
2069+
Return ``True`` if the widget exists, ``False`` otherwise.
20702070

20712071
.. method:: winfo_fpixels(number)
20722072

@@ -2115,7 +2115,7 @@ Base and mixin classes
21152115

21162116
.. method:: winfo_ismapped()
21172117

2118-
Return ``1`` if the widget is currently mapped, ``0`` otherwise.
2118+
Return ``True`` if the widget is currently mapped, ``False`` otherwise.
21192119

21202120
.. method:: winfo_manager()
21212121

@@ -2246,8 +2246,8 @@ Base and mixin classes
22462246

22472247
.. method:: winfo_viewable()
22482248

2249-
Return ``1`` if the widget and all of its ancestors up through the
2250-
nearest toplevel window are mapped, ``0`` otherwise.
2249+
Return ``True`` if the widget and all of its ancestors up through the
2250+
nearest toplevel window are mapped, ``False`` otherwise.
22512251

22522252
.. method:: winfo_visual()
22532253

@@ -5737,7 +5737,7 @@ Widget classes
57375737
.. method:: edit_modified(arg=None)
57385738

57395739
If *arg* is omitted, return the current state of the modified flag as
5740-
``0`` or ``1``; the flag is set automatically whenever the text is
5740+
a :class:`bool`; the flag is set automatically whenever the text is
57415741
inserted or deleted.
57425742
Otherwise set the flag to the boolean *arg*.
57435743

Doc/library/tkinter.ttk.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,7 @@ ttk.Treeview
13721372
Without arguments, returns a tuple of all detached items,
13731373
but not their descendants (see :meth:`detached_all`).
13741374
With *item*, returns whether *item* is detached; since Tk 9.1, also
1375-
returns true if an ancestor of *item* is detached.
1375+
returns ``True`` if an ancestor of *item* is detached.
13761376

13771377
Requires Tk 9.0 or newer.
13781378

Lib/test/test_tkinter/test_images.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -694,12 +694,12 @@ def test_data(self):
694694

695695
def test_transparency(self):
696696
image = self.create()
697-
self.assertEqual(image.transparency_get(0, 0), True)
698-
self.assertEqual(image.transparency_get(4, 6), False)
697+
self.assertIs(image.transparency_get(0, 0), True)
698+
self.assertIs(image.transparency_get(4, 6), False)
699699
image.transparency_set(4, 6, True)
700-
self.assertEqual(image.transparency_get(4, 6), True)
700+
self.assertIs(image.transparency_get(4, 6), True)
701701
image.transparency_set(4, 6, False)
702-
self.assertEqual(image.transparency_get(4, 6), False)
702+
self.assertIs(image.transparency_get(4, 6), False)
703703
self.assertRaises(tkinter.TclError, image.transparency_get, -1, 0)
704704
self.assertRaises(tkinter.TclError, image.transparency_get, 16, 0)
705705
self.assertRaises(tkinter.TclError, image.transparency_set, -1, 0, True)

Lib/test/test_tkinter/test_misc.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,13 +905,26 @@ def test_winfo_visual_info(self):
905905
self.assertIsInstance(name, str)
906906
self.assertIsInstance(depth, int)
907907

908+
def test_winfo_exists(self):
909+
f = tkinter.Frame(self.root)
910+
self.assertIs(f.winfo_exists(), True)
911+
f.destroy()
912+
self.assertIs(f.winfo_exists(), False)
913+
914+
def test_winfo_ismapped(self):
915+
f = tkinter.Frame(self.root)
916+
self.assertIs(f.winfo_ismapped(), False)
917+
f.pack()
918+
self.root.update()
919+
self.assertIs(f.winfo_ismapped(), True)
920+
908921
def test_winfo_viewable(self):
909922
f = tkinter.Frame(self.root)
910-
self.assertFalse(f.winfo_viewable())
923+
self.assertIs(f.winfo_viewable(), False)
911924
f.pack()
912925
f.wait_visibility()
913926
self.root.update()
914-
self.assertTrue(f.winfo_viewable())
927+
self.assertIs(f.winfo_viewable(), True)
915928

916929
@requires_tk(9, 1)
917930
def test_winfo_isdark(self):

Lib/test/test_tkinter/test_text.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ def test_debug(self):
1818
text = self.text
1919
olddebug = text.debug()
2020
try:
21-
text.debug(0)
22-
self.assertEqual(text.debug(), 0)
23-
text.debug(1)
24-
self.assertEqual(text.debug(), 1)
21+
text.debug(False)
22+
self.assertIs(text.debug(), False)
23+
text.debug(True)
24+
self.assertIs(text.debug(), True)
2525
finally:
2626
text.debug(olddebug)
2727
self.assertEqual(text.debug(), olddebug)
@@ -293,13 +293,13 @@ def test_tag_cget_configure(self):
293293

294294
def test_edit_modified(self):
295295
text = self.text
296-
self.assertEqual(text.edit_modified(), 0)
296+
self.assertIs(text.edit_modified(), False)
297297
text.insert('1.0', 'spam')
298-
self.assertEqual(text.edit_modified(), 1)
298+
self.assertIs(text.edit_modified(), True)
299299
text.edit_modified(False)
300-
self.assertEqual(text.edit_modified(), 0)
300+
self.assertIs(text.edit_modified(), False)
301301
text.edit_modified(True)
302-
self.assertEqual(text.edit_modified(), 1)
302+
self.assertIs(text.edit_modified(), True)
303303

304304
def test_edit_undo_redo(self):
305305
text = self.text

Lib/test/test_ttk/test_widgets.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,9 +1685,9 @@ def test_detach_reattach(self):
16851685
self.assertEqual(self.tv.get_children(item_id), ())
16861686

16871687
def test_exists(self):
1688-
self.assertEqual(self.tv.exists('something'), False)
1689-
self.assertEqual(self.tv.exists(''), True)
1690-
self.assertEqual(self.tv.exists({}), False)
1688+
self.assertIs(self.tv.exists('something'), False)
1689+
self.assertIs(self.tv.exists(''), True)
1690+
self.assertIs(self.tv.exists({}), False)
16911691

16921692
# the following will make a tk.call equivalent to
16931693
# tk.call(treeview, "exists") which should result in an error

Lib/tkinter/__init__.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1332,8 +1332,8 @@ def winfo_depth(self):
13321332
return self.tk.getint(self.tk.call('winfo', 'depth', self._w))
13331333

13341334
def winfo_exists(self):
1335-
"""Return true if this widget exists."""
1336-
return self.tk.getint(
1335+
"""Return True if this widget exists."""
1336+
return self.tk.getboolean(
13371337
self.tk.call('winfo', 'exists', self._w))
13381338

13391339
def winfo_fpixels(self, number):
@@ -1370,8 +1370,8 @@ def winfo_isdark(self): # new in Tk 9.1
13701370
self.tk.call('winfo', 'isdark', self._w))
13711371

13721372
def winfo_ismapped(self):
1373-
"""Return true if this widget is mapped."""
1374-
return self.tk.getint(
1373+
"""Return True if this widget is mapped."""
1374+
return self.tk.getboolean(
13751375
self.tk.call('winfo', 'ismapped', self._w))
13761376

13771377
def winfo_manager(self):
@@ -1498,8 +1498,8 @@ def winfo_toplevel(self):
14981498
'winfo', 'toplevel', self._w))
14991499

15001500
def winfo_viewable(self):
1501-
"""Return true if the widget and all its higher ancestors are mapped."""
1502-
return self.tk.getint(
1501+
"""Return True if the widget and all its higher ancestors are mapped."""
1502+
return self.tk.getboolean(
15031503
self.tk.call('winfo', 'viewable', self._w))
15041504

15051505
def winfo_visual(self):
@@ -4207,6 +4207,8 @@ def edit_modified(self, arg=None):
42074207
modified flag. If boolean is specified, sets the
42084208
modified flag of the widget to arg.
42094209
"""
4210+
if arg is None:
4211+
return self.tk.getboolean(self.edit("modified"))
42104212
return self.edit("modified", arg)
42114213

42124214
def edit_redo(self):

Lib/tkinter/ttk.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,7 +1604,7 @@ def detached(self, item=None):
16041604
16051605
Without arguments, return a tuple of all detached items (but not
16061606
their descendants; see detached_all). With item, return whether item
1607-
is detached; since Tk 9.1, also return true if an ancestor of item
1607+
is detached; since Tk 9.1, also return True if an ancestor of item
16081608
is detached.
16091609
16101610
* Availability: Tk 9.0"""
@@ -2031,8 +2031,8 @@ def tag_configure(self, tagname, option=None, **kw):
20312031

20322032

20332033
def tag_has(self, tagname, item=None):
2034-
"""If item is specified, returns 1 or 0 depending on whether the
2035-
specified item has the given tagname. Otherwise, returns a list of
2034+
"""If item is specified, returns True if the specified item has the
2035+
given tagname, False otherwise. Otherwise, returns a list of
20362036
all items which have the specified tag.
20372037
20382038
* Availability: Tk 8.6"""
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:meth:`!winfo_exists`, :meth:`!winfo_ismapped` and :meth:`!winfo_viewable`
2+
methods of :mod:`tkinter` widgets and :meth:`!edit_modified` of
3+
:class:`tkinter.Text` now return a :class:`bool` instead of an integer or,
4+
depending on ``wantobjects``, a string.

0 commit comments

Comments
 (0)