[wxpython-users] wx.Frame

Mark Erbaugh mark at microenh.com
Tue Apr 1 12:07:47 PDT 2008


I think this is more of a Python question, but it is something that I
encountered in developing the wxPython GUI.

I have a wx.Frame that has a Panel (part of a splitter windows) that
displays exactly one panel of several depending on the menu option
selected.  To save startup time, I wait until the user first selects the
menu option to create the panel.  

During the Frame's creation, I set a data member (variable) for each
panel to None. When the user selects the menu option, I check to see if
that variable is None. If it is None, I create the panel and save the
panel to the variable. All these panels are added to the same wx.Sizer,
but only one is visible. To display the panel, I do a Freeze and hide
all the panels, then Show the one I want to display. This seems to be
working fine.

However, my issue arose when I realized that I was writing very similar
code to handle each menu option. There are only two things that are
different for each menu option handler: The class of the panel to create
and the Frame data member that keeps track of the panel.

I can easily re-factor the panel class by passing the class to the
common handler.  However, passing the frame data member is a problem as
I can't think of a simple way for the common handler to store the newly
created frame back to the frame data member.

I ended up 'solving' this by passing the frame data member name as a
string and indexing into the frame's __dict__ member, but this seems
like I am 'cheating'. I think my problem is that I consider double
underscore data members as reserved for system use. 

Here's a code snippet:

    def FacilityPanel(self, panelType, panelName):
        
        panel = self.__dict__.get(panelName)
        if panel is None:
            # means this is the first time, create the panel
            self.data_window.Freeze()
            try:
                panel = panelType(self.data_window)
                self.data_sizer.Add(panel, 1, wx.EXPAND)
                self.__dict__[panelName] = panel
            finally:
                self.data_window.Thaw()
        self.replace_panel(panel)
    
    def onCustomerCard(self, evt):
        self.FacilityPanel(FP.LUCustomerPanel, 'customer_panel')

    def onProspectCard(self, evt):
        self.FacilityPanel(FP.LUProspectPanel, 'prospect_panel')

    def onVendorCard(self, evt):
        self.FacilityPanel(FP.LUVendorPanel, 'vendor_panel')

Thanks,
Mark




More information about the wxpython-users mailing list