Skip to content

Commit ecab19d

Browse files
gh-145578: Add tkinter support for the Tk 9.1 accessibility API (GH-153247)
The accessible property of widgets returns an Accessible object whose attributes are exposed to assistive technologies such as screen readers and whose methods register custom widgets with the platform accessibility API. The tk_check_screenreader() method reports whether a screen reader is running. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent b05fe37 commit ecab19d

5 files changed

Lines changed: 208 additions & 0 deletions

File tree

‎Doc/library/tkinter.rst‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2023,6 +2023,31 @@ Base and mixin classes
20232023

20242024
.. versionadded:: 3.16
20252025

2026+
The following members support assistive technologies such as screen
2027+
readers.
2028+
They require Tk 9.1 or newer.
2029+
Tk automatically exposes the standard widgets to the platform
2030+
accessibility API;
2031+
the :attr:`!accessible` attributes allow overriding what is exposed,
2032+
and the :class:`Accessible` methods allow registering custom widgets.
2033+
If no assistive technology is active when the application starts,
2034+
setting the attributes has no effect,
2035+
reading them returns ``0``,
2036+
and the methods do nothing (except :meth:`!tk_check_screenreader`).
2037+
2038+
.. method:: tk_check_screenreader()
2039+
2040+
Return whether a screen reader is currently running.
2041+
2042+
.. versionadded:: next
2043+
2044+
.. attribute:: accessible
2045+
2046+
The accessible attributes of the widget,
2047+
as an :class:`Accessible` object.
2048+
2049+
.. versionadded:: next
2050+
20262051
The methods with the ``busy_`` prefix manage the busy state of a window,
20272052
which shows a busy cursor and ignores user input.
20282053

@@ -2455,6 +2480,55 @@ Base and mixin classes
24552480
.. versionadded:: 3.11
24562481

24572482

2483+
.. class:: Accessible()
2484+
2485+
The accessible attributes of a widget,
2486+
returned by the :attr:`~Misc.accessible` property of the widget.
2487+
The attributes are exposed to assistive technologies
2488+
such as screen readers.
2489+
Requires Tk 9.1 or newer.
2490+
2491+
Setting an attribute overrides what Tk exposes for the widget;
2492+
reading an attribute that has not been set raises
2493+
:exc:`~tkinter.TclError`.
2494+
A widget must have a role before any other attribute is set or read,
2495+
otherwise :exc:`~tkinter.TclError` is raised.
2496+
Tk sets the role of most standard widgets when they are mapped;
2497+
for other widgets, or before that, set :attr:`!role` first.
2498+
The supported roles are ``Button``, ``Canvas``, ``Checkbutton``,
2499+
``Combobox``, ``Entry``, ``Label``, ``Listbox``, ``Menu``,
2500+
``Notebook``, ``Progressbar``, ``Scale``, ``Scrollbar``,
2501+
``Spinbox``, ``Table``, ``Text`` and ``Tree``.
2502+
2503+
.. versionadded:: next
2504+
2505+
.. attribute:: role
2506+
name
2507+
description
2508+
value
2509+
state
2510+
action
2511+
help
2512+
2513+
The accessible attributes of the widget.
2514+
2515+
.. method:: add_object()
2516+
2517+
Register the widget with the platform accessibility API.
2518+
This is only needed for custom widgets;
2519+
the standard widgets are registered automatically.
2520+
2521+
.. method:: emit_selection_change()
2522+
2523+
Notify the platform accessibility API that the selection in the
2524+
widget has changed.
2525+
2526+
.. method:: emit_focus_change()
2527+
2528+
Notify the platform accessibility API that the widget has received
2529+
focus.
2530+
Not available on macOS.
2531+
24582532

24592533
.. class:: Wm()
24602534

‎Doc/whatsnew/3.16.rst‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -893,6 +893,15 @@ tkinter
893893
native print dialog. It requires Tk 8.7/9.0 or newer.
894894
(Contributed by Serhiy Storchaka in :gh:`153256`.)
895895

896+
* Added :mod:`tkinter` support for the Tk 9.1 accessibility API: the
897+
:attr:`~tkinter.Misc.accessible` property of widgets returns an
898+
:class:`~tkinter.Accessible` object whose attributes are exposed to
899+
assistive technologies such as screen readers and whose methods register
900+
custom widgets with the platform accessibility API, and the
901+
:meth:`~tkinter.Misc.tk_check_screenreader` method reports whether a
902+
screen reader is running.
903+
(Contributed by Serhiy Storchaka in :gh:`145578`.)
904+
896905
* Added new window-management methods :meth:`~tkinter.Misc.winfo_isdark`
897906
(dark mode detection), :meth:`~tkinter.Wm.wm_iconbadge` (application icon
898907
badge) and :meth:`~tkinter.Wm.wm_stackorder` (toplevel stacking order).

‎Lib/test/test_tkinter/test_misc.py‎

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,43 @@ def test_tk_print(self):
712712
self.assertNotHasAttr(tkinter.Frame, 'tk_print')
713713
self.assertNotHasAttr(tkinter.Misc, 'tk_print')
714714

715+
@requires_tk(9, 1)
716+
def test_accessible(self):
717+
# Without an active screen reader every "tk accessible" call is
718+
# a no-op returning 0, so only test that the calls do not raise
719+
# and check the values when a screen reader is active.
720+
try:
721+
sr = self.root.tk_check_screenreader()
722+
except tkinter.TclError:
723+
# Tk 9.1a0 or Tk compiled without accessibility support
724+
# (e.g. without ATK on X11).
725+
self.skipTest('the "tk accessible" command is not supported')
726+
self.assertIsInstance(sr, bool)
727+
f = tkinter.Frame(self.root)
728+
acc = f.accessible
729+
self.assertIsInstance(acc, tkinter.Accessible)
730+
self.assertIs(f.accessible, acc)
731+
# The role has to be set first, it creates the table in which
732+
# other attributes are stored.
733+
acc.role = 'Label'
734+
result = acc.role
735+
if sr:
736+
self.assertEqual(result, 'Label')
737+
# Reading an attribute that has not been set raises TclError.
738+
with self.assertRaises(tkinter.TclError):
739+
acc.help
740+
for attr in ('name', 'description', 'value', 'state',
741+
'action', 'help'):
742+
with self.subTest(attr=attr):
743+
setattr(acc, attr, 'spam')
744+
result = getattr(acc, attr)
745+
if sr:
746+
self.assertEqual(result, 'spam')
747+
acc.add_object()
748+
acc.emit_selection_change()
749+
if self.root._windowingsystem != 'aqua':
750+
acc.emit_focus_change()
751+
715752
def test_wait_variable(self):
716753
var = tkinter.StringVar(self.root)
717754
self.assertEqual(self.root.waitvar, self.root.wait_variable)

‎Lib/tkinter/__init__.py‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,25 @@ def tk_inactive(self, reset=False, *, displayof=0):
821821
else:
822822
return self.tk.getint(self.tk.call(args))
823823

824+
def tk_check_screenreader(self):
825+
"""Return whether a screen reader is currently running.
826+
827+
Requires Tk 9.1 or newer."""
828+
return self.tk.getboolean(self.tk.call(
829+
'tk', 'accessible', 'check_screenreader'))
830+
831+
@property
832+
def accessible(self):
833+
"""The accessible attributes of the widget, as an Accessible
834+
object.
835+
836+
Requires Tk 9.1 or newer."""
837+
try:
838+
return self._accessible_object
839+
except AttributeError:
840+
self._accessible_object = Accessible(self)
841+
return self._accessible_object
842+
824843
def wait_variable(self, name):
825844
"""Wait until the variable is modified.
826845
@@ -2310,6 +2329,68 @@ def yview_scroll(self, number, what):
23102329
self.tk.call(self._w, 'yview', 'scroll', number, what)
23112330

23122331

2332+
def _accessible_attribute(name):
2333+
"""Internal function. Create an Accessible attribute property."""
2334+
def getter(self):
2335+
widget = self._widget
2336+
return widget.tk.call('tk', 'accessible', 'get_acc_' + name,
2337+
widget._w)
2338+
def setter(self, value):
2339+
widget = self._widget
2340+
widget.tk.call('tk', 'accessible', 'set_acc_' + name,
2341+
widget._w, value)
2342+
return property(getter, setter,
2343+
doc=f'The accessible {name} of the widget.')
2344+
2345+
2346+
class Accessible:
2347+
"""The accessible attributes of a widget.
2348+
2349+
Returned by the accessible property of a widget. The attributes
2350+
are exposed to assistive technologies such as screen readers.
2351+
Requires Tk 9.1 or newer.
2352+
2353+
If no assistive technology is active when the application starts,
2354+
setting an attribute has no effect and reading it returns 0.
2355+
Reading an attribute that has not been set raises TclError.
2356+
A widget must have a role before other attributes can be used.
2357+
"""
2358+
2359+
def __init__(self, widget):
2360+
self._widget = widget
2361+
2362+
def __repr__(self):
2363+
return f'<Accessible object of {self._widget!r}>'
2364+
2365+
role = _accessible_attribute('role')
2366+
name = _accessible_attribute('name')
2367+
description = _accessible_attribute('description')
2368+
value = _accessible_attribute('value')
2369+
state = _accessible_attribute('state')
2370+
action = _accessible_attribute('action')
2371+
help = _accessible_attribute('help')
2372+
2373+
def add_object(self):
2374+
"""Register the widget with the platform accessibility API."""
2375+
widget = self._widget
2376+
widget.tk.call('tk', 'accessible', 'add_acc_object', widget._w)
2377+
2378+
def emit_selection_change(self):
2379+
"""Notify the platform accessibility API that the selection
2380+
in the widget has changed."""
2381+
widget = self._widget
2382+
widget.tk.call('tk', 'accessible', 'emit_selection_change',
2383+
widget._w)
2384+
2385+
def emit_focus_change(self):
2386+
"""Notify the platform accessibility API that the widget has
2387+
received focus.
2388+
2389+
Not available on macOS."""
2390+
widget = self._widget
2391+
widget.tk.call('tk', 'accessible', 'emit_focus_change', widget._w)
2392+
2393+
23132394
class Wm:
23142395
"""Provides functions for the communication with the window manager."""
23152396

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Added :mod:`tkinter` support for the Tk 9.1 accessibility API: the
2+
:attr:`~tkinter.Misc.accessible` property of widgets returns an
3+
:class:`~tkinter.Accessible` object whose attributes are exposed to
4+
assistive technologies such as screen readers and whose methods register
5+
custom widgets with the platform accessibility API, and the
6+
:meth:`~tkinter.Misc.tk_check_screenreader` method reports whether a
7+
screen reader is running.

0 commit comments

Comments
 (0)