简体   繁体   English

如何在Kivy中访问全局变量

[英]How can i access a global variable in kivy

I need to update a label in kivy with a global variable in python. 我需要使用python中的全局变量来更新kivy中的标签。 How can I do that 我怎样才能做到这一点

The final result I need is to update the pos of canvas according to the switch interrupt. 我需要的最终结果是根据切换中断来更新canvas的位置。

.Py 的.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 .KV

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

You already declared x1 as a global variable in your class. 您已经在类中将x1声明为全局变量。 In your method try printing self.x1." print self.x1" 在您的方法中尝试打印self.x1。“ print self.x1”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM