Python on Windows: How to create accessible label for wx.ListCtrl widget using wxPython?

Viewed 88

In Python 3.10.5 on Windows 11 using the wxPython 4.2.0 package, I am trying to create a labelled wx.ListCtrl widget, where the widget label would be recognised by screen readers, but I cannot find the way to do so. So far, I have the following code:

hbox = wx.BoxSizer(wx.HORIZONTAL)
label = wx.StaticText(self.panel, wx.ID_ANY, 'My label')
hbox.Add(label, 1, wx.EXPAND | wx.ALIGN_LEFT | wx.ALL, 5)
list = wx.ListCtrl(self.panel, wx.ID_ANY)
hbox.Add(list, 1, wx.EXPAND | wx.ALIGN_LEFT | wx.ALL, 5)

If I use the code above, both the label and list are created, but they seem not to be tied together, as when the list widget is focused, screen reader narrates only "List view", ignoring the label. However, if in the same code I replace wx.ListCtrl with wx.ListBox, the label starts to be narrated without an issue. But I need to use wxListCtrl, not wx.Listbox.

How can I label the wx.ListCtrl widget properly from the accessibility perspective, so that when focused with the screen reader, the label for the widget would be narrated?

1 Answers

So there is a workaround for labelling wx.ListCtrl widgets, which is acceptable by me, at least from the screen reader user's point of view. That solution lies in wrapping of the wx.ListCtrl widget with a wx.StaticBox or wx.StaticBoxSizer object like described here. The code then might look like this:

panel = wx.Panel(self)
box = wx.StaticBoxSizer(wx.VERTICAL, panel, 'Your lable for the list goes here')
list = wx.ListCtrl(box.GetStaticBox(), wx.ID_ANY)
box.Add(list)
panel.SetSizer(box)
Related