How to find out function signatures?

Hello all,

I'm perpetual wxPython novice with a question about
documentation (wxPython-2.8.12.0).

I am trying to write a ListBox subclass that will handle
data associated with each item by automatically calling
.GetClientData() and .SetClientData() or equivalent as
needed.

So my first question is, is there anything like this
already out there? Assuming no, then...

In one of my methods, I call:

  self.Insert (s, pos, data)

where 'self' is my ListBox subclass. This results in
the error:

  TypeError: 'Insert() takes exactly 3 arguments (4 given)'

The wxWigets doc does not list the Insert() method in ListBox
but gives three signatures in ListBox's superclass, wxControlWithItems:

  int Insert(const wxString& item, unsigned int pos)
  int Insert(const wxString& item, unsigned int pos, void *clientData)
  int Insert(const wxString& item, unsigned int pos, wxClientData *clientData)

From the error I got, I guess only the first signature is
implemented?

If I look in wxPython's _core.py, in the ItemContainer section
(apparently the superclass of wxControlWithItems), I see:

  Insert(self, String item, int pos, PyObject clientData=None) -> int

The wxPython API docs for ListBox at
  http://www.wxpython.org/docs/api/wx.ListBox-class.html
say it has an Insert although it doesn't mention a return value:

  Insert(self, item, pos, clientData)
    Insert an item into the control before the item at the pos index,
    optionally associating some data object with the item.

And the API doc for ItemContainer at
  http://www.wxpython.org/docs/api/wx.ItemContainer-class.html
gives the same interface but specifies a return value:

  int Insert(self, item, pos, clientData)
    Insert an item into the control before the item at the pos index,
    optionally associating some data object with the item.

I thought maybe the ListBox doc includes inherited methods but
there are some ItemContainer methods like IsEmpty that aren't
mentioned in the ListBox API. There seems to be some confusing
and conflicting information here.

So, the question...

How the heck am I supposed to find out what the *real*
ListBox.Insert() signature is?? (I'm assuming this is not a
one-off case in the docs -- an answer to the general problem
would be nice.) Thanks for any enlightenment.

Hi,

Hello all,

<snip>

So, the question...

How the heck am I supposed to find out what the *real*
ListBox.Insert() signature is?? (I'm assuming this is not a
one-off case in the docs -- an answer to the general problem
would be nice.) Thanks for any enlightenment.

Looks like you already found it?

otherwise in the interpreter

import wx
help(wx.ListBox.Insert)

Do you have a sample that reproduces the problem your seeing? The
method works fine when tested below:

class MyFrame(wx.Frame):
    def __init__(self):
        super(MyFrame, self).__init__(None)

        # Attributes
        self.listbox = wx.ListBox(self, choices=["1", "2", "3"])

        self.listbox.Insert("foobar", 1, "data")

app = wx.App(False)
f = MyFrame()
f.Show()
app.MainLoop()

Cody

···

On Thu, Aug 11, 2011 at 8:49 PM, Stuart McGraw <smcg4191@frii.com> wrote:

[...]

Do you have a sample that reproduces the problem your seeing? The
method works fine when tested below:

Mea cupla, you are correct. I was calling my own
Insert(), not the wx.ListBox one.

However, I remain puzzled by the output of:

  class MyFrame(wx.Frame):
    def __init__ (self):
        super (MyFrame, self).__init__ (None)

        lb1 = wx.ListBox (self)
        lb1.Insert("a", 0)
  print lb1.GetCount(), lb1.GetString(0)

        lb2 = wx.ListBox (None)
        lb2.Insert("a", 0)
  print lb2.GetCount(), lb2.GetString(0)

  app = wx.App(False)
  f = MyFrame()

which prints:

  1 a
  0

Why does the Insert() silently fail if the listbox
has no parent? I have the feeling I am missing some
bit of basic knowledge about how wxPython works...

···

On 08/11/2011 10:58 PM, Cody Precord wrote:

Hi,

[…]

Do you have a sample that reproduces the problem your seeing? The

method works fine when tested below:

Mea cupla, you are correct. I was calling my own

Insert(), not the wx.ListBox one.

However, I remain puzzled by the output of:

class MyFrame(wx.Frame):

def __init__ (self):

    super (MyFrame, self).__init__ (None)



    lb1 = wx.ListBox (self)

    lb1.Insert("a", 0)

    print lb1.GetCount(), lb1.GetString(0)



    lb2 = wx.ListBox (None)

    lb2.Insert("a", 0)

    print lb2.GetCount(), lb2.GetString(0)

app = wx.App(False)

f = MyFrame()

which prints:

1 a

0

Why does the Insert() silently fail if the listbox

has no parent? I have the feeling I am missing some

bit of basic knowledge about how wxPython works…

wx.ListBox is one of the (vast majority) of wxPython widgets that must have a parent window defined (i.e., it can not exist or be created by itself, it needs to live inside another window - the parent window).

Most of the wxPython widgets are not “top-level” windows or derived from popup windows, so they need to have a parent window specified in their constructor (unless you are using a 2-phase construction, in which case you have to specify it later, but let’s keep things simple).

For most of the everyday uses, the window hierarchy you want is:

wx.Frame ==> wx.Panel ==> wx.ListBox

wx.Frame (along with wx.Dialog and classes directly derived from them) are “top-level” windows and can be created without a parent. The full list of those “standalone” windows is here:

http://docs.wxwidgets.org/2.9.2/classwx_top_level_window.html

Andrea.

“Imagination Is The Only Weapon In The War Against Reality.”

http://xoomer.alice.it/infinity77/

import PyQt4.QtGui

Traceback (most recent call last):

File “”, line 1, in

ImportError: No module named PyQt4.QtGui

import pygtk

Traceback (most recent call last):

File “”, line 1, in

ImportError: No module named pygtk

···

On 14 August 2011 19:34, Stuart McGraw wrote:

On 08/11/2011 10:58 PM, Cody Precord wrote:

import wx

    [...]
    > Do you have a sample that reproduces the problem your seeing? The
    > method works fine when tested below:

    Mea cupla, you are correct. I was calling my own
    Insert(), not the wx.ListBox one.

    However, I remain puzzled by the output of:

     class MyFrame(wx.Frame):
       def __init__ (self):
           super (MyFrame, self).__init__ (None)

           lb1 = wx.ListBox (self)
           lb1.Insert("a", 0)
           print lb1.GetCount(), lb1.GetString(0)

           lb2 = wx.ListBox (None)
           lb2.Insert("a", 0)
           print lb2.GetCount(), lb2.GetString(0)

     app = wx.App(False)
     f = MyFrame()

    which prints:

     1 a
     0

    Why does the Insert() silently fail if the listbox
    has no parent? I have the feeling I am missing some
    bit of basic knowledge about how wxPython works...

wx.ListBox is one of the (vast majority) of wxPython widgets that *must* have a parent window defined (i.e., it can not exist or be created by itself, it needs to live inside another window - the parent window).

Most of the wxPython widgets are not "top-level" windows or derived from popup windows, so they need to have a parent window specified in their constructor (unless you are using a 2-phase construction, in which case you have to specify it later, but let's keep things simple).

For most of the everyday uses, the window hierarchy you want is:

wx.Frame ==> wx.Panel ==> wx.ListBox

In my case, I am writing a subclass of ListBox and, having
some problems (self-inflicted mostly), wanted to go step-by-
step, writing tests to verify each addition. Since these
are simply testing basic functions (put x in, get x back,
like in the quote above) I was not interested in display
and was not aware that some parent object was still required,
regardless.

Just out of curiosity, did I miss that fact in the documentation
somewhere, or is it just part of the wx "oral tradition" that
everyone just knows?

Either way, I understand now. Thanks!

[...]

http://docs.wxwidgets.org/2.9.2/classwx_top_level_window.html

That is very nice! Thanks.

···

On 08/14/2011 11:16 AM, Andrea Gavana wrote:

On 14 August 2011 19:34, Stuart McGraw wrote:
    On 08/11/2011 10:58 PM, Cody Precord wrote:

     [...]
     > Do you have a sample that reproduces the problem your seeing? The
     > method works fine when tested below:

     Mea cupla, you are correct. I was calling my own
     Insert(), not the wx.ListBox one.

     Why does the Insert() silently fail if the listbox
     has no parent? I have the feeling I am missing some
     bit of basic knowledge about how wxPython works...

wx.ListBox is one of the (vast majority) of wxPython widgets that *must* have a parent window defined (i.e., it can not exist or be created by itself, it needs to live inside another window - the parent window).

Most of the wxPython widgets are not "top-level" windows or derived from popup windows, so they need to have a parent window specified in their constructor (unless you are using a 2-phase construction, in which case you have to specify it later, but let's keep things simple).

For most of the everyday uses, the window hierarchy you want is:

wx.Frame ==> wx.Panel ==> wx.ListBox

In my case, I am writing a subclass of ListBox and, having
some problems (self-inflicted mostly), wanted to go step-by-
step, writing tests to verify each addition. Since these
are simply testing basic functions (put x in, get x back,
like in the quote above) I was not interested in display
and was not aware that some parent object was still required,
regardless.

Just out of curiosity, did I miss that fact in the documentation
somewhere, or is it just part of the wx "oral tradition" that
everyone just knows?

Probably a little of the "oral tradition" where it's just something that everybody assumes that everybody knows it, but it seems that you're having another problem that is not your fault. There should be an exception in that case that is generated from an assertion in the C++ code. However if you're using Linux then this is probably the fault of the package builder or the distro's policy manager who has turned off assertions in order to eek out a bit more performance in the wx libraries. If you're new to wxPython then I recommend that you either learn on Windows or OSX using my builds (where the assertions are never turned off), use a debug build if there is one available in your Linux distro's package repository, or build your own copy of wx and wxPython so you can assure that the runtime assertions are not turned off. Here is what it would look like if you had tried your code in a build with the runtime assertions turned on:

  >>> import wx
  >>> wx.version()
  '2.8.12.1 (mac-unicode)'
  >>> import wx
  >>> app = wx.App(False)
  >>> lb = wx.ListBox(None)
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    File "/usr/local/lib/wxPython-unicode-2.8.12.1/lib/python2.6/site-packages/wx-2.8-mac-unicode/wx/_controls.py", line 1284, in __init__
      _controls_.ListBox_swiginit(self,_controls_.new_ListBox(*args, **kwargs))
  wx._core.PyAssertionError: C++ assertion "parent" failed at /BUILD/wxPython-src-2.8.12.1/src/mac/carbon/window.cpp(1157) in Create(): can't create wxWindowMac without parent

···

On 8/14/11 10:56 AM, Stuart McGraw wrote:

On 08/14/2011 11:16 AM, Andrea Gavana wrote:

On 14 August 2011 19:34, Stuart McGraw wrote:
     On 08/11/2011 10:58 PM, Cody Precord wrote:

  >>>

--
Robin Dunn
Software Craftsman

I was indeed running under linux (Fedora 15).

Thanks for that suggestion. I tried running my current
code on Windows and got two assertion errors in some
unrelated parts of the code. I will make this part of
my debugging/testing regimen.

···

On 08/14/2011 02:31 PM, Robin Dunn wrote:

[...] There should be an
exception in that case that is generated from an assertion in the C++
code. However if you're using Linux then this is probably the fault of
the package builder or the distro's policy manager who has turned off
assertions in order to eek out a bit more performance in the wx
libraries. If you're new to wxPython then I recommend that you either
learn on Windows or OSX using my builds (where the assertions are never
turned off), use a debug build if there is one available in your Linux
distro's package repository, or build your own copy of wx and wxPython
so you can assure that the runtime assertions are not turned off. Here
is what it would look like if you had tried your code in a build with
the runtime assertions turned on:
[...]