ListCtrl SetHeaderAttr method seems to be missing in 4.1.1

Using wxPython 4.1.1 on Windows 10 with Python 3.9.4

I’ve created a simple ListCtrl in REPORT mode, and I’m trying to set the header attributes (to use bold font) with the “SetHeaderAttr” method as listed in the wxPython 4.1.1 API documentation for wx.ListCtrl. Here’s the code:

# Testing ListCtrl

import wx

class MyPanel(wx.Panel):
    def __init__(self, parent):
        # Define some rows of data
        row1 = [355, 'Client', 'Zingall']
        row2 = [1, 'ShipTo', 'Smith']
        row3 = [55536, 'B2B', 'Summer Supplies']
        # create a listctrl
        self.list = wx.ListCtrl(self, -1,
                                style = wx.LC_REPORT
                                | wx.LC_HRULES
                                | wx.LC_VRULES
                                | wx.LC_SINGLE_SEL)
        # attribute = (textcolor, backgroundcolor, font)
        listfont = self.list.GetFont()
        headfont = listfont.MakeBold()
        headAttr = ((0,0,0), (240,240,240), headfont)
        layout = wx.BoxSizer(wx.VERTICAL)
        layout.Add(self.list, 1, wx.EXPAND)

class MyFrame(wx.Frame):
    def __init__(self, parent, title=""):
        super().__init__(parent, title=title) 
        self.SetSize((640, 480))
        # Set the panel
        self.panel = MyPanel(self)
class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, title='ListCtrl Testing')
        return True
if __name__ == "__main__":
    app = MyApp(False)

I believe this should work, but it fails with:

AttributeError: 'ListCtrl' object has no attribute 'SetHeaderAttr'

The API says this method is new in 4.1, but it doesn’t seem to be there. Am I missing something or is the method missing? Any ideas?


there is a TypeError of your argument to the method: it likes wx.ItemAttr :cowboy_hat_face:

OK, yes you are right. But this still doesn’t explain why the ListCtrl object reports it has no SetHeaderAttr method. In fact, the wx.ItemAttr does not exist either, even though the documentation says it is. Did you actually try to run a corrected version of the above? (1.6 KB)

Thank you!

Now here’s a funny thing: My test code was initially EXACTLY like what you sent me but it was still failing with the same “ListCtrl does not have an attribute SetHeaderAttr”. For about 45 minutes I’m pulling my hair out comparing line by line to see where I was going wrong but they were identical.

Then I remembered that I was running my version from a folder in my Eclipse IDE that by default was using an older Python 3.6 environment on my system with wxPython 4.0. DUH!! No wonder it couldn’t find SetHeaderAttr or ItemAttr. I had installed your version into my current projects folder in Eclipse which is using Python 3.9.4 and wxPython 4.1.1 so of course it ran perfectly.

So my lesson learned here is that while it is quite possible to have multiple Python environments on your machine and in your IDE, there is a downside if you aren’t careful!!

Now I just need to figure out why the “background” color value in the ItemAttr seems to be ignored…

a control in wx is using the windowing system of the OS and tries its best; but if some functionality isn’t supported by the channel then, instead of going into a fit it will quietly be ignored (robustness) so that it doesn’t disturb the ‘native feeling’ (if there are any) :thinking: