0

My goal is to press a keyboard key, which activates a function which should update a variable within a while loop which is in a broader while loop. I am using Python and some features of Sikuli (Automation Program). My script is meant to keep a camera monitor open, press continue watching every 1-5 minutes and to facilitate switching between cameras with the FKeys. It does that, it would seem except for when one of the while loops in a function is active. When the while loop is active, the variable that determines which camera is meant to be active is not responding to updates which are meant to go global on press of programed Fkey strokes. Again, the program already does almost everything as intended. With the exception of variables like isCamera won't update if I hit a programed key when the while loop within the isNotPaused function is looping. If the script is running code outside of said loop, anywhere else in the app driver loop the variable updates just fine.

import thread
import org.sikuli.basics.Settings;
Settings.MinSimilarity = 0.7;
def runCam1key(event):
    global isCamera
    isCamera = 1
    isScriptPaused = 2
def runCam2key(event):
    global isCamera
    isCamera = 2
    isScriptPaused = 2
def isNotPaused():
    global isCamera
    if isCamera == 1:
        while exists(SOME PICTURE):
           ###LOTS OF IRRELEVANT CODE REDACTED
    if isCamera == 2:
        while exists(SOME PICTURE):
           ###LOTS OF IRRELEVANT CODE REDACTED
    ###And So On Redacted around 800 lines total
switcher = {
    1: isPaused,
    2: isNotPaused,
    3: isSetupPaused,
    4: isStarting,
    5: isTesting
    }
def switch(thePauseSwitch):
    return switcher.get(thePauseSwitch, default)()
### App Driver ->
Env.addHotkey(Key.F1, 0, runPausekey)
Env.addHotkey(Key.F2, 0, runSetupkey)
Env.addHotkey(Key.F3, 0, runResumekey)
Env.addHotkey(Key.F4, 0, runCam1key)
Env.addHotkey(Key.F5, 0, runCam3key)
Env.addHotkey(Key.F6, 0, runCam4key)
Env.addHotkey(Key.F7, 0, runCam5key)
Env.addHotkey(Key.F8, 0, runCam6key)
Env.addHotkey(Key.F9, 0, runCam7key)
Env.addHotkey(Key.F10, 0, runCam8key)
isScriptRunning = True
isScriptPaused = 2
isCamera = 2
###A BUNCH OF IRRELEVANT SETTINGS
while isScriptRunning == True:
    switch(isScriptPaused)

2 Answers 2

2

You need to define your global variable outside any function. Creating local variable with global will not do the job.

Sign up to request clarification or add additional context in comments.

4 Comments

I failed to show in my unedited code that I have this problem even when the variable is created outside of the function.
@RemoteV so please edit it
I did. isCamera = 2 was added to the app driver section, as it is in my real copy of the script. Still no dice, I'm not sure if missing something about passing the variable to the thread of the while loop or what. However, thank you for the help.
Also, I would expect the loop to stop once the variable isCamera = 2 changes. I'm not sure the loop is stopping at that point. Because, in my debug I have console prints that indicate that section of the loop keeps running. So one thing I have tried is making the loop return or pass once any keystroke is detected. However, the best I could figure out is using Control+C as a trigger for a Keyboard interrupt exception handler. This is not ideal as I need this loop to break when the same FKeys that change the camera is pressed.
0

The problem here was that while programing and running from the sikulix-ide, there is some condition where when you kill a script, the environment sometimes carries over the variable to the next running iteration. My workaround is to put a section of my apps that checks for this scenario and resets the variables each time the script runs. I've been meaning to to come back and a give a good example of how to do this, because I found it tricky and a sloppy fix. But, if this thread does not lock me out of it I will come back and give examples at some point.

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.