简体   繁体   English

从Kivy Python中的类属性出价

[英]biding a value from a class property in kivy python

I have multiple classes happily communicating thanks to x being an object of class foo: 由于x是foo类的对象,我有多个类可以愉快地进行通信:

class foo(EventDispatcher):
   def variable_input(self, *args):
      self.a='This is an observed value being changed'

   def __init__(self):
      self.a=StringProperty('')

now I create an object X being an instance of class foo 现在我创建一个对象X它是foo类的实例

X=foo()

I have a Screen as a part of a ScreenManager containing a Label with id vystup 我有一个Screen作为包含ID为vystupLabelScreenManager的一部分

class Hlavne(Screen):
   pass

What I am trying to do is to write function on_propname with Xa as a propname. 我所试图做的是写函数on_propnameXa作为PROPNAME。 How do I point to value belonging to instance of other class. 我如何指向属于其他类实例的值。

I have read: 我读过了:

Warning 警告

Be careful with 'on_'. 注意“ on_”。 If you are creating such a callback on a property you are inheriting, you must not forget to call the superclass function too. 如果要在继承的属性上创建此类回调,则一定不要忘记也调用超类函数。

But as its common with kivy's documentation it does not tell you how. 但是,与kivy文档一样,它不会告诉您如何使用。

Kivy Properties are declared at class level, not in an instance. Kivy属性在级别而不是在实例级别声明。 (bad) (坏)

def __init__(self):
   self.a=StringProperty('')

Binding to a property and using an on_property event are two different things. 绑定属性和使用on_property事件是两件事。

from kivy.clock import Clock
from kivy.base import runTouchApp
from kivy.uix.widget import Widget
from kivy.properties import NumericProperty


class Test(Widget):
    hop = NumericProperty(0)

    def __init__(self, **kwargs):
        super(Test, self).__init__(**kwargs)
        Clock.schedule_interval(
            lambda *dt: setattr(self, 'hop', self.hop + 1), 0.1
        )

        # bind to property
        self.bind(hop=self.bind_hop)

    def on_hop(self, instance, value):
        # no event, no super() necessary
        # unless inherrited from "Test"
        print('on_hop:', instance, value)

    def bind_hop(self, instance, value):
        print('bind_hop:', instance, value)

runTouchApp(Test())

You can check for an instance in each of the function with isinstance because of the instance argument passed into each event/bind of a Kivy property mentioned here . 您可以使用isinstance在每个函数中检查实例,因为instance参数已传递到此处提到的Kivy属性的每个事件/绑定中。

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

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