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.
Notices
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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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. This is on Debian Lenny.
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?
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()
name_list.sort()
# Set() clears and reloads the listbox
self.listbox.Set(name_list)
Is there any other function before 2.8 that will return the selected (string) list, instead of numbers?
Not ready-made.
But it isn't too difficult to make one.
Here's a complete working example:
Code:
#!/usr/bin/env python
import wx
# 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,
validator=wx.DefaultValidator, name=wx.ListBoxNameStr):
wx.ListBox.__init__(self, parent, id, pos, size, choices, style, validator, name)
# Adding Home-made GetItems() method
def GetItems(self):
# 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()):
result.append(self.GetString(index))
# Finshed filling the result list here. Now just return it.
return result
# Create out own window for simple demonstration program
#
class MainWindow(wx.Frame):
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:
print item
# Start the thing:
#
def main():
app = wx.App()
win = MainWindow(None, "The Listbox")
win.Show()
app.MainLoop()
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:
Code:
for index in range(MyListBox.GetCount()):
print MyListBox.GetString(index))
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.