【问题标题】:Getting a property from a different widget in Kivy从 Kivy 中的不同小部件获取属性
【发布时间】:2017-08-04 23:21:25
【问题描述】:

如何在不同的小部件中使用属性? 我有这个代码:

Python 文件

class Control(ToggleButton):
    width_multiplier = NumericProperty(1)


class MainScreen(StackLayout):
    pass


class HomeControl(App):

    def build(self):
        return MainScreen()


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

KV 文件

<Control>:
    size_hint: None,None
    height: '100dp'
    width: self.width_multiplier * self.height + (self.width_multiplier - 1) * spacing
    halign: 'center'

<MainScreen>:
    orientation: 'lr-tb'
    spacing: '10dp'
    padding: '15dp'

    Control:
        text: 'Button'

    Control:
        text: 'Long Button'
        width_multiplier: 2

Control 小部件的宽度应该是 width_multiplier 乘以小部件的高度加上 MainScreen 的子级之间的间距。如何在Control 角色中使用MainScreen 的间距属性? 我是 Kivy 的新手,所以这可能是一个愚蠢的问题,但我希望有人可以帮助我。

【问题讨论】:

    标签: python kivy kivy-language


    【解决方案1】:

    在您的情况下,您可以使用 parent 属性

    <Control>:
        size_hint: None,None
        height: '100dp'
        width: self.width_multiplier * self.height + (self.width_multiplier - 1) * self.parent.spacing[0]  <--- 
        halign: 'center'
    

    【讨论】:

    • 我不得不使用self.parent.spacing[0],因为它是一个列表。但是仍然感谢您的回答。不幸的是,我没有足够的声誉来支持您的回答。
    • 酷,固定到 spacing[0]
    猜你喜欢
    • 2016-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-12
    • 2015-07-04
    • 1970-01-01
    • 2016-10-12
    相关资源
    最近更新 更多