@@ -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+
23132394class Wm :
23142395 """Provides functions for the communication with the window manager."""
23152396
0 commit comments