LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 04-03-2009, 09:44 PM   #1
browny_amiga
Member
 
Registered: Dec 2001
Location: /mnt/UNV/Mlkway/Earth/USA/California/Silicon Valley
Distribution: Kubuntu 10.04, Debian Squeeze, Windoze 7
Posts: 512

Rep: Reputation: 35
wxpython: ListBox object has no attribute 'GetItems'


I am trying to execute the code here:

http://www.python-forum.org/pythonfo...hp?f=2&t=10297

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.

Markus
 
Old 04-04-2009, 04:55 PM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by browny_amiga View Post
I am trying to execute the code here:
http://www.python-forum.org/pythonfo...hp?f=2&t=10297

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)
 
Old 04-04-2009, 06:06 PM   #3
browny_amiga
Member
 
Registered: Dec 2001
Location: /mnt/UNV/Mlkway/Earth/USA/California/Silicon Valley
Distribution: Kubuntu 10.04, Debian Squeeze, Windoze 7
Posts: 512

Original Poster
Rep: Reputation: 35
Oh, so very true. Blast, I expected some other stranger problem to be the cause.

Is there any other function before 2.8 that will return the selected (string) list, instead of numbers?

Markus
 
Old 04-05-2009, 08:03 AM   #4
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by browny_amiga View Post
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()

Last edited by Hko; 04-05-2009 at 08:06 AM.
 
Old 04-06-2009, 07:42 PM   #5
browny_amiga
Member
 
Registered: Dec 2001
Location: /mnt/UNV/Mlkway/Earth/USA/California/Silicon Valley
Distribution: Kubuntu 10.04, Debian Squeeze, Windoze 7
Posts: 512

Original Poster
Rep: Reputation: 35
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.

;-)

Markus
 
Old 04-07-2009, 03:33 AM   #6
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Quote:
Originally Posted by browny_amiga View Post
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))
 
Old 04-10-2009, 11:58 PM   #7
browny_amiga
Member
 
Registered: Dec 2001
Location: /mnt/UNV/Mlkway/Earth/USA/California/Silicon Valley
Distribution: Kubuntu 10.04, Debian Squeeze, Windoze 7
Posts: 512

Original Poster
Rep: Reputation: 35
Well, yes, that makes perfect sense there. Totally forgot that point. It is a wrapper and therefore C rules how it is laid out.

Cheers

Markus
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
python: AttributeError: 'list' object has no attribute 'Append' browny_amiga Programming 3 01-12-2009 05:58 AM
gentoo env-update AttributeError: 'module' object has no attribute 'env_update' linux_mopper Linux - Newbie 2 08-06-2008 02:20 PM
can javascript control the loop attribute of an embed object? BrianK Programming 3 06-27-2008 11:09 AM
wxPython problem: /_core_.so: cannot open shared object file: No such file or directo aregmi Linux - Software 2 06-06-2007 09:53 PM
Need JavaScript help with Listbox lothario Programming 0 09-07-2004 04:20 AM


All times are GMT -5. The time now is 11:44 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration