RASelkirk
Posts: 66
Joined: Mon Jan 07, 2019 2:48 pm

Using one button to cycle thru 3 scenarios

Thu Jan 24, 2019 5:35 pm

Hi All,

Constant errors with this code no matter what:

Code: Select all

global btnclick
btnclick = 0

def do_cycle():
    If btnclick == 0:
        labeloffo.configure(image=picred)
        labeloffo.image=picred
        btnclick = btnclick + 1
    Elif btnclick == 1:
        labeloffc.configure(image=picgrn)
        labeloffc.image=picgrn
        btnclick = btnclick + 1
    Elif btnclick == 2:
        labeloffo.configure(image=picoff)
        labeloffo.image=picoff
        labeloffc.configure(image=picoff)
        labeloffc.image=picoff
        btnclick = 0
    Else:
I have one button that calls this function to cycle thru 3 graphic scenarios. It should increment by the "btnclick" variable switching 2 "LEDs" between off and on. Keep getting the invalid syntax error mark just after "btnclick" and the first "==". I've Googled the past hour and am not seeing the problem. Should be a straight comparison, if the counter is 0: do this, if it's 1, do this, etc...

Russ

pcmanbob
Posts: 6209
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Using one button to cycle thru 3 scenarios

Thu Jan 24, 2019 6:17 pm

If you want to use a global variable in your function then you have to include the line " global btnclick " as the first line in your function so the following code knows to use the global variable and not a local one just in the function.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Using one button to cycle thru 3 scenarios

Thu Jan 24, 2019 10:48 pm

to elaborate a little more.

When a function is called it will create/allocate any variables that are used within the function that are not declared global then when the function returns it will destroy/de-allocate all variables that are not global.

So every time you call your function it creates a new variable that is equal to zero. If you declare it as global then it will only create it once and use it each time the function is called :)

RASelkirk
Posts: 66
Joined: Mon Jan 07, 2019 2:48 pm

Re: Using one button to cycle thru 3 scenarios

Thu Jan 24, 2019 11:43 pm

pcmanbob wrote:
Thu Jan 24, 2019 6:17 pm
If you want to use a global variable in your function then you have to include the line " global btnclick " as the first line in your function so the following code knows to use the global variable and not a local one just in the function.
OK, I put it inside the loop and I still get the same error. Both "global btnclick" with/without "btnclick = 0". Small "g" and capital "G". Even so, how will this ever increment if it's declared as 0? Please show a working example so my ancient (65 year old) mind can comprehend this mess...

Russ

PS- I really don't care about the scope of the variable, I just want it increment!

User avatar
OutoftheBOTS
Posts: 711
Joined: Tue Aug 01, 2017 10:06 am

Re: Using one button to cycle thru 3 scenarios

Fri Jan 25, 2019 8:34 am

Code: Select all

global btnclick
btnclick = 0

def do_cycle():
	global btnclick
    if btnclick == 0:
        labeloffo.configure(image=picred)
        labeloffo.image=picred
        btnclick = btnclick + 1
    elif btnclick == 1:
        labeloffc.configure(image=picgrn)
        labeloffc.image=picgrn
        btnclick = btnclick + 1
    elif btnclick == 2:
        labeloffo.configure(image=picoff)
        labeloffo.image=picoff
        labeloffc.configure(image=picoff)
        labeloffc.image=picoff
        btnclick = 0

pcmanbob
Posts: 6209
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Using one button to cycle thru 3 scenarios

Fri Jan 25, 2019 12:20 pm

RASelkirk wrote:
Thu Jan 24, 2019 11:43 pm

OK, I put it inside the loop and I still get the same error. Both "global btnclick" with/without "btnclick = 0". Small "g" and capital "G". Even so, how will this ever increment if it's declared as 0? Please show a working example so my ancient (65 year old) mind can comprehend this mess...

Russ

PS- I really don't care about the scope of the variable, I just want it increment!
well it works for me, so using you code as a base this little test program runs you function as if a button was being pressed.

Code: Select all

import time
loop = 0
btnclick = 0

def do_cycle():
    global btnclick
    if btnclick == 0:
        print ("first if statment" , btnclick)
        btnclick = btnclick + 1
    elif btnclick == 1:
        print ("second if statment" , btnclick)
        btnclick = btnclick + 1
    elif btnclick == 2:
        print ("third if statment" , btnclick)
        btnclick = 0
        print ("end of if statments ", btnclick)

        
while loop < 7:
    print (" this is were you push your button")
    do_cycle()
    time.sleep(3)
    loop = loop + 1
    
    
after looping through the function twice it stops.

this is the result

Code: Select all

[email protected]:~ $ python3 gloop.py
 this is were you push your button
first if statment 0
 this is were you push your button
second if statment 1
 this is were you push your button
third if statment 2
end of if statments  0
 this is were you push your button
first if statment 0
 this is were you push your button
second if statment 1
 this is were you push your button
third if statment 2
end of if statments  0
 this is were you push your button
first if statment 0
[email protected]:~ $
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

RASelkirk
Posts: 66
Joined: Mon Jan 07, 2019 2:48 pm

Re: Using one button to cycle thru 3 scenarios

Fri Jan 25, 2019 5:59 pm

Geeze Louise! :shock: After another hr of wasting time, it turned out to be a simple issue of improper case on the if/elif statements. :oops:

Sorry for the bother, all good now!

Russ

Return to “Python”