0

I define a varible, name 'Decisoin_name' and set -1 at first

and I try to change it in a def of a class

because I’d like to add 1 everytime when I call the def

but the system send me a message

"local variable 'Decision_name' referenced before assignment"

what can I do?

Would you give me a solution to fix it ? thank you

The following is my code

Decision_name = -1

class Decision_Dialog(QDialog):

    def sendback(self):

        Decision_name+=1

        print(Decision_name)

        self.close()
Mike Scotty
  • 10,530
  • 5
  • 38
  • 50
余昌翰
  • 13
  • 3

1 Answers1

0

You can't change a global name from a class method directly, you need to declare it as a global variable beforehand:

class Decision_Dialog(QDialog):
    def sendback(self):
        global Decision_name
        Decision_name += 1

Although if it does not need to be a global variable, you can take different routes e.g. make it a class variable and let each instance modify to it's need, or make it a instance variable by defining in __init__ and make necessary changes afterwards.

Also, you should be using snake_case for variable names e.g. decision_name.

heemayl
  • 39,294
  • 7
  • 70
  • 76