Search code examples
python-2.7inheritancewxpython

Python class inheritance with wx.frame


I am using python2.7 + wxpython to show some chart data.

I have two class, one(wx class) is manage for wx.frame and another(text decode class) is for parser from file context. I can use self.* to control wx.frame(eg: self.SetMenuBar, self.SetTitle) in wx class, and can I pass this "self" to another class to control the same wx.frame?

Like follow brief code,

class CanvasFrame(wx.Frame):
    def __init__(self, parent, title):
        wx.Frame.__init__(self, parent, -1, title, size=(550, 350))
        self.SetBackgroundColour(wxc.NamedColour("WHITE"))
        ....
        normal_test_mode_decode(filename, directory)
        ....

class normal_test_mode_decode(CanvasFrame):
    def __init(self, csv_fname, csv_dir):
        ....
        self.SetTitle(os.path.join(csv_dir, csv_fname)) #Error here

class MyApp(wx.App):
    def OnInit(self):
        frame = CanvasFrame(None, "wxPython mathtext demo app")
        ....

if __name__ == '__main__':
    app = MyApp()
    app.MainLoop()

This is error message if I directly use self.SetTitle at another class.

Traceback (most recent call last):
  File "D:\normal_test_mode_parser\normal_test_mode_parser.py", line 45, in OnOpen
    self.decode = normal_test_mode_decode(filename, directory)
  File "D:\normal_test_mode_parser\normal_test_mode_parser.py", line 64, in __init__
    self.SetTitle(os.path.join(csv_dir, csv_fname))
  File "C:\Python27\lib\site-packages\wx-3.0-msw\wx\_windows.py", line 455, in SetTitle
    return _windows_.TopLevelWindow_SetTitle(*args, **kwargs)
TypeError: in method 'TopLevelWindow_SetTitle', expected argument 1 of type 'wxTopLevelWindow *'

I want to fix this issue because my plan is that a class(CanvasFrame) for wxpython to manage GUI functionality and another class(normal_test_mode_decode) handle data analysis functionality.

thank you!

When run this code, choose file -> open any file, then you see error.


Solution

  • I have made a few small changes to your code, either to simplify it or to eliminate bugs.

    • I replaced more complicated expressions involving super with super() since you're using Py3 and this makes mistakes less likely to occur.
    • In super().__init__(parent, id=-1, title='something something', size=(550, 350)) I added the literal title. This is probably not what you want to have ultimately but it makes it possible to proceed with development work.
    • I changed the lines near self.content = to self.content = self.fp.readline().strip().split(',') because line was undefined. Again, you will probably want to give this more attention.