我如何能? C#type" on_< property-name>"事件和bind() - 在pyton中 [英] Howdoi? C# type "on_<property-name>" events and bind() - in pyton

查看:75
本文介绍了我如何能? C#type" on_< property-name>"事件和bind() - 在pyton中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自'C'/ unix背景并学习Python。 kivy框架 - 用于python - 实现了一个精彩的事件驱动架构,我想在我自己的应用程序中复制 - 但我没有使用Kivy,因为我会避免在这里记录以便于记忆。



在kivy上,人们可以使用'on_< property-name>'来观察对属性(集合)的更改。 on_< property-name>当属性值改变时,通过hieratchy调用。如果你自己定义这个类:

 来自 kivy  import  Widget 

class MyClass(Widget):
我想我们需要从EventDispatcher开始?

my_property = NumericProperty( 1 NumbericProperty是一个kivy类

此方法可以通过此类的基础克拉覆盖
def on_my_property(self,instance,value):
请注意'on_< property-name> ;一定不要忘记打电话
超类函数'on_< property>'另外,
但我不知道该怎么做?
super(MyClass,self).on .... ??

print ' 我的属性a更改为',value)



如何复制此架构?

我看过这个属性类的Kivy源文件 https://github.com/kivy/kivy/blob /master/kivy/properties.pyx#L219 但是远远超出了我的需要,我很难承担代码。我看不出On_< property-name>的方式和位置定义方法/函数/事件(无论它被调用),以便我可以在MyClass中覆盖它



所以在我的设计中,我想要一个根类:

  class  MyProperty(object): 对象是否正确? 
我有不知道从哪个子类?
或如何提供on_< property-name> ;事件?



这样我就可以继承:

  class  MyModel(EventDispatcher): 这是正确的吗? 
声明名为data的属性
data = MyProperty(。 ..) 不知道该怎么做?

MySubModel(MyModel): 对象是否正确???
覆盖超类MyModel的on_data事件以响应对data的更改
def on_data():
一些行动代码



但我不知道该怎么做实现这个on_< property>以便适当地调用超类并且on_< property>可以通过我定义的子类覆盖。



请帮助,不要忘记给出所有重要的解释。请,请不要把我链接到谷歌,因为显然我已经到了谷歌搜索网站和页面,并来到这里寻求帮助。并且,请不要提供另一种解决方案,因为我已经花了这么长时间,我现在必须理解并实现相同的设计模式以保持理智!谢谢



我尝试过:



kivy/properties.pyx at master·kivy / kivy·GitHub [ ^ ]

解决方案

参见 python - 如何在价值变化时触发功能? - 堆栈溢出 [ ^ ]。


找到这个并且EventDispatcher代码通过使用内省来自动创建on_事件__attr__ list



kivy / _event。主人的pyx·kivy / kivy·GitHub [ ^ ]

I am coming from a 'C'/unix background and learning Python. The kivy framework - for python - implements a wonderful event driven architecture, that I would like to replicate in my own app - but I'm not using Kivy due to reasons I shall avoid documenting here for sake of ease of dicsussion.

On kivy one can observe changes to a propery (set) using 'on_<property-name>'. The on_<property-name> is invoked through the hieratchy when the property value changes. If defining the class yourself:

from kivy import Widget

class MyClass(Widget):
   # I guess we need to start with EventDispatcher??

   my_property = NumericProperty(1) # NumbericProperty is a kivy class

   # This method can be overriden by base clases of this class
   def on_my_property(self, instance, value):
      # Note that 'on_<property-name>' must not forget to call
      # the superclass function 'on_<property>' also ,
      # but I do not know how to do this?
      super(MyClass, self).on.... ??

      print('My property a changed to', value)


How do I replicate this architecute?
I have looked at this Kivy source file for property class https://github.com/kivy/kivy/blob/master/kivy/properties.pyx#L219 but there is far far more going on than I need and it is difficult for me to undertand the code. I do not see how or where the "On_<property-name>" method/function/event (whatever it is called) is defined so that I can override it in MyClass

So in my design, I want a root class to be:

class MyProperty(object): # Is object correct?
  # I have no idea what to subclass from ?
  # or how to provide the on_<property-name> event?


So that I can subclass:

class MyModel(EventDispatcher): # Is this right???
   # declare property called "data"
   data=MyProperty(...) # not sure what to do here?

class MySubModel(MyModel): # Is Object right???
   # override super class MyModel's on_data event to react to changes to "data"
   def on_data():
      # some action code


But I have no idea how to implement this the on_<property> so that the superclass is called appropriately and on_<property> can be overriden by subclasses I define.

Help please , and don't forget to give the all important explanation. Please, PLEASE dont link me to google because clearly I have got to a point where I have googled and looked at sites and pages, and come here for help. And, PLEASE dont offer another solution because I've spent so long on this, that I must now understand and implement the same design patterns for sanity !! Thank you

What I have tried:

kivy/properties.pyx at master · kivy/kivy · GitHub[^]

解决方案

See python - How to trigger function on value change? - Stack Overflow[^].


Found this and the EventDispatcher code does the automatic "on_" event creation by using introspection of the __attr__ list

kivy/_event.pyx at master · kivy/kivy · GitHub[^]


这篇关于我如何能? C#type&quot; on_&lt; property-name&gt;&quot;事件和bind() - 在pyton中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆