#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Location
    Isle of dogs
    Posts
    68
    Rep Power
    11

    Need to turn a SubClass into a Function.


    I am creating a HTML editor; it uses the worlds best toolkit of course. This project will be purely functional so I will have no user defined classes in it, unless there is good justification for doing so.
    But the hart of my applications is that, a user defined class called App. Not only that but one that subclasses another toolkit.

    Is there a way to cleanly turn this into a function(s)?
    .......................
    .........
    class App(wx.App):
    def OnInit(self):
    global text
    global frame
    frame, text = MainFrame(None, -1, "Small editor")
    print text
    tb = build_wx_toolbar(frame)
    build_wx_menus(frame)
    wx.EVT_TOOL(frame,ID_FIND, FindReplaceData)
    wx.EVT_TOOL(frame,ID_LOAD, load)
    wx.EVT_TOOL(frame,ID_SAVE, save)
    bind(frame)
    frame.Show(true)
    self.SetTopWindow(frame)
    return True
    .............................
    ......................
    if __name__ == "__main__":
    app = App(0)
    app.MainLoop()
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    You can't, because wx.App() calls OnInit() as part of its constructor code. OnInit() is not defined by the wx.App() class, so your derived class needs to define it. If you try something like this:
    Code:
    import wx
    
    app = wx.App()
    this will fail since the app object doesn't have an OnInit() function defined.

    On a side note, please post your code within [code] and [/code] tags. This is very important in the python forum .
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
  4. #3
  5. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,643
    Rep Power
    4248
    Duh, I think I should have slept some more this week . The key is to declare wx.App.OnInit and point it at a function. That way it will get called by the constructor and won't complain about a missing function. Something like this ought to do the trick.
    Code:
    import wx
    
    def setupapp(app):
        global text
        global frame
        frame, text = app.MainFrame(None, -1, "Small editor")
        print text
        tb = app.build_wx_toolbar(frame)
        app.build_wx_menus(frame)
        wx.EVT_TOOL(frame,ID_FIND, FindReplaceData)
        wx.EVT_TOOL(frame,ID_LOAD, load)
        wx.EVT_TOOL(frame,ID_SAVE, save)
        bind(frame)
        frame.Show(true)
        return true
    
    def mysuperdupereditor():
        wx.App.OnInit = setupapp
        app = wx.App()
        
        app.SetTopWindow(frame)
        app.Init()
        app.MainLoop()
        return true
    
    if __name__ == "__main__":
        mysuperdupereditor()
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo