简体   繁体   English

外部规则规则的奇异果

[英]Kivy outside rule inherence

I want to specify a special button so I don't have to adjust every button I use, however I want it's event to trigger a function in a different class. 我想指定一个特殊的按钮,这样就不必调整我使用的每个按钮,但是我希望它是触发另一个类中的函数的事件。

main.py main.py

from kivy.app import App
from kivy.uix.button import Button
from kivy.uix.stacklayout import StackLayout
from kivy.properties import ObjectProperty


class FancyButton(Button):
    imp = ObjectProperty(None)


class Important(StackLayout):

    def NoInspiration(self, smile):
        print("Received: {}".format(smile))


class TestApp(App):
    def build(self):
        pass

if __name__ == '__main__':
    TestApp().run()

test.kv (working) test.kv(工作)

#:kivy 1.9.0

<FancyButton@Button>:
    on_release: self.parent.NoInspiration(':)')


<Important>:
    id: imp

    FancyButton:
        text: "smiley"

BoxLayout:
    Important

Received: :) 收到::)

test.kv ( not working) test.kv( 工作)

#:kivy 1.9.0

<FancyButton>:
    on_release: self.parent.NoInspiration(':)')


<Important>:
    id: imp

    BoxLayout:
        FancyButton:
            text: "smiley"

BoxLayout:
    Important

In the 2nd test.kv I added 'BoxLayout:' in front of FancyButton and suddenly I get the error: 在第二个test.kv中,我在FancyButton前面添加了“ BoxLayout:”,然后突然出现错误:

AttributeError: 'BoxLayout' object has no attribute 'NoInspiration' AttributeError:“ BoxLayout”对象没有属性“ NoInspiration”

Question

  • Why does self.parent only refer to the direct parent and not < Important > in the 2nd example? 为什么在第二个示例中self.parent仅引用直接父级而不引用<Important>?
  • How can I have on_release: in < FancyButton > point to the function NoInspiration() while keeping the function in < Important >? 如何在<FancyButton>中将on_release指向函数NoInspiration(),同时将该函数保留在<Important>中?

Follow up questions 跟进问题

Change the kv to this 将kv更改为此

<FancyButton>:
    on_release: self.imp.NoInspiration(':)')


<Important>:
    id: imp

    BoxLayout:
        FancyButton:
            text: "smiley"
            imp: root

BoxLayout:
    Important

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

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