views:

47

answers:

3

OK, I'm trying to explain what I want to achieve in another way. Here's an example:

Say if it's an anti virus program, and user can choose between two ways to run the program, choice one, automatically start to scan disks for virus when the program starts up, choice two, hit the start button to make the program scan disks for virus after the program starts up any time the user wants. So, as a wxpython beginner, I know how to bind wx.EVT_BUTTON to let scanning start after the user hit the start button, but I don't know how to make the scanning start once the program starts up. I wonder if there's a program_start event I can bind? Hope you guys can help me out. Thanks!

+1  A: 

Why don't you run it just in module code? This way it will be run only once, because code in module is run only once per program instance.

gruszczy
Actually I want to give user a choice, do stuff(run the event handler) at program startup or manually do stuff(run the event handler) when hit a button after the program starts. I'm just starting with wxpython, so would you please be more specific? I don't really understand what you mean, sorry...
Shane
I guess, you must be a little more specific about what you are trying to achieve. Please try to describe it in the question.
gruszczy
+1  A: 

In wxPython you can override the OnInit method of your Application class to run code when the program launches. For example:

  def OnInit(self):
    # Check for a running instance for this user.  Do not instantiate if found.
    if self.checkInstance():
      dbcon.cursor().callproc('post_mutex', (self.mutexname,))
      dbcon.commit()
      self.Cleanup()
      return False

    # Register for database events.
    DataCache['dbListener'] = dbListener()

    return True

There is of course another method on my Application class called checkInstance. Depending on it's return value, my application either launches, or triggers the other running instance to launch.

In wxPython you don't have to do anything special with your App class to get the binding to take place for your OnInit method. It'll happen automatically if you override it.

g.d.d.c
Is it possible to define a new event and bind it just like button event binding? Such as self.Bind(NewEvent, self.RunAtStart, self.StartButton)
Shane
Yes - http://wiki.wxpython.org/CustomEventClasses
Steven Sproat
@Shane - Yes you can create custom events, bind them, and then trigger them, but that's not really what your question asked initially. This is an example of how to run code when your program launches. You can make decisions, instantiate other classes, etc from here. It sounds like you really just want to bind a Button to a Method, which you already know how to do.
g.d.d.c
A: 

In your init or OnInit method, do some kind of check to see if the program should run the startup process on startup (i.e. check a config file or some such). If yes, call the "scan" method using wx.CallAfter or wx.CallLater or call it after you Show() the frame.

Mike Driscoll