473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

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.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
3 4993
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: frank | last post by:
need help with wxpython. wxpython code is made with boa-constructor when i run the code i get this error message: Traceback (most recent call last): File "wxApp1.py", line 24, in ? main()...
0
by: Richard Townsend | last post by:
I've been experimenting with passing a window handle from a wxPython app to a Tkinter app. The Tkinter app should embed a Toplevel window containing a Canvas widget in the wxPython app's Frame (see...
2
by: Emiliano Molina | last post by:
This has been driving me crazy for a couple of days and I have finally narrowed it down to the following code. If the commented section is uncommented the drag and drop handler is never called. I...
1
by: John Kinson | last post by:
Hi, I'm trying to get the wxPython demo to run on a Debian sarge testing installation, but encounter the following error when I run the demo script: # ./demo.py Traceback (most recent call...
3
by: John Salerno | last post by:
I'm using the sample code of the file 'simple.py' and trying to make a single window with a panel in it, but I keep getting an error. Here's my code: (I know I might need something else, like a...
7
by: John Salerno | last post by:
I was reading in the wxPython wiki that most of the time you don't have to include the id parameter at all, and you can just use keyword arguments for other parameters. But I'm having trouble...
9
by: Tyler | last post by:
Hello All: I am currently working on a project to create an FEM model for school. I was thinking about using wxPython to gather the 12 input variables from the user, then, after pressing the...
6
by: Iain King | last post by:
Hi. I have a modal dialog whcih has a "Browse..." button which pops up a file selector. This all works fine, but the first thing the user has to do when they open the dialog is select a file, so...
3
by: Netwatcher | last post by:
im try to activate the tutorial code for wxPython, it is working properly when i tell it to import * from wxPython.wx but with an error about the package, i've done as i was told to change the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.