[wxPython] GetParent() bug?

I have a suspicion that when calling GetParent() on a window that some sort of (C++) slicing of types is going on. I have an app where I have a wxTreeCtrl nested in an wxMDIChildFrame which in turn is child of an wxMDIParentFrame. I have found that if I supply “tree.GetParent().GetParent()” as the parent argument to the constructor for another wxMDIChildFrame that I get complaints than the parent supplied is not of the right (wxMDIParentFrame) type. However if I stick the frame in a global and use that as an argument that I get no complaints.

I know that in both cases they are getting the same object. If I print the object returned by tree.GetParent().GetParent() I observe the same output as just printing the global reference with the exception that the former reports the type as wxWindow and the latter wxMDIParentFrame.

I can supply the code but it is rather length. Any suggestions?

Regards,

Chuck

The objects are diffrerent because they are C++ pointers encapsulated in python
objects. When you call the function twice, you get 2 different pointers, and
this is a problem if you want to do comparisons. You'll get the same behaviour
with *many* wxPython mappings (in wxTreeCtrl for instance). I know no general
solution to the problem.

Alexandre Fayolle

I don't really have a need to compare the objects. I just used a python
print statement to determine whether the object returned by the
tree.GetParent().GetParent() was the same as the frame window. It is. The
problem is that object's type returned by the chained GetParent() calls
seems to be sliced. The framework don't recognize the type as a
wxMDIParentFrame and will not accept it as the parent argument to a
wxMDIChildFrame constructor. I am currently using a global to get around
it, but clearly this is a hack.

Regards,
Chuck

···

----- Original Message -----
From: "Alexandre Fayolle" <alexandre.fayolle@free.fr>
To: <wxpython-users@wxwindows.org>; "Charles Medcoff" <cmedcoff@sprynet.com>
Sent: Monday, August 14, 2000 3:13 AM
Subject: Re: [wxPython] GetParent() bug? No !

The objects are diffrerent because they are C++ pointers encapsulated in

python

objects. When you call the function twice, you get 2 different pointers,

and

this is a problem if you want to do comparisons. You'll get the same

behaviour

with *many* wxPython mappings (in wxTreeCtrl for instance). I know no

general

solution to the problem.

Alexandre Fayolle

Charles Medcoff wrote:

I have a suspicion that when calling GetParent() on a window that some
sort of (C++) slicing of types is going on. I have an app where I
have a wxTreeCtrl nested in an wxMDIChildFrame which in turn is child
of an wxMDIParentFrame. I have found that if I supply
"tree.GetParent().GetParent()" as the parent argument to the
constructor for another wxMDIChildFrame that I get complaints than the
parent supplied is not of the right (wxMDIParentFrame) type. However
if I stick the frame in a global and use that as an argument that I
get no complaints.

I know that in both cases they are getting the same object. If I print
the object returned by tree.GetParent().GetParent() I observe the same
output as just printing the global reference with the exception that
the former reports the type as wxWindow and the latter
wxMDIParentFrame.

I can supply the code but it is rather length. Any suggestions?

If you are SURE about the type tree.GetParent().GetParent() will
return you can use wxPyTypeCast to change it to the type you want.

E.g. wxPyTypeCast(tree.GetParent().GetParent(), 'wxMDIParentFrame')

···

Regards,
Chuck

--
Riaan >>> a='a=%s;a%%`a`';a%`a`
___________________________________________________
Boa Constructor - RAD GUI building IDE for wxPython
     http://boa-constructor.sourceforge.net

The objects are diffrerent because they are C++ pointers encapsulated in

python

objects. When you call the function twice, you get 2 different pointers,

and

No! No! No!!!!!! I am being misunderstood. I don't care about comparing
objects. I don't want to compare objects.

All I am trying to do is call tree.GetParent().GetParent() in order to work
my way up to a wxMDIParentFrame. I want to use this as the parent argument
to a wxMDIChildFrame constructor call. It does not work! The constructor
call fails because it thinks that I am trying to pass a wxWindow as a parent
(instead of a wxMDIChildFrame).

However, if I save the wxMDIParentFrame in a global variable and pass that
it works fine. I'm guessing that some some sort of slicing is going on
through the call to GetParent(), but it is just a guess.

The two print statements are printing reference to the same object; just
accessing them in different ways. The reference produced by the call to
GetParent().GetParent() seems to be be returning a sliced object. If I use
the commented version I get no errors.

        print g_frame # this is the top level frame window
        parent = self.tree.GetParent().GetParent()
        print parent # this is the same top level frame window object
        MyTabbedMDIChildFrame(parent, -1, self.device.GetTitle() + ":
Document", \
            wxPoint(10,10), wxSize(40,40))
## # kludge using global
## MyTabbedMDIChildFrame(g_frame, -1, self.device.GetTitle() + ":
Document", \
## wxPoint(10,10), wxSize(40,40))

Here are the errors reported at runtime

<C wxMDIParentFrame instance at _1c87130_wxMDIParentFrame_p>
<C wxWindow instance at _1c87130_wxWindow_p>
Traceback (innermost last):

bla, bla, bla

  File "E:\Python\wxPython\mdi.py", line 82, in __init__
    self.this = apply(mdic.new_wxMDIChildFrame,_args,_kwargs)
TypeError: Type error in argument 1 of new_wxMDIChildFrame. Expected
_wxMDIParentFrame_p.

From wx.py, at least in 2.1.16:

# This helper function will take a wxPython object and convert it to
# another wxPython object type. This will not be able to create objects
# that are derived from wxPython classes by the user, only those that are
# actually part of wxPython and directly corespond to C++ objects.

···

#
# This is useful in situations where some method returns a generic
# type such as wxWindow, but you know that it is actually some
# derived type such as a wxTextCtrl. You can't call wxTextCtrl specific
# methods on a wxWindow object, but you can use this function to
# create a wxTextCtrl object that will pass the same pointer to
# the C++ code. You use it like this:
#
# textCtrl = wxPyTypeCast(window, "wxTextCtrl")
#
#
# WARNING: Using this function to type cast objects into types that
# they are not is not recommended and is likely to cause your
# program to crash... Hard.
#

That should do the trick.

Cheerio,
Chris

-------------------------------------------------------------------

-----Original Message-----
From: wxpython-users-admin@wxwindows.org
[mailto:wxpython-users-admin@wxwindows.org]On Behalf Of Charles Medcoff
Sent: Tuesday, 15 August 2000 11:37 AM
To: wxpython-users@wxwindows.org; Alexandre Fayolle
Subject: Re: [wxPython] GetParent() bug? No !

> The objects are diffrerent because they are C++ pointers encapsulated in
python
> objects. When you call the function twice, you get 2 different pointers,
and

No! No! No!!!!!! I am being misunderstood. I don't care about comparing
objects. I don't want to compare objects.

All I am trying to do is call tree.GetParent().GetParent() in
order to work
my way up to a wxMDIParentFrame. I want to use this as the
parent argument
to a wxMDIChildFrame constructor call. It does not work! The constructor
call fails because it thinks that I am trying to pass a wxWindow
as a parent
(instead of a wxMDIChildFrame).

However, if I save the wxMDIParentFrame in a global variable and pass that
it works fine. I'm guessing that some some sort of slicing is going on
through the call to GetParent(), but it is just a guess.

The two print statements are printing reference to the same object; just
accessing them in different ways. The reference produced by the call to
GetParent().GetParent() seems to be be returning a sliced object.
If I use
the commented version I get no errors.

        print g_frame # this is the top level frame window
        parent = self.tree.GetParent().GetParent()
        print parent # this is the same top level frame window object
        MyTabbedMDIChildFrame(parent, -1, self.device.GetTitle() + ":
Document", \
            wxPoint(10,10), wxSize(40,40))
## # kludge using global
## MyTabbedMDIChildFrame(g_frame, -1, self.device.GetTitle() + ":
Document", \
## wxPoint(10,10), wxSize(40,40))

Here are the errors reported at runtime

<C wxMDIParentFrame instance at _1c87130_wxMDIParentFrame_p>
<C wxWindow instance at _1c87130_wxWindow_p>
Traceback (innermost last):

bla, bla, bla

  File "E:\Python\wxPython\mdi.py", line 82, in __init__
    self.this = apply(mdic.new_wxMDIChildFrame,_args,_kwargs)
TypeError: Type error in argument 1 of new_wxMDIChildFrame. Expected
_wxMDIParentFrame_p.

_______________________________________________
wxPython-users mailing list wxPython-users@wxwindows.org
http://wxwindows.org/mailman/listinfo/wxpython-users