从 Kivy 中的另一个小部件获取 属性
Getting a property from a different widget in Kivy
如何在不同的小部件中使用 属性?
我有这个代码:
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 的新手,所以这可能是一个愚蠢的问题,但我希望有人能帮助我。
在您的情况下,您可以使用 parent 属性
<Control>:
size_hint: None,None
height: '100dp'
width: self.width_multiplier * self.height + (self.width_multiplier - 1) * self.parent.spacing[0] <---
halign: 'center'
如何在不同的小部件中使用 属性? 我有这个代码:
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 的新手,所以这可能是一个愚蠢的问题,但我希望有人能帮助我。
在您的情况下,您可以使用 parent 属性
<Control>:
size_hint: None,None
height: '100dp'
width: self.width_multiplier * self.height + (self.width_multiplier - 1) * self.parent.spacing[0] <---
halign: 'center'