[visionegg] Re: Terminating a While Loop

  • From: Mark Halko <mhalko@xxxxxx>
  • To: visionegg@xxxxxxxxxxxxx
  • Date: Tue, 17 Jul 2007 14:48:53 -0400

Without seeing your code, it's difficult to say.
Your error comes from p not being defined.
if p=Presentation(...) is not defined before the loop it won't work. Or it could be out of the namespace somehow (if you put it in another function, for example). I would suggest using the callback functions for the presentation classes, rather than the keydown checks after your presentation like I showed.

import sys
p=Presentation(foo...)
quit_now=False
pause = False
def keydown(event) :
        global quit_now, pause
        if event.key == pygame.locals.K_ESCAPE :
                quit_now = True
        if event.key == pygame.locals.K_SPACE :
                pause = not pause
p.parameters.handle_event_callbacks = [(pygame.locals.QUIT, sys.exit), (pygame.locals.KEYDOWN,keydown)]

while not quit_now :
        p.go()
        # you may need to add callback checks outside of go loop, I don't know
        for ev in pygame.event.get() :
                if ev == pygame.locals.KEYDOWN : keydown(ev)
                if ev == pygame.locals.QUIT: sys.exit()



If you're trying to give an unspecified number of trials, you may find it easier to code up a single presentation as the "go loop", and incorporate the trial structure with a controller.

for example:

p=Presentation(go_duration=('forever',), viewports=myviewports)

nTrials=0
max_trials=5
trialStartTime=0.
trialLen =2.0
def my_trial_handler(t) :
        global nTrials, trialStartTime
        if t > trialStartTime + trialLen:
                nTrials +=1
                if nTrials > max_trials: sys.exit()
                else : trialStartTime=t
        #do some trial specific stuff

p.add_controller(None,None, FunctionController (during_go_func=my_trial_handler)) p.parameters.handle_event_callbacks = [(pygame.locals.QUIT, sys.exit), (pygame.locals.KEYDOWN,keydown)]

Hope this helps,
Mark

On Jul 17, 2007, at 2:07 PM, Maher, Jeffrey wrote:

Mark,

Alright I tried this, and I seemed to get some slightly better results, it didnt fail as quickly. But when I looked into my log of errors, it now says that the p in the p.go is not defined. Heres the exact error: File "C:\Python22\VisionEgg\visionegg-0.9.9-demo\demo\project \stim_project4c.py", line 614, in ?
    p.go()
NameError: name 'p' is not defined

I havent had a problem with it before, when it was in the while loop. Any suggestions as to why its becoming a problem now? Sorry if this is a stupid mistake on my part, but any advice that you may have would be much appreciated.
-Jeff

________________________________

From: visionegg-bounce@xxxxxxxxxxxxx on behalf of Mark Halko
Sent: Tue 7/17/2007 12:52 PM
To: visionegg@xxxxxxxxxxxxx
Subject: [visionegg] Re: Terminating a While Loop



How about something as simple as:

quit_now = False
pause = False
while not quit_now:
        if not pause :
                p.go()
        for event in pygame.event.get():
                if event.type == pygame.locals.KEYDOWN :
                        if event.key == pygame.locals.K_ESCAPE :
                                quit_now = True
                        if event.key == pygame.locals.K_SPACE :
                                pause = not pause

You could also set quit_now to True to quit if you reach some
criterion level from your experiment.

Mark

On Jul 17, 2007, at 1:14 PM, Maher, Jeffrey wrote:

Hello,
Im currently working on the following code trying to determine a
way to exit a while loop before the criteria is satisfied.

i=0
        while i<eval(self.number.get()):
                p.go
                i=i+1

My goal is to set it to a certain key that will either pause the
program, and then resume it once pressed again, or have it exit the
program all-together, either/or would be acceptable.

I had tried setting it to the "esc" key by doing the following code
that I found while searching for an answer:
      if msvcrt.kbhit() and msvcrt.getch() == chr(27):
           break
But that just causes the program to fail to start up.
I have also tried:
for event in pygame.event.get():
        if event.type == pygame.locals.KEYDOWN:
            if event.key == pygame.locals.K_ESCAPE:
                  sys.exit()
But this again just causes the program to fail to start up.

Any suggestions would be greatly appreciated.
-Jeff
======================================
The Vision Egg mailing list
Archives: //www.freelists.org/archives/visionegg
Website: http://www.visionegg.org/mailinglist.html

======================================
The Vision Egg mailing list
Archives: //www.freelists.org/archives/visionegg
Website: http://www.visionegg.org/mailinglist.html


<winmail.dat>

======================================
The Vision Egg mailing list
Archives: //www.freelists.org/archives/visionegg
Website: http://www.visionegg.org/mailinglist.html

Other related posts: