[wxPython] Access to custom windows inside Notebook

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

[wxPython] Access to custom windows inside Notebook

rtolbert
While this message has a specific subject, this may be a more general
question.  I have a notebook control, populated with a custom composite
widget that has some data and a couple of other controls. From my main
program I would like to be able to do the following to set some toolbar and
menu specific settings for the current visible page:


current=self.notebook.GetPage(self.notebook.GetSelection())

then access data members in current via:

current.GridStyle

and then Change them via

current.SetGridStyle(2)

etc.

The problem is that GetPage returns a generic wxWindow and I can't seem to
get access to the members via the "current" as returned above.

I would like to use the OO to hide my data inside each "notebook page"
object, but I need to access it from the parent of the control.

Am I missing something fundamentally simple here? Is there a set of methods
I must implement in my "derived" class so that it will play well with the
rest of wxPython. My custom class is simply derived from wxSplitterWindow as
shown below:

class mviewWorld(wxSplitterWindow):
  def __init__(self,parent,id,ML):
    wxSplitterWindow.__init__(self,parent,id)

    self.GridStyle=1
    .
    .
    .

  def SetGridStyle(self,n):
     self.GridStyle=n


Thanks for your help,

Bob

Robert W. Tolbert, Ph.D. ph.
203.778.7840
Senior Principal Systems Engineer fax 203.798.5660
Boehringer Ingelheim Pharmaceuticals, Inc.
900 Ridgebury Road/P. O. Box 368
Ridgefield, CT 06877-0368


_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] Access to custom windows inside Notebook

Robin Dunn
> While this message has a specific subject, this may be a more general
> question.

And a more general problem within wxPython...  This issue comes up in several
places and is derived from the fact that there are two objects for
everything, the Python object and the C++ object, and the C++ object usually
knows nothing or almost nothing about the Python object...

> I have a notebook control, populated with a custom composite
> widget that has some data and a couple of other controls.

...When you call self.notebook.AddPage(myWin) you are passing a Python object
to a Python object, but what really happens behind the scenes is the C++
version of the notebook object is getting a pointer to the C++ version of
myWin...

> From my main
> program I would like to be able to do the following to set some toolbar and
> menu specific settings for the current visible page:
>
> current=self.notebook.GetPage(self.notebook.GetSelection())
>

...So when you make this call it asks the C++ notbook for a page, and all it
knows about is the C++ pointer to the window it was given and nothing about
the coresponding Python object.  A wxWindow pointer is returned and SWIG
wraps a new Python object around it.

There is a helper function in wx.py called wxPyTypeCast that can be usefull
in some of these situations, but it still doesn't give you the original
Python object but just changes the type of the new shadow object.  For
example:

parent = wxPyTypeCast(self.GetParent(), "wxFrame")

I could probably come up with a way to keep track of the actual Python
objects and add code to all the wrappers to return references to the real
objects instead of creating a new shadow, but I'm afraid that it would
increase complexity, fragility, the potential for memory leaks and runtime
overhead, and decrease maintainability to the point of not being worth the
effort.  Especially when simple workarounds can usually be found.

>
> I would like to use the OO to hide my data inside each "notebook page"
> object, but I need to access it from the parent of the control.
>

My suggestion would be to derive a class from wxNotebook and override its
AddPage, DeletePage, GetPage and etc. methods.  These methods could call the
coresponding wxNotebook method and then maintain/use a python list or
dictionary of the actual objects added.

Each of the wxPython objects has a string attribute named "this" that holds a
"SWIGged pointer" which is essentially the combinarion of the address of the
C++ object and the name of its type.  If you wrote a function to extract the
numeric (hex) portion of this string it would make a great key to the
dictionary.  The numeric portion of the window.this returned from GetPage
should be the same as for the window you sent to AddPage.

--
Robin Dunn
Software Craftsman
[hidden email]
http://AllDunn.com/robin/
http://AllDunn.com/wxPython/  Check it out!



_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

Re: [wxPython] Access to custom windows inside Notebook

F. Oliver Gathmann-2
In reply to this post by rtolbert

Hi Bob,

as it happens, I came across the same problem a few days ago, and here is
my
solution:

class wxPyNotebook(wxNotebook):
    '''
    A notebook that maintains a list of the pointers to the Python windows
    associated with the individual pages.
    '''
    def __init__(self, *_argT, **_optionD):
        self._pyWindowL = []
        apply(wxNotebook.__init__, (self,) + _argT, _optionD)
    #
    # additional methods of the derived class:
    #
    def GetPyPage(self, index):
        return self._pyWindowL[index]
    #
    def GetSelectedPyPage(self):
        return self._pyWindowL[self.GetSelection()]
    #
    # overriden virtual methods of wxNotebook:
    #
    def AddPage(self, window, *_argT, **_optionD):
        self._pyWindowL.append(window)
        apply(wxNotebook.AddPage, (self, window) + _argT, _optionD)
    #
    def InsertPage(self, window, index, *_argT, **_optionD):
        self._pyWindowL.insert(index, window)
        apply(wxNotebook.InsertPage, (self, window, index) + _argT,
optionD)
    #
    def DeletePage(self, index):
        del self._pyWindowL[index]
        wxNotebook.DeletePage(self, index)
    #
    def DeleteAllPages(self):
        self._pyWindowL = []
        wxNotebook.DeleteAllPages(self)
    #
    def RemovePage(self, index):
        del self._pyWindowL[index]
        wxNotebook.RemovePage(self, index)

Regards,

Oliver

F. Oliver Gathmann
CRI Inc., 80 Ashford St., Boston, 02134 MA
phone: (617) 787-5400#224 ; fax: (617) 787-4488
e-mail: [hidden email]
web: http://www.scar.utoronto.ca/~gathmann


_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users



Reply | Threaded
Open this post in threaded view
|

RE: [wxPython] Access to custom windows inside Notebook

rtolbert
In reply to this post by rtolbert
This is amazingly simple and works PERFECTLY. Thanks for saving me the time
to do it myself.  

I guess the same could be applied to wrap all the "container" widgets, so
I'm sure I'll use this feature again.

Bob

> -----Original Message-----
> From: [hidden email] [SMTP:[hidden email]]
> Sent: Thursday, January 06, 2000 2:07 PM
> To: [hidden email];
> [hidden email]
> Subject: Re: [wxPython] Access to custom windows inside Notebook
>
>
> Hi Bob,
>
> as it happens, I came across the same problem a few days ago, and here is
> my
> solution:
>
> class wxPyNotebook(wxNotebook):
>     '''
>     A notebook that maintains a list of the pointers to the Python windows
>     associated with the individual pages.
>     '''
>     def __init__(self, *_argT, **_optionD):
>         self._pyWindowL = []
>         apply(wxNotebook.__init__, (self,) + _argT, _optionD)
>     #
>     # additional methods of the derived class:
>     #
>     def GetPyPage(self, index):
>         return self._pyWindowL[index]
>     #
>     def GetSelectedPyPage(self):
>         return self._pyWindowL[self.GetSelection()]
>     #
>     # overriden virtual methods of wxNotebook:
>     #
>     def AddPage(self, window, *_argT, **_optionD):
>         self._pyWindowL.append(window)
>         apply(wxNotebook.AddPage, (self, window) + _argT, _optionD)
>     #
>     def InsertPage(self, window, index, *_argT, **_optionD):
>         self._pyWindowL.insert(index, window)
>         apply(wxNotebook.InsertPage, (self, window, index) + _argT,
> optionD)
>     #
>     def DeletePage(self, index):
>         del self._pyWindowL[index]
>         wxNotebook.DeletePage(self, index)
>     #
>     def DeleteAllPages(self):
>         self._pyWindowL = []
>         wxNotebook.DeleteAllPages(self)
>     #
>     def RemovePage(self, index):
>         del self._pyWindowL[index]
>         wxNotebook.RemovePage(self, index)
>
> Regards,
>
> Oliver
>
> F. Oliver Gathmann
> CRI Inc., 80 Ashford St., Boston, 02134 MA
> phone: (617) 787-5400#224 ; fax: (617) 787-4488
> e-mail: [hidden email]
> web: http://www.scar.utoronto.ca/~gathmann
>
>
> _______________________________________________
> wxPython-users maillist  -  [hidden email]
> http://starship.python.net/mailman/listinfo/wxpython-users

_______________________________________________
wxPython-users maillist  -  [hidden email]
http://starship.python.net/mailman/listinfo/wxpython-users