RemoteV
RemoteV

Reputation: 1

Python While Loop - Variable Not Updating across Functions

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)

Upvotes: 0

Views: 175

Answers (2)

RemoteV
RemoteV

Reputation: 1

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.

Upvotes: 0

Eugene S
Eugene S

Reputation: 6910

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

Upvotes: 2

Related Questions