By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,898 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,898 IT Pros & Developers. It's quick & easy.

wxpython - passing arg to wx.app OnInit

P: n/a
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.SetTopWindow (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.
Oct 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Stuart McGraw wrote:
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? :-(


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
Oct 23 '05 #2

P: n/a

"Peter Hansen" <pe***@engcorp.com> wrote in message news:7p********************@powergate.ca...
Stuart McGraw wrote:
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? :-(


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


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?
Oct 23 '05 #3

P: n/a
Stuart McGraw wrote:
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?


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(redirect=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
Oct 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.