@@ -818,6 +818,25 @@ def tk_inactive(self, reset=False, *, displayof=0):
818818 else :
819819 return self .tk .getint (self .tk .call (args ))
820820
821+ def tk_check_screenreader (self ):
822+ """Return whether a screen reader is currently running.
823+
824+ Requires Tk 9.1 or newer."""
825+ return self .tk .getboolean (self .tk .call (
826+ 'tk' , 'accessible' , 'check_screenreader' ))
827+
828+ @property
829+ def accessible (self ):
830+ """The accessible attributes of the widget, as an Accessible
831+ object.
832+
833+ Requires Tk 9.1 or newer."""
834+ try :
835+ return self ._accessible_object
836+ except AttributeError :
837+ self ._accessible_object = Accessible (self )
838+ return self ._accessible_object
839+
821840 def wait_variable (self , name ):
822841 """Wait until the variable is modified.
823842
@@ -2307,6 +2326,67 @@ def yview_scroll(self, number, what):
23072326 self .tk .call (self ._w , 'yview' , 'scroll' , number , what )
23082327
23092328
2329+ def _accessible_attribute (name ):
2330+ """Internal function. Create an Accessible attribute property."""
2331+ def getter (self ):
2332+ widget = self ._widget
2333+ return widget .tk .call ('tk' , 'accessible' , 'get_acc_' + name ,
2334+ widget ._w )
2335+ def setter (self , value ):
2336+ widget = self ._widget
2337+ widget .tk .call ('tk' , 'accessible' , 'set_acc_' + name ,
2338+ widget ._w , value )
2339+ return property (getter , setter ,
2340+ doc = f'The accessible { name } of the widget.' )
2341+
2342+
2343+ class Accessible :
2344+ """The accessible attributes of a widget.
2345+
2346+ Returned by the accessible property of a widget. The attributes
2347+ are exposed to assistive technologies such as screen readers.
2348+ Requires Tk 9.1 or newer.
2349+
2350+ If no assistive technology is active when the application starts,
2351+ setting an attribute has no effect and reading it returns 0.
2352+ Reading an attribute that has not been set raises TclError.
2353+ """
2354+
2355+ def __init__ (self , widget ):
2356+ self ._widget = widget
2357+
2358+ def __repr__ (self ):
2359+ return f'<Accessible object of { self ._widget !r} >'
2360+
2361+ role = _accessible_attribute ('role' )
2362+ name = _accessible_attribute ('name' )
2363+ description = _accessible_attribute ('description' )
2364+ value = _accessible_attribute ('value' )
2365+ state = _accessible_attribute ('state' )
2366+ action = _accessible_attribute ('action' )
2367+ help = _accessible_attribute ('help' )
2368+
2369+ def add_object (self ):
2370+ """Register the widget with the platform accessibility API."""
2371+ widget = self ._widget
2372+ widget .tk .call ('tk' , 'accessible' , 'add_acc_object' , widget ._w )
2373+
2374+ def emit_selection_change (self ):
2375+ """Notify the platform accessibility API that the selection
2376+ in the widget has changed."""
2377+ widget = self ._widget
2378+ widget .tk .call ('tk' , 'accessible' , 'emit_selection_change' ,
2379+ widget ._w )
2380+
2381+ def emit_focus_change (self ):
2382+ """Notify the platform accessibility API that the widget has
2383+ received focus.
2384+
2385+ Not available on macOS."""
2386+ widget = self ._widget
2387+ widget .tk .call ('tk' , 'accessible' , 'emit_focus_change' , widget ._w )
2388+
2389+
23102390class Wm :
23112391 """Provides functions for the communication with the window manager."""
23122392
0 commit comments