mercredi 28 janvier 2015

manipulate python variable inside a GTK action listener

I'm trying to an action listener that is attached to a checkbox button. My goal is to toggle the variable value ( 0 or 1 ) whenever the checkbox is checked/unchecked.


I come from a C background and am fairly new to python. I've also been reading up on global variable but I cannot seem to grasp it.


I will do an example in C first :



//#includes

int test; //i could say public int test but it is public by default

int foo()
{
test = 1;
return test;
}
void main()
{
test = 0;
print (foo);
}


Some example python code is below.



class someRandomClass:

def on_checkbutton1_toggled(self, widget, data=None):
if test == 0:
test = 1
if test == 1:
test = 0

def __init__(self):
global test
test = 0

if __name__ == "__main__":
main = someRandomClass()
gtk.main()


Executing this python code and clicking on the checkbox throws an UnboundLocalError: local variable 'test' referenced before assignment


I've tried creating the global variable outside of a function but still inside the class - the same method as the example C code.





Aucun commentaire:

Enregistrer un commentaire