【问题标题】:Kivy: inherit another dynamic classes valuesKivy:继承另一个动态类值
【发布时间】:2018-03-10 15:30:33
【问题描述】:

我有一个这样的动态类:

<BLMother@BoxLayout>:
    orientation:"horizontal"
    padding: 10, 0
    spacing: 10

对于我的一些 CustomBoxLayout,我想添加一个画布:before。 我可以创建一个新的动态类,它将两者的值结合起来,如下所示:

<BLChildren@BoxLayout>:
    orientation:"horizontal"
    padding: 10, 0
    spacing: 10
    canvas.before:
        Color:
            rgba: 1, 1, 1, 0.8
       Rectangle:
            size: self.size
            pos: self.x + self.width*0.025, self.y    

有没有办法BLChildren 可以继承BLMother 的所有值?

我使用 Kivy (1.10.1.dev0)

【问题讨论】:

    标签: python-3.x class nested kivy kivy-language


    【解决方案1】:

    是的,BLChildren 可以继承 BLMother 的所有值。详情请参考以下示例。

    示例 - 从 BLMother 继承

    main.py

    ​​>
    from kivy.app import App
    from kivy.uix.floatlayout import FloatLayout
    
    
    class RootWidget(FloatLayout):
        pass
    
    
    class TestApp(App):
        title = "With Inheritance of BLMother"
    
        def build(self):
            return RootWidget()
    
    
    if __name__ == "__main__":
        TestApp().run()
    

    test.kv

    #:kivy 1.10.0
    
    <BLMother@BoxLayout>:
        orientation:"horizontal"
        padding: 10, 0
        spacing: 10
    
    <BLChildren@BoxLayout>:
        canvas.before:
            Color:
                rgba: 1, 1, 1, 0.8
            Rectangle:
                size: self.size
                pos: self.x + self.width*0.025, self.y
        BLMother:
    
    <RootWidget>:
        BLChildren:
    

    输出 - 从 BLMother 继承

    【讨论】:

    • 感谢您的详细解释。我尝试了一下,我认为这会创建两个 BoxLayout:BLMother 在 BLChildren 内部。这与我以前的标题相匹配,但我犯了一个错误,我的意思不是嵌套而是继承。对此感到抱歉。我希望只有一个 BoxLayout BLChildren,而这个 BLChildren 应该有自己的价值和 BLMother 的价值。有什么想法可以吗?
    【解决方案2】:

    我不知道你为什么把 pos = self.x + self.width*0.025, self.y 但是 给你:

    -kv:

    <BLMother@BoxLayout>:
       orientation:"horizontal"
       padding: 10, 0
       spacing: 10
    
    <BLChildren@BLMother>:
       canvas.before:
          Color:
             rgba: 1, 1, 1, 0.8
          Rectangle:
             size: self.size
             pos: self.x , self.y
    
    BLChildren:
    

    -py:

    from kivy.app import App
    
    
    class TestApp(App):
       pass
    
    
    if __name__ == "__main__":
       TestApp().run()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-22
      • 1970-01-01
      • 1970-01-01
      • 2016-01-13
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      • 1970-01-01
      相关资源
      最近更新 更多