Zack
Zack

Reputation: 4385

Is it possible to change the background color for item in a wx.ListBox?

I'd like to alternate the background color in my listbox so that it is a little easier to read. Something like this:

enter image description here

However, I can't figure out how to do so. From the docs, I see that the wx.listbox has a method called SetItemBackgroundColor(self, item, colour), but I'm unsure if (a) the item parameter is an index or a wx.ID of some kind, or (b) if that method is actual applicable to the listbox (maybe it was for something higher up in the inheritance tree?)

I've tried passing in an index, along with a color to the method, but it doesn't do anything. So I'm not sure whether it doesn't work because I'm passing in the wrong type of identifier, or if it doesn't work because the method doesn't apply to the object.

If it is a wx.ID that I'm supposed to pass in for the item parameter, how to do get the such information for the listbox instance?

Upvotes: 3

Views: 1756

Answers (1)

tom10
tom10

Reputation: 69192

Setting the background color is not possible with a ListBox. ListCtrl can do this though.

In general, ListBox is for small simple lists (and is therefore quick and easy), and ListCtrl provides much more flexibility (but is also a bit more involved).

For an example of how to make striped backgrounds, see the demo under CoreWindows\ListCtrl_virtual.

Update on the difference between the wx docs (which don't have the SetItemBackgroundColor method), and wxpython docs (which do):
It seems that when the native API can be used, and when it supports some functionality beyond the wx library, access to it is sometime added to the wrapper. That is, use of SetItemBackgroundColor is platform dependent, based on the native API. See this thread for some discussion (particularly, Robin Dunn's entry). When it's not available in the native platform, the command is ignored.

Upvotes: 3

Related Questions