Search code examples
pythonkivykivy-language

Increasing value of variable via button in kivy


I am trying to increase variable value, but I get this error:

"UnboundLocalError: local variable 'x' referenced before assignment"

Can someone help me please?

.kv:

<InputValuesScr>:
    GridLayout:
        rows:2
        TextInput:
            input_filter: 'float'
            font_size: 50
            text: 'Please, input value of x1'
            id: xval
            multiline: False
            on_touch_down: self.text = ''
        Button:
            text: 'Submit'
            on_press: root.x_changer()

.py:

global x
x = 1

class TypeOfGeometryScr(Screen):
    pass

class SelectDemensionsScr(Screen):

    def submit_dn(self):
        global dn
        dn = self.ids.demensions.text

class InputValuesScr(Screen):

    def x_changer(self):
        x = x
        x += 1
        self.ids.xval.text = 'Please, input value of x' + str(x)


Solution

  • This is happening becouse the global x is in the global scope and not in the x_changer() function lac scope. For solve this u need to move the global x inside the local scope of the function

    def x_changer(self):
            global x
            x = x
            x += 1
            self.ids.xval.text = 'Please, input value of x' + str(x)