Newbie question on messaging

Well, this is a program that generates (well-formed but random in structure and vocabulary) sentences. One menu item, bound to the Frame that holds everything else, simply calls a member function of the Frame called GenerateSentence(), which goes through the routine once. Another item should *keep* calling that function (generating a long, long stream of sentences) until the user tells it to quit by pressing a mouse button. Nothing fancy there -- I just don't know what to do in a situation where you need to watch for one message while inside the handler for another. Can't be that uncommon a situation, can it?

Charles Hartman
http://cherry.conncoll.edu/cohar
http://villex.blogspot.com

Charles Hartman wrote:
> Here's
> what I'm trying to do:
>
> def init(. . .):
> self.Bind(wx.EVT_MENU, self.KeepGoingTill, id= . . .)
>
> def KeepGoingTill(self, evt):
> while 1:
> if {user presses, or has pressed, a mouse button}:
> break
> self.doSomethingAgain()

Can you give us more info about what you want to "Keep Going". What it
is will help determine the best solution. A few hints in the meantime:

When a callback is called, KeepGoingTill(), in this case, the event
queue is not re-visited until it returns. What that means is that you
won't get the key-presses, etc, until after function finishes. You can
put a wxApp.Yield() in there, to ask the system to process events, but
then it could do anything. I'm at a loss for a general solution without
a better idea what you need to do.

-Chris

Charles Hartman wrote:

Well, this is a program that generates (well-formed but random in structure and vocabulary) sentences. One menu item, bound to the Frame that holds everything else, simply calls a member function of the Frame called GenerateSentence(), which goes through the routine once. Another item should *keep* calling that function (generating a long, long stream of sentences) until the user tells it to quit by pressing a mouse button. Nothing fancy there -- I just don't know what to do in a situation where you need to watch for one message while inside the handler for another. Can't be that uncommon a situation, can it?

Here's how I'd do that:

Set up a wx.Timer that calls GenerateSentence().

Create a method that starts the timer, and have it called by your menu item, or button, or whatever.

Create a method that stops the timer, and have it called by a "stop" button, or whatever.

I've written a little demo of this, check out the enclosed code.

-Chris

TimerDemo.py (1.89 KB)

···

--
Christopher Barker, Ph.D.
Oceanographer
                                         
NOAA/OR&R/HAZMAT (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

Looks like a job for.... wx.Timer!

Set up a handler in the frame to catch the desired
termination event (mouse event, escape, whatever.).
Something like (not tested!):

def genSentenceTimerTerminationHandler( self, evt ):
    """
    Intercept (which?) event to stop the generate-sentence timer.

    Generate-sentence timer is not running, allow the event
    to pass through for other uses.
    """
    # Presence of timer acts as flag
    if not self._genSentenceTimer:
       evt.Skip()
    self.genSentenceTimerStop()

def genSentenceTimerCallback( self, evt ):
    self.GenerateSentence()

def genSentenceTimerStart( self, interval=100 ):
    """
    Start up generate-sentence timer.

    @param interval: Timer firing interval (milliseconds)
    """
    # Set up timer to call GenerateSentence periodically.
    self._genSentenceTimer = wx.Timer( self )
    timer.Start( interval, wx.TIMER_CONTINUOUS ):
    # Bind timer event
    self.Bind( wx.EVT_TIMER, self.genSentenceTimerCallback )

def genSentenceTimerStop( self ):
    """
    Stop and clean up generate-sentence timer.
    """
    self._genSentenceTimer.Stop()
    # Unbind timer event
    self.Unbind( wx.EVT_TIMER )
    # Delete timer
    del self._genSentenceTimer

Have your menu item call genSentenceTimerStart.

HTH.

- Sam

···

At 2004-11-08 12:55 PM -0500, you wrote:

Another item should *keep* calling that function (generating a long, long stream of sentences) until the user tells it to quit by pressing a mouse button. Nothing fancy there -- I just don't know what to do in a situation where you need to watch for one message while inside the handler for another. Can't be that uncommon a situation, can it?

__________________________________________________________
Spinward Stars, LLC Samuel Reynolds
Software Consulting and Development 303-805-1446
http://SpinwardStars.com/ sam@SpinwardStars.com