wxpython: ListBox object has no attribute 'GetItems'
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
name_list = self.listbox.GetItems()
AttributeError: 'ListBox' object has no attribute 'GetItems'
which is pretty standard and get the error in the subject. There is supposed to be a Method GetItems in Python, in wx, but I don't get it why I am getting this error Message.
Why didn't you just have a look at the code?
def sort_buttonClick(self, event):
"""sort the items in the listbox"""
# GetItems() is new in wxPython2.8
# puts the listbox items into a list
name_list = self.listbox.GetItems()
# Set() clears and reloads the listbox
Is there any other function before 2.8 that will return the selected (string) list, instead of numbers?
But it isn't too difficult to make one.
Here's a complete working example:
# Creating a new ListBox class that supports the GetItems() method
# like in wx version 2.8
class OurSlightlyEnhancedListBox(wx.ListBox): # inheriting wx.ListBox class
# Need to define construstor, but it can just call the inherited one.
# A lot of parameters, but just pass them on. Not complex as it may seem.
def __init__(self, parent, id=wx.ID_ANY, pos=wx.DefaultPosition,
size=wx.DefaultSize, choices=, style=0,
wx.ListBox.__init__(self, parent, id, pos, size, choices, style, validator, name)
# Adding Home-made GetItems() method
# First create the list we are going to fill and return
result = 
# From the docs we know we can get an item from the listbox
# with the GetString(<index>). So we need to loop over the
# <index>. For that we first need to know how many there are.
# We can get that with the GetCount method of the ListBox class
# from wx version before v2.8.
# (note that we inherited GetCount and GetString from wx.ListBox)
for index in range(self.GetCount()):
# Finshed filling the result list here. Now just return it.
# Create out own window for simple demonstration program
def __init__(self, parent, title, size=wx.DefaultSize):
wx.Frame.__init__(self, parent, wx.ID_ANY, title, wx.DefaultPosition, size)
# Create an instance of the new ListBox class
self.listbox = OurSlightlyEnhancedListBox(self, wx.ID_ANY)
# Just add some items.
self.listbox.InsertItems(["one", "two", "three", "four", "five"], 0)
# Now do the same as GetItems() in wxPython v2.8 on the listbox
# and print the items to the stdout (the terminal).
itemlist = self.listbox.GetItems()
for item in itemlist:
# Start the thing:
app = wx.App()
win = MainWindow(None, "The Listbox")
thanks for the code. I also wrote a workaround in the meanwhile. Am I the only one that thinks that this is really strange, that this feature, that is so basic and common sense has only been integrated in this newest version?
The indirect approach, of getting the number of the item was there before the direct one, with the string returned on the selection.
Am I the only one that thinks that this is really strange, that this feature, that is so basic and common sense has only been integrated in this newest version?
I agree that seems very basic and common sense. But, that is from a python point of view. Keep in mind that wxPython is a binding, a wrapper, library for what is essentially a C++ lib. And in C++ there's no lists (not the way lists exists in python).
Very often you don't really need to make a list of he items of a listbox: many times you would fill the listbox from a list you already have, or you just want to iterate over the items in a loop.
With the listbox-methods that existed before wx v2.8 it was already pretty trivial to loop over the items in a ListBox. you only had to dive a little deeper in the doc's to see how to do it:
for index in range(MyListBox.GetCount()):