Python 克维外部规则内在性
我想指定一个特殊的按钮,这样我就不必调整我使用的每个按钮,但是我希望它的事件触发不同类中的函数 main.pyPython 克维外部规则内在性,python,kivy,Python,Kivy,我想指定一个特殊的按钮,这样我就不必调整我使用的每个按钮,但是我希望它的事件触发不同类中的函数 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 = ObjectP
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()
试验电压(千伏)(工作电压)
#:kivy 1.9.0
:
发布时:self.parent.NoInspiration(':'))
:
id:imp
FancyButton:
文字:“笑脸”
盒子布局:
重要的
收到:)
试验电压为0.kv(不工作)
#:kivy 1.9.0
:
发布时:self.parent.NoInspiration(':'))
:
id:imp
盒子布局:
FancyButton:
文字:“笑脸”
盒子布局:
重要的
在第二次测试中,我在FancyButton前面添加了“BoxLayout:”,突然我发现错误:
AttributeError:“BoxLayout”对象没有属性“NoInspiration”
问题:
- 为什么self.parent在第二个示例中仅指直接父级而不是
- 如何让on_release:in
指向函数NoInspiration(),同时将函数保持在 中
<FancyButton>:
on_release: self.imp.NoInspiration(':)')
<Important>:
id: imp
BoxLayout:
FancyButton:
text: "smiley"
imp: root
BoxLayout:
Important
:
发布时:self.imp.NoInspiration(':'))
:
id:imp
盒子布局:
FancyButton:
文字:“笑脸”
根
盒子布局:
重要的
#:kivy 1.9.0
<FancyButton>:
on_release: self.parent.NoInspiration(':)')
<Important>:
id: imp
BoxLayout:
FancyButton:
text: "smiley"
BoxLayout:
Important
<FancyButton>:
on_release: self.imp.NoInspiration(':)')
<Important>:
id: imp
BoxLayout:
FancyButton:
text: "smiley"
imp: root
BoxLayout:
Important