wxpython - passing arg to wx.app OnInit

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stuart McGraw

    #1

    wxpython - passing arg to wx.app OnInit

    I have a wxPython app, conventionally structured
    with a Application class derived from wx.App.
    My problem is that the app accepts a command
    line argument that must be acted upon within the
    OnInit() method of the Application class. How do
    I pass it cleanly from main() into app.OnInit()? In
    the simplified example below, dbfn is the piece of
    info that is in main() that OnInit() needs to use.
    Is a global variable is the only way? :-(

    class Application (wx.App):
    def OnInit (self):
    oper = Operations (dbfn);
    frame = Frame (None, -1, "Title", oper)
    self.SetTopWind ow (frame)
    return True

    def main ():
    dbfn = sys.args[1]
    app = Application (redirect=0)
    app.MainLoop ()

    Apologies if the answer is (or should be) obvious... this
    is my first time using wxWindows and I am still rather new
    to Python.
  • Peter Hansen

    #2
    Re: wxpython - passing arg to wx.app OnInit

    Stuart McGraw wrote:[color=blue]
    > I have a wxPython app, conventionally structured
    > with a Application class derived from wx.App.
    > My problem is that the app accepts a command
    > line argument that must be acted upon within the
    > OnInit() method of the Application class. How do
    > I pass it cleanly from main() into app.OnInit()? In
    > the simplified example below, dbfn is the piece of
    > info that is in main() that OnInit() needs to use.
    > Is a global variable is the only way? :-([/color]

    There are various ways, but the simplest is to accept that sys.argv is
    *already* a "global" and just to access it directly from the
    Application's OnInit() method.

    This wiki page demonstrates:


    -Peter

    Comment

    • Stuart McGraw

      #3
      Re: wxpython - passing arg to wx.app OnInit


      "Peter Hansen" <peter@engcorp. com> wrote in message news:7pGdnfmqQo JJG8beRVn-iA@powergate.ca ...[color=blue]
      > Stuart McGraw wrote:[color=green]
      > > I have a wxPython app, conventionally structured
      > > with a Application class derived from wx.App.
      > > My problem is that the app accepts a command
      > > line argument that must be acted upon within the
      > > OnInit() method of the Application class. How do
      > > I pass it cleanly from main() into app.OnInit()? In
      > > the simplified example below, dbfn is the piece of
      > > info that is in main() that OnInit() needs to use.
      > > Is a global variable is the only way? :-([/color]
      >
      > There are various ways, but the simplest is to accept that sys.argv is
      > *already* a "global" and just to access it directly from the
      > Application's OnInit() method.
      >
      > This wiki page demonstrates:
      > http://wiki.wxpython.org/index.cgi/U...dLineArguments
      >
      > -Peter[/color]

      I simplied the my code for posting. In my real program, the
      thing being passed is not a command line argument per se,
      but the result of signifigant processing dependent on the
      command line argument. I do not want to repeat that
      processing in the wx.App method.
      Would you elaborate on the other ways?

      Comment

      • Peter Hansen

        #4
        Re: wxpython - passing arg to wx.app OnInit

        Stuart McGraw wrote:[color=blue]
        > I simplied the my code for posting. In my real program, the
        > thing being passed is not a command line argument per se,
        > but the result of signifigant processing dependent on the
        > command line argument. I do not want to repeat that
        > processing in the wx.App method.
        > Would you elaborate on the other ways?[/color]

        In that case, override __init__ something like this:

        class Application(wx. App):
        def __init__(self, *pargs, clargs=None, **kwargs):
        self.clargs = clargs or [] # store reference to args

        # call parent class initializer
        wx.App.__init__ (self, *pargs, **kwargs)

        def OnInit(self):
        # use self.clargs here


        app = Application(red irect=0, clargs=[dbfn])

        or whatever... I just guessed at what you meant to do with dbfn though,
        but I think the basic idea is clear enough.

        -Peter

        Comment

        Working...