Search code examples
pythonkivykivy-language

How can i access a global variable in kivy


I need to update a label in kivy with a global variable in python. How can I do that

The final result I need is to update the pos of canvas according to the switch interrupt.

.Py

global x1

def hi():
    print "hi"
    x1 = 20
    print x1
class Mode1(Screen):
    global x1
    x1 = NumericProperty()
    y1 = NumericProperty()
    Buttonstatus = ''
    ButtonPressed=''

    def on_touch_move(self, touch):
        print 'x1'
        global x1
        print x1
        hi()

.KV

canvas.after:
    Color:
        rgb:[1, 0, 0,1]
    Rectangle:
        pos:root.x1,root.y1
        size:20,20

Solution

  • You already declared x1 as a global variable in your class. In your method try printing self.x1." print self.x1"