python-2.7inheritancewxpythonwxformbuilder

wxPython - adding new objects in an inherited Class?


I am not a complete beginner, but I am by no means an expert (perhaps I know enough to be dangerous? :) ). I tried googling an answer and doing my own troubleshooting for about 2 hours before posting this. There may well exist an explanation for this, I might be using the wrong terminology. Hopefully I have explained the issue well enough below.

I've been using wxFormBuilder to manage the GUI aspects of my applications and all has been going well. However I now need to add a WebView object, which is not catered for in the menu of objects that can be added using wxFormBuilder.

For overcoming this, I thought that I should be able to specify the missing object in the Inherited class file and then write a line to have it added to an existing Sizer that is defined in the Generated class file. I considered manually adding it to the Generated code file, but if I need make changes and regenerate the file, I would lose the code I manually wrote.

Creating the new WebView object in the Inherited Class is fine, but I cannot work out how to reference the Sizer in order to add it.

Here is an excerpt from my Inherited Class

import wx
import wx.html2
import isdPhoneQueueGui  #My Generated Class

class isdPhoneQueueMain(isdPhoneQueueGui.isdPhoneQueue):
    def __init__(self, parent):
        isdPhoneQueueGui.isdPhoneQueue.__init__(self,parent)
        self.myPage = wx.html2.WebView.New(self.myPanel)
        #STUCK HERE - WHAT SCOPE TO USE: to add "myPage" object to existing Sizer "mySizer"???

And from my Generated Class (just showing the definiton of "mySizer" etc)

class isdPhoneQueue ( wx.Frame ):    
    def __init__( self, parent ):
        wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = u"ISD Phone Queue", pos = wx.DefaultPosition, size = wx.Size( 1024,768 ), style = wx.CAPTION|wx.MAXIMIZE_BOX|wx.MINIMIZE_BOX|wx.SYSTEM_MENU|wx.TAB_TRAVERSAL )
        mySizer = wx.BoxSizer( wx.VERTICAL )
        self.myPanel = wx.Panel( self.m_notebook1, wx.ID_ANY, wx.DefaultPosition, wx.Size( 1024,768 ), wx.TAB_TRAVERSAL )
        self.myPanel.SetSizer( mySizer )

I have various ways of referencing mySizer in the Inherited Class, but have failed to workout the correct way to reference it.

I initially thought to try it as in the Generated Class:

mySizer.Add(self.myPage, 1, wx.EXPAND, 10)

but this gives "NameError: global name 'mySizer' is not defined".

I then tried various combinations of referencing prefixes, none of which worked:

isdPhoneQueueGui.mySizer.Add(self.myPage, 1, wx.EXPAND, 10)
self.isdPhoneQueueGui.mySizer.Add(self.myPage, 1, wx.EXPAND, 10)
isdPhoneQueue.mySizer.Add(self.myPage, 1, wx.EXPAND, 10)
self.isdPhoneQueue.mySizer.Add(self.myPage, 1, wx.EXPAND, 10)

Thanks in advance for any assistance.


Solution

  • The variable 'mySizer' in isdPhoneQueue.__init__() is not available after that routine has run, because it is a local variable. A reference to the sizer that it was referring to is assigned to the panel self.myPanel - and that is where it remains.

    So you can get hold of it in your inherited class constructor with

    the_sizer = self.myPanel.GetSizer()

    ... because myPanel is an inherited member variable that you can access.