Splitting python source code

By now, my main app file is getting a bit big and I would like to split out some code, such as the event handlers into a separate file, but being new to python, I have no idea how or where I would import the routines.
For instance, I currently have a stub in my main class

def OnMenuLogWindowShow( self, event ):
    print( "ShowLog window")
    event.Skip()

which I would like to move to a secondary file called mainHelper.py in a way where it will still be called as a member of my main frame class and show or hide the log window.
Being very new to python, I am not having much luck.

I have tried to import the file at the very top, as well as part of the main frame init but no luck at all. As long as the code is in the helper file, it does not get called.

1 Like

One way to do this would be to use multiple inheritance.

For example, in the mainHelper.py module you could define a class that contains all the delegated methods:

class MainHelper:
    def OnLogWindowShow(self, event):
        print("ShowLog window")
        event.Skip()

Then in your main module you could import that class and then use it as one of the base classes for your main frame:

import wx
from mainHelper import MainHelper

class MainFrame(wx.Frame, MainHelper):
    def __init__(self, parent):
        super(MainFrame, self).__init__(parent)
        panel = wx.Panel(self)
        v_sizer = wx.BoxSizer(wx.VERTICAL)
        log_file_button = wx.Button(panel, -1, "Show Log")
        v_sizer.Add(log_file_button)
        panel.SetSizer(v_sizer)
        self.Bind(wx.EVT_BUTTON, self.OnLogWindowShow, log_file_button)


app = wx.App()
frame = MainFrame(None)
frame.Show()
app.MainLoop()

Tested using Python 3.10.12 + wxPython 4.2.1 gtk3 (phoenix) wxWidgets 3.2.2.1 on Linux Mint 21.2

but what if MainHelper has a constructor (__init__ method) :upside_down_face:

In that case I would call each base class’s __init__ method directly, instead of using super.

Thank you both.
Your replies lead me to investigate super() in a bit more detail and now I have another question, or more likely an extension to my first one.

One of the things, I forgot to mention initially, that my main frame is already inheriting from a class created by my wxFormBuilder (wxFB) GUI builder.
And, reading up on super(), has me even more confused, particularly wondering if all of the classes created by wxFB, should include a super() call and since they do not, whether there is a work around or whether it does not matter.

Right now, the start of my main frame looks like:

#import the wxFB created GUI file 
import wpBgBase  

class MyFrameAui(wpBgBase.MyFrameAui):
    
    def __init__(self,parent):
        sys.excepthook = MyExceptionHook
        super().__init__(None) 
        wpBgBase.MyFrameAui.__init__(self,parent)

but what about this :thinking:

import wx

# from mainHelper import MainHelper
class MainHelper:
    def __init__(self, *args, **kargs):
        super().__init__(*args, **kargs)
        self.txt = self
    def OnLogWindowShow(self, event):
        print("ShowLog window", self.txt)
        event.Skip()

class MainFrame(MainHelper, wx.Frame):
    def __init__(self, parent):
        super().__init__(parent)
        panel = wx.Panel(self)
        v_sizer = wx.BoxSizer(wx.VERTICAL)
        log_file_button = wx.Button(panel, -1, "Show Log")
        v_sizer.Add(log_file_button)
        panel.SetSizer(v_sizer)
        self.Bind(wx.EVT_BUTTON, self.OnLogWindowShow, log_file_button)

app = wx.App()
frame = MainFrame(None)
frame.Show()
app.MainLoop()

I obviously haven’t been doing enough research. I guess it’s time for me to give up. Bye.

I have sort of given up for now and keep everything in my main file, just so I can get on with getting the main functionality working and get the job done.
The finer details will, hopefully, become clearer with time and experience and then perhaps I can split things up a bit.

the purpose of super is pretty simple (from the docs):

This is useful for accessing inherited methods that have been overridden in a class.

in your example it’s the method __init__ which is in your class MyFrameAui and in the base class wpBgBase.MyFrameAui (presumably)
using super avoids using the base class textually which may change
what you have done is using both versions and that is definitely one too much
(in case of multiple base classes, as @RichardT suggested, matters get a bit more intersting :sweat_smile:)

Yeah, well, that ‘presumption’ does not seem to apply.
I have looked at the base code generated by wxFormBuilder and I cannot find any call to super() in its ‘init’.
I have posted a question regarding this issue on the wxFB GitHub page, but have not had any reply to that.
Seems rather basic to me since , as far as I understand things, the wxFB code typically is intended to be base class for the working code, but then, I am very much a newb as far as Python & wxPython goes.

well, they may have a problem with your problem :rofl: (but tell us how they perform)

The site “bot” told me to come down here and check up on you little hellions… Believe that! No shit. Carry on punks. Stay cool. Else the wrath of tomorrow comes…